Channel.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\controller\Backend;
  4. use app\common\library\Rabbitmq;
  5. use app\common\library\Redis;
  6. use app\common\model\AuthGroup;
  7. use app\common\utility\WeChatMenu;
  8. use app\main\constants\AdminConstants;
  9. use app\main\helper\StringHelper;
  10. use app\main\service\AdminService;
  11. use app\main\service\GoodsService;
  12. use app\main\service\OfficialAccountsService;
  13. use fast\Random;
  14. use think\db\Query;
  15. use think\Log;
  16. use think\Session;
  17. /**
  18. * 渠道商管理
  19. *
  20. * @icon fa fa-user
  21. * @remark 渠道商管理
  22. */
  23. class Channel extends Backend
  24. {
  25. const SYNC_MQ_QUEUE_NAME = 'Q_SYNC_USER';
  26. const SYNC_MQ_EXCHANGE_NAME = 'E_SYNC_USER';
  27. /**
  28. * @var \app\common\model\Admin
  29. */
  30. protected $modelAdmin = null;
  31. protected $dataLimit = false;
  32. protected $childrenGroupIds = [];
  33. protected $searchFields = 'id,username,nickname';
  34. protected $relationSearch = true;
  35. /**
  36. * 无需登录的方法,同时也就不需要鉴权了
  37. * @var array
  38. */
  39. protected $noNeedRight = ['ajaxgetophostlistbyplatformid', 'ajaxgetwxpaylistbyplatformid', 'checkplatformauth','select'];
  40. public function _initialize()
  41. {
  42. parent::_initialize();
  43. $this->modelAdmin = model('Admin');
  44. $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin() ? true : false);
  45. $payList = model('AdminExtend')->getPayMethodList();
  46. unset($payList[3]);
  47. unset($payList[4]);
  48. $this->view->assign("payMethodList", $payList);
  49. $this->assignconfig("admin", ['id' => $this->auth->id]);
  50. $this->assign("group", $this->group);
  51. $payerList = model('Payer')->getPayerList();
  52. $this->assign("payerList", $payerList);
  53. }
  54. /**
  55. * 查看
  56. */
  57. public function index()
  58. {
  59. // $isassociated: 1=> 正常 2=> 关联 3=> 删除
  60. $kl_id = $this->request->param('kl_id') ?? 0;
  61. // KL规则是否关联渠道
  62. if ($kl_id && $this->request->param('isassociated')) {
  63. $isassociated = $this->request->param('isassociated');
  64. } else {
  65. $isassociated = 1;
  66. }
  67. $kl_id = $this->request->param('kl_id') ?? 0;
  68. if ($kl_id && $this->request->param('isassociated')) {
  69. $isassociated = $this->request->param('isassociated');
  70. } else {
  71. $isassociated = 1;
  72. }
  73. $admin_where_in = [];
  74. $admin_where_not_in = [];
  75. if ($isassociated == 3) {
  76. // 查询已关联的渠道商,即将做删除操作
  77. $except_rule_obj = model('KlRuleManage')->get(['id' => $kl_id]);
  78. $except_rule_ids = $except_rule_obj->channel_ids;
  79. if (empty($except_rule_ids)) {
  80. $admin_where_in['admin.id'] = ['IN', '-1'];
  81. }
  82. if (!empty($except_rule_ids) && $except_rule_ids != '*') {
  83. $admin_where_in['admin.id'] = ['IN', $except_rule_ids];
  84. }
  85. } elseif ($isassociated == 2) {
  86. // 查询未关联的渠道商,即将做关联操作
  87. $except_rule_obj = model('KlRuleManage')->get(['id' => $kl_id]);
  88. $except_rule_ids = $except_rule_obj->channel_ids;
  89. if (!empty($except_rule_ids) && $except_rule_ids != '*') {
  90. $admin_where_not_in['admin.id'] = ['NOT IN', $except_rule_ids];
  91. }
  92. }
  93. $isIndex = 0;
  94. //筛选渠道
  95. $operate = $this->request->param('operate') ?? '';
  96. $channel_ids = $this->request->param('channel_ids') ?? '';
  97. if ($operate) {
  98. if ($operate == 'add') {
  99. //添加
  100. $isIndex = 1;
  101. } elseif ($operate == 'remove') {
  102. //移除
  103. $isIndex = 2;
  104. }
  105. }
  106. $this->assignconfig('channel_ids', $channel_ids);
  107. $this->assignconfig('operate', $operate);
  108. $this->assign('is_index', $isIndex);
  109. $this->assignconfig('is_index', $isIndex);
  110. $platformlist = model("platform")->getPlatformList();
  111. $ophosts = OfficialAccountsService::instance()->getOphostModel()
  112. ->where('status', 1)
  113. ->column('id, concat(id," | ",host," \| ",p_desc)');
  114. $wxpays = OfficialAccountsService::instance()->getWxpayModel()
  115. ->where('status', 1)
  116. ->column('id, concat(id," | ",pay_host," \| ",name,if(is_closure=\'1\',"(被封)",""))');
  117. $this->assignconfig('platforms', $platformlist);
  118. $this->assignconfig('ophosts', $ophosts);
  119. $this->assignconfig('wxpays', $wxpays);
  120. $this->assignconfig('isassociated', $isassociated);
  121. $this->assignconfig('kl_id', $kl_id);
  122. $this->assign('isassociated', $isassociated);
  123. $this->assign('kl_id', $kl_id);
  124. if ($this->request->isAjax()) {
  125. //选择渠道列表
  126. if ($operate) {
  127. if ($channel_ids && !is_null($channel_ids)) {
  128. $channelsRow = model("ChannelMenuList")->where('id', 'eq', $channel_ids)->find();
  129. if ($channelsRow) {
  130. $channel_ids = $channelsRow['channel_id'];
  131. }
  132. }
  133. if ($operate == 'add') {
  134. //添加
  135. $channel_ids = explode(',', $channel_ids);
  136. if (!empty($channel_ids) && $channel_ids[0] != '*') {
  137. $admin_where_not_in['admin.id'] = ['NOT IN', $channel_ids];
  138. }
  139. } elseif ($operate == 'remove') {
  140. //移除
  141. if (empty($channel_ids)) {
  142. $admin_where_in['admin.id'] = ['IN', '-1'];
  143. }
  144. if (!empty($channel_ids) && $channel_ids[0] != '*') {
  145. $admin_where_in['admin.id'] = ['IN', $channel_ids];
  146. }
  147. }
  148. }
  149. //今天
  150. $today = date('Ymd', time());
  151. //昨天
  152. $yesterday = date('Ymd', strtotime('-1 days'));
  153. $channels = $this->modelAdmin->join('auth_group_access a', 'a.uid= admin.id')->where('a.group_id', 'lt', 3)
  154. ->select();
  155. $cNames = [];
  156. foreach ($channels as $channel) {
  157. $cNames[$channel->id] = $channel->nickname;
  158. }
  159. // list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  160. $filter = $this->request->get("filter", '');
  161. $sort = $this->request->get("sort", "id");
  162. $order = $this->request->get("order", "DESC");
  163. $offset = $this->request->get("offset", 0);
  164. $limit = $this->request->get("limit", 0);
  165. $filter = json_decode($filter, true);
  166. $where = [];
  167. $idMap = [
  168. 'ophost' => 'ophost_id',
  169. 'wxpay' => 'wxpay_id',
  170. 'menuophost' => 'menuophost_id',
  171. 'menuwxpay_host' => 'menuwxpay_id',
  172. ];
  173. if ($filter) {
  174. foreach ($filter as $k => $v) {
  175. if (array_key_exists($k, $idMap)) {
  176. $where["ac.{$idMap[$k]}"] = $v;
  177. } elseif ($k == "admin.id" || $k == "admin_extend.reward_state" || $k == "status" || $k == "ac.platform_id") {
  178. $where[$k] = ['=', $v];
  179. } else {
  180. $where[$k] = ['like', "%{$v}%"];
  181. }
  182. }
  183. }
  184. $fileChannel = [];
  185. $authAccess = model('AuthGroupAccess')->where('uid', $this->auth->id)->find();
  186. if ($authAccess->group_id == 2) {
  187. $fileChannel['adminExtend.create_by'] = $this->auth->id;
  188. }
  189. if ($isassociated != 1 || $isIndex != 0) {
  190. $total = $this->modelAdmin
  191. ->with("adminExtend")
  192. ->join("admin_config ac", "admin.id = ac.admin_id", "left")
  193. ->join('auth_group_access a', 'a.uid= admin.id')
  194. ->where($where)
  195. ->where($fileChannel)
  196. ->where('a.group_id', 3)
  197. ->where($admin_where_in)
  198. ->where($admin_where_not_in)
  199. ->count();
  200. $list = $this->modelAdmin
  201. ->with("adminExtend")
  202. ->join("admin_config ac", "admin.id = ac.admin_id", "left")
  203. ->join('auth_group_access a', 'a.uid= admin.id')
  204. //->join('orders_collect o','o.admin_id = admin.id and o.type=3 and o.flag =3 and business_line="0"','left')
  205. //->join('admin_money m','m.admin_id = admin.id','left')
  206. ->where($where)
  207. ->where($fileChannel)
  208. ->where('a.group_id', 3)
  209. ->where($admin_where_in)
  210. ->where($admin_where_not_in)
  211. //->field('admin.id as id,o.id as oid,o.recharge_money as recharge_money,m.benefit_money as benefit_money,m.no_cash_money as no_cash_money,ac.guide_domain,ac.is_fouce,ac.entryhost_id,ac.platform_list,ac.appid,ac.qrcode_image')
  212. ->field('admin.id as id,ac.guide_domain,ac.is_fouce,ac.entryhost_id,ac.platform_list,ac.appid,ac.qrcode_image')
  213. ->order($sort, $order)
  214. ->limit($offset, $limit)
  215. ->select();
  216. foreach ($list as $k => &$v) {
  217. $v['admin_extend']['create_by'] = isset($cNames[$v['admin_extend']['create_by']]) ? $cNames[$v['admin_extend']['create_by']] : '';
  218. }
  219. //渠道商开户人列表
  220. $create_list = $offset <= 0 ? model('Admin')->getListByGroup(2, [],
  221. 'admin.id,admin.username,admin.nickname') : [];
  222. $result = array("total" => $total, "rows" => $list, "create_list" => $create_list);
  223. return json($result);
  224. }
  225. # 结束
  226. $total = $this->modelAdmin
  227. ->with("adminExtend")
  228. ->join("admin_config ac", "admin.id = ac.admin_id", "left")
  229. ->join('auth_group_access a', 'a.uid= admin.id')
  230. ->where($where)
  231. ->where($fileChannel)
  232. ->where('a.group_id', 3)
  233. ->count();
  234. $list = $this->modelAdmin
  235. ->with("adminExtend")
  236. ->join("admin_config ac", "admin.id = ac.admin_id", "left")
  237. ->join('auth_group_access a', 'a.uid= admin.id')
  238. ->join('orders_collect o', 'o.admin_id = admin.id and o.type=3 and o.flag =3 and business_line="0"',
  239. 'left')
  240. ->join('admin_money m', 'm.admin_id = admin.id', 'left')
  241. ->where($where)
  242. ->where($fileChannel)
  243. ->where('a.group_id',3)
  244. ->field('admin.id as id,o.id as oid,o.recharge_money as recharge_money,m.cash_money,m.count_cash_money,m.split_money,ac.guide_domain,ac.is_fouce,ac.entryhost_id,ac.platform_list,ac.appid,ac.qrcode_image')
  245. ->order($sort, $order)
  246. ->limit($offset, $limit)
  247. ->select();
  248. $todayRes = $this->modelAdmin
  249. ->with("adminExtend")
  250. ->join("admin_config ac", "admin.id = ac.admin_id", "left")
  251. ->join('auth_group_access a', 'a.uid= admin.id')
  252. ->join('orders_collect o',
  253. 'o.admin_id = admin.id and o.type=1 and o.flag =3 and business_line="0" and o.createdate=' . $today,
  254. 'left')
  255. ->where($where)
  256. ->where($fileChannel)
  257. ->where('a.group_id', 3)
  258. ->field('admin.id as id,o.id as oid,o.recharge_money as recharge_money')
  259. ->order($sort, $order)
  260. ->limit($offset, $limit)
  261. ->select();
  262. $yesterdayRes = $this->modelAdmin
  263. ->with("adminExtend")
  264. ->join("admin_config ac", "admin.id = ac.admin_id", "left")
  265. ->join('auth_group_access a', 'a.uid= admin.id')
  266. ->join('orders_collect o',
  267. 'o.admin_id = admin.id and o.type=1 and o.flag =3 and business_line="0" and o.createdate=' . $yesterday,
  268. 'left')
  269. ->where($where)
  270. ->where($fileChannel)
  271. ->where('a.group_id', 3)
  272. ->field('admin.id as id,o.id as oid,o.recharge_money as recharge_money')
  273. ->order($sort, $order)
  274. ->limit($offset, $limit)
  275. ->select();
  276. foreach ($list as $k => &$v)
  277. {
  278. $v['no_cash_money'] = $v['benefit_money'] = 0;
  279. //未提现金额,结算单金额
  280. $money_collect = model('orders_collect')->sumCollect($v['id'],1,3, '20180101', date("Ymd"));
  281. if ($money_collect) {
  282. $v['benefit_money'] = $money_collect['recharge_money_benefit'];
  283. $v['no_cash_money'] = StringHelper::moneyFormat(($money_collect['recharge_money_benefit']*100-$v['cash_money']*100-$v['count_cash_money']*100 - $v['split_money']*100)/100);
  284. }
  285. $v['entryhost'] = "";
  286. $entryinfo = model("entryhost")->getinfo($v['entryhost_id']);
  287. if ($entryinfo) {
  288. $v['entryhost'] = $entryinfo['host'];
  289. }
  290. $adminconfiginfo = model("AdminConfig")->getAdminInfoAll($v['id']);
  291. $v['ophost'] = "";
  292. $v['wxpay'] = "";
  293. $v['menuophost'] = "";
  294. $v['menuwxpay_host'] = "";
  295. if ($adminconfiginfo) {
  296. $v['ophost'] = $adminconfiginfo['ophost_host'];
  297. $v['wxpay'] = $adminconfiginfo['wxpay_name'] . "-{$adminconfiginfo['wxpay_pay_host']}";
  298. if (isset($adminconfiginfo['menuophost'])) {
  299. $v['menuophost'] = $adminconfiginfo['menuophost'];
  300. }
  301. if (isset($adminconfiginfo['menuwxpay_host'])) {
  302. $v['menuwxpay_host'] = $adminconfiginfo['menuwxpay_host'];
  303. }
  304. }
  305. $v['platforms'] = "";
  306. if (!empty($v['platform_list'])) {
  307. $platarr = explode(",", trim($v['platform_list'], ","));
  308. foreach ($platformlist as $pk => $pv) {
  309. if (in_array($pv['id'], $platarr)) {
  310. $token = model("ptoken")->getToken($pv['id'], $v['id']);
  311. if ($token) {
  312. $v['platforms'] .= $pv['name'] . "-已授权<br>";
  313. } else {
  314. $v['platforms'] .= $pv['name'] . "-未授权<br>";
  315. }
  316. }
  317. }
  318. }
  319. if (empty($v['platforms'])) {
  320. $v['platforms'] = "无";
  321. }
  322. $v['admin_extend']['create_by'] = isset($cNames[$v['admin_extend']['create_by']]) ? $cNames[$v['admin_extend']['create_by']] : '';
  323. $v['today_recharge_money'] = $todayRes[$k]['recharge_money'];
  324. $v['yesterday_recharge_money'] = $yesterdayRes[$k]['recharge_money'];
  325. //关联短链域名数量
  326. $v['short_relation_count'] = model('ShortRelation')->getRelationCount($v['id']);
  327. //关联导粉域名数量
  328. $v['guide_relation_count'] = model('GuideRelation')->getRelationCount($v['id']);
  329. }
  330. unset($v);
  331. //渠道商开户人列表
  332. $create_list = $offset <= 0 ? model('Admin')->getListByGroup(2, [],
  333. 'admin.id,admin.username,admin.nickname') : [];
  334. $result = array("total" => $total, "rows" => $list, "create_list" => $create_list);
  335. return json($result);
  336. }
  337. return $this->view->fetch();
  338. }
  339. /**
  340. * 渠道商订单明细
  341. */
  342. /*public function orders($ids)
  343. {
  344. $ids = intval($ids);
  345. $userInfo = model('Admin')->get($ids);
  346. $list = model('Orders')->join('User u', 'u.id=orders.user_id')->join('Book b', 'orders.book_id=b.id', 'LEFT')
  347. ->where('u.channel_id', $ids)
  348. ->field('u.id,orders.user_id,u.avatar,u.nickname,orders.payid,orders.money,orders.state,orders.kandian,orders.free_kandian,b.name,b.author,orders.createtime')
  349. ->order('orders.createtime', 'desc')
  350. ->paginate(10);
  351. if ($list) {
  352. foreach ($list as $key => $val) {
  353. $list[$key]['createtime'] = date('Y-m-d H:i:s', $val['createtime']);
  354. }
  355. }
  356. $this->assign('list', $list);
  357. $this->assign('userInfo', $userInfo);
  358. //dump($list);
  359. return $this->view->fetch();
  360. }*/
  361. /**
  362. * 读者管理
  363. * @param $ids
  364. */
  365. public function users($ids)
  366. {
  367. header('location:/admin/auth/user/index?channel=' . $ids);
  368. }
  369. /**
  370. * 更多
  371. */
  372. public function more($ids)
  373. {
  374. $this->assign('ids', $ids);
  375. $this->assign('user', '/admin/auth/channel/users/ids/' . $ids);
  376. $this->assign('order', '/admin/auth/channel/orders/ids/' . $ids);
  377. return $this->view->fetch();
  378. }
  379. /**
  380. * 渠道商数据统计
  381. */
  382. public function collect($ids)
  383. {
  384. $ids = intval($ids);
  385. $userInfo = model('Admin')->get($ids);
  386. $users = model('UserCollect')->where(['admin_id' => $ids, 'type' => 3])->find(); //用户统计
  387. $collect = model('OrdersCollect')->where(['admin_id' => $ids, 'type' => 3])->find(); //订单统计
  388. $this->assign('userInfo', $userInfo);
  389. $this->assign('users', $users);
  390. $this->assign('collect', $collect);
  391. return $this->view->fetch();
  392. }
  393. /**
  394. * 添加
  395. */
  396. public function add()
  397. {
  398. if ($this->request->isPost()) {
  399. $params = $this->request->post("row/a");
  400. $extends = $this->request->post("extend/a");
  401. $config = $this->request->post("config/a");
  402. $adminConfig = $this->request->post("admin_config/a");
  403. if ($params) {
  404. if (empty($config)) {
  405. $this->error('平台相关信息不能为空');
  406. }
  407. //设置AdminConfig参数
  408. $adminData = [];
  409. $adminData['unpay_tip'] = '1';
  410. $adminData['read_tip'] = '1';
  411. $adminData['book_guide_chapter_idx'] = config('site.book_guide_chapter_idx');
  412. $adminData['createtime'] = time();
  413. $adminData['updatetime'] = time();
  414. if ($config) {
  415. //推送开关 add at 2018-08-31 19:46:08
  416. $adminData['subscribe_tip'] = $config['subscribe_tip'] ?? '0';
  417. $adminData['sign_tip'] = $config['sign_tip'] ?? '0';
  418. $adminData['continueread_tip'] = $config['continueread_tip'] ?? '0';
  419. $adminData['unpay_tip'] = $config['unpay_tip'] ?? '0';
  420. $adminData['read_tip'] = $config['read_tip'] ?? '0';
  421. $adminData['update_tip'] = $config['update_tip'] ?? '0';
  422. $adminData['timing_tip'] = $config['timing_tip'] ?? '0';
  423. $adminData['ref_out_push'] = $config['ref_out_push'] ?? '0';
  424. $adminData['wx_ad_state'] = $config['wx_ad_state'];
  425. $adminData['is_account_self'] = $config['is_account_self'];
  426. $adminData['custom_goods'] = $config['custom_goods'];
  427. $adminData['first_recharge_imgtext_post'] = $config['first_recharge_imgtext_post'] ?? '0';
  428. $adminData['order_unrecharge_post_time'] = (int)$config['order_unrecharge_post_time'] ?? 0;
  429. //检查平台信息
  430. $this->checkedPlatform($config, $adminData);
  431. }
  432. //创建Admin
  433. if(! AdminService::instance()->checkPassword($params['password'])){
  434. $this->error(AdminService::instance()->getPasswordRule());
  435. }
  436. $params['salt'] = Random::alnum();
  437. $params['password'] = md5(md5($params['password']) . $params['salt']);
  438. $params['avatar'] = asset('/img/avatar.png'); //设置新管理员默认头像。
  439. $result = $this->modelAdmin->validate('Admin.add')->save($params);
  440. if ($result === false) {
  441. $this->error($this->modelAdmin->getError());
  442. }
  443. //添加关联权限组
  444. model('AuthGroupAccess')->insert(['uid' => $this->modelAdmin->id, 'group_id' => 3]);
  445. //创建AdminExtend
  446. if ($extends) {
  447. $extends['admin_id'] = $this->modelAdmin->id;
  448. $extends['create_by'] = Session::get('admin')['id'];
  449. //过滤一下收款方式的空格
  450. $extends['card_holder'] = rm_space($extends['card_holder']);
  451. $extends['card_num'] = rm_space($extends['card_num']);
  452. $extends['card_bank'] = rm_space($extends['card_bank']);
  453. $res = model('Admin_extend')->save($extends);
  454. $redis = Redis::instance();
  455. $key = 'AE:' . $this->modelAdmin->id;
  456. $redis->del($key);
  457. if ($res === false) {
  458. $this->error(model('Admin_extend')->getError());
  459. }
  460. }
  461. //创建AdminConfig
  462. $adminData['admin_id'] = $this->modelAdmin->id;
  463. if ($adminConfig['palmpay_sub_account_ids']) {
  464. $palmpay_sub_account_arr = explode(',', $adminConfig['palmpay_sub_account_ids']);
  465. if (!empty($palmpay_sub_account_arr)) {
  466. foreach ($palmpay_sub_account_arr as $val) {
  467. list($company_id, $palmpay_sub_account_id) = explode('|', $val);
  468. $palmpay_sub_account_id_arr[] = array(
  469. 'company_id' => $company_id,
  470. 'palmpay_sub_account_id' => $palmpay_sub_account_id
  471. );
  472. }
  473. }
  474. }
  475. $adminData['palmpay_sub_account_ids'] = empty($palmpay_sub_account_id_arr) ? '[]' : json_encode($palmpay_sub_account_id_arr);
  476. $resultSql = model('AdminConfig')->insert($adminData);
  477. if ($resultSql === false) {
  478. $this->error(model('AdminConfig')->getError());
  479. }
  480. $this->success();
  481. }
  482. $this->error();
  483. }
  484. //虚假域名
  485. $fake_ophost_list = $fake_menuophost_list = model("VirtualDomain")->getFakeophostList();
  486. $this->assign('wxpay_list', model("wxpay")->getListFormatByPlatformId());
  487. $this->assign('platforms', model('Platform')->getPlatformList());
  488. $this->assign('fake_menuophost_list', $fake_menuophost_list);
  489. $this->assign('fake_ophost_list', $fake_ophost_list);
  490. return $this->view->fetch();
  491. }
  492. /**
  493. * 检查平台信息
  494. * @param $config
  495. * @param $admin_data
  496. */
  497. private function checkedPlatform(&$config, &$admin_data)
  498. {
  499. $admin_data = array_merge($config, $admin_data);
  500. if (!$ophost_id = $config['ophost_id'] ?? null) {
  501. $this->error('业务域名不能为空');
  502. }
  503. if (!$wxpay_id = $config['wxpay_id'] ?? null) {
  504. $this->error('支付域名不能为空');
  505. }
  506. $ophost = model('Ophost')->where('id', $ophost_id)->column('platform_id,host');
  507. if (!$menuophost_id = $config['menuophost_id'] ?? null) {
  508. $this->error('菜单域名不能为空');
  509. }
  510. if (!$menuwxpay_id = $config['menuwxpay_id'] ?? null) {
  511. $this->error('菜单支付域名不能为空');
  512. }
  513. $menu_ophost = model('Ophost')->where('id', $menuophost_id)->column('platform_id,host');
  514. //添加参数
  515. $admin_data['platform_id'] = key($ophost);
  516. $admin_data['menu_platform_id'] = key($menu_ophost);
  517. $admin_data['ophost_id'] = $ophost_id;
  518. $admin_data['wxpay_id'] = $wxpay_id;
  519. $admin_data['menuophost_id'] = $menuophost_id;
  520. $admin_data['menuwxpay_id'] = $menuwxpay_id;
  521. if ($config['platform_list']) {
  522. if (!in_array($admin_data['platform_id'], $config['platform_list'])) {
  523. array_push($config['platform_list'], $admin_data['platform_id']);
  524. }
  525. } else {
  526. array_push($config['platform_list'], $admin_data['platform_id']);
  527. }
  528. $admin_data['platform_list'] = trim(implode(',', $config['platform_list']), ',');
  529. //下单未充值时间间隔验证
  530. if(isset($admin_data['order_unrecharge_post_time'])){
  531. if($admin_data['order_unrecharge_post_time']<=0){
  532. $admin_data['order_unrecharge_post_time'] = 1;
  533. }
  534. if($admin_data['order_unrecharge_post_time']>31){
  535. $admin_data['order_unrecharge_post_time'] = 30;
  536. }
  537. }
  538. }
  539. /**
  540. * 编辑
  541. */
  542. public function edit($ids = null)
  543. {
  544. $row = $this->modelAdmin->get(['id' => $ids]);
  545. if (!$row) {
  546. $this->error(__('No Results were found'));
  547. }
  548. if ($this->request->isPost()) {
  549. //编辑处理
  550. $message = null;
  551. $params = $this->request->post("row/a");
  552. $extends = $this->request->post("extend/a");
  553. $config = $this->request->post("config/a");
  554. $adminConfig = $this->request->post("admin_config/a");
  555. //修改Admin
  556. if ($params) {
  557. if ($params['password']) {
  558. if(! AdminService::instance()->checkPassword($params['password'])){
  559. $this->error(AdminService::instance()->getPasswordRule());
  560. }
  561. $params['salt'] = Random::alnum();
  562. $params['password'] = md5(md5($params['password']) . $params['salt']);
  563. $params['token'] = Random::uuid();
  564. AdminService::instance()->updateAdminSessionStatus($ids);
  565. } else {
  566. unset($params['password'], $params['salt']);
  567. }
  568. //自定义商品是否开启为否,则删除渠道商自定义商品信息,数据库+redis
  569. if ($adminConfig['custom_goods'] == 0) {
  570. GoodsService::instance()->delChannelGoodsList($row->id);
  571. }
  572. //这里需要针对username和email做唯一验证
  573. $adminValidate = \think\Loader::validate('Admin');
  574. $adminValidate->rule([
  575. 'username' => 'require|max:50|unique:admin,username,' . $row->id,
  576. 'email' => 'email|unique:admin,email,' . $row->id
  577. ]);
  578. $result = $row->validate('Admin.edit')->save($params);
  579. if ($result === false) {
  580. $this->error($row->getError());
  581. }
  582. }
  583. if ($adminConfig['palmpay_sub_account_ids']) {
  584. $palmpay_sub_account_arr = explode(',', $adminConfig['palmpay_sub_account_ids']);
  585. if (!empty($palmpay_sub_account_arr)) {
  586. foreach ($palmpay_sub_account_arr as $val) {
  587. list($company_id, $palmpay_sub_account_id) = explode('|', $val);
  588. $palmpay_sub_account_id_arr[] = array(
  589. 'company_id' => $company_id,
  590. 'palmpay_sub_account_id' => $palmpay_sub_account_id
  591. );
  592. }
  593. }
  594. }
  595. $adminConfig['palmpay_sub_account_ids'] = empty($palmpay_sub_account_id_arr) ? '[]' : json_encode($palmpay_sub_account_id_arr);
  596. //修改AdminExtend
  597. if ($extends) {
  598. //仅超管可以编辑开户人 add at 2018-08-31 15:34:09
  599. if (isset($extends['create_by']) && in_array($this->group,
  600. [1]) && $extends['create_by'] != $extends['create_by_old']) {
  601. //检查开户人状态
  602. $admin = model('Admin')->getById($extends['create_by']);
  603. if (!$admin || $admin['status'] != 'normal') {
  604. $this->error(__('当前开户人不存在,请重新修改开户人'));
  605. }
  606. }
  607. //过滤一下收款方式的空格
  608. $extends['card_holder'] = rm_space($extends['card_holder']);
  609. $extends['card_num'] = rm_space($extends['card_num']);
  610. $extends['card_bank'] = rm_space($extends['card_bank']);
  611. $res = model('Admin_extend')->allowField(true)->save($extends, ['admin_id' => $row->id]);
  612. $redis = Redis::instance();
  613. $key = 'AE:' . $row->id;
  614. $redis->del($key);
  615. if ($res === false) {
  616. $this->error(model('Admin_extend')->getError());
  617. }
  618. }
  619. //推送开关检查 && AdminConfig修改
  620. $adminConfig['subscribe_tip'] = $adminConfig['subscribe_tip'] ?? '0';
  621. $adminConfig['sign_tip'] = $adminConfig['sign_tip'] ?? '0';
  622. $adminConfig['continueread_tip'] = $adminConfig['continueread_tip'] ?? '0';
  623. $adminConfig['unpay_tip'] = $adminConfig['unpay_tip'] ?? '0';
  624. $adminConfig['read_tip'] = $adminConfig['read_tip'] ?? '0';
  625. $adminConfig['update_tip'] = $adminConfig['update_tip'] ?? '0';
  626. $adminConfig['search_tip'] = $adminConfig['search_tip'] ?? '0';
  627. $adminConfig['timing_tip'] = $adminConfig['timing_tip'] ?? '0';
  628. $adminConfig['first_recharge_imgtext_post'] = $adminConfig['first_recharge_imgtext_post'] ?? '0';
  629. $adminConfig['order_unrecharge_post_time'] = (int)$adminConfig['order_unrecharge_post_time'] ?? 0;
  630. //检查平台相关信息
  631. if ($config) {
  632. //倒粉域名&短链域名 开关 -- 检测是否有关联的倒粉域名或者短链域名
  633. if (isset($config['guide_domain']) && $config['guide_domain']) {
  634. if (!model('ShortRelation')->checkChannelRelation($row['id']) || !model('GuideRelation')->checkChannelRelation($row['id'])) {
  635. $this->error('请先为渠道指派导粉域名和短链域名');
  636. }
  637. }
  638. //检查平台信息&设置要更新的参数
  639. $configCache = model('AdminConfig')->getAdminInfoAll($row['id']);
  640. $this->checkedPlatform($config, $adminConfig);
  641. //检查是否要刷新菜单
  642. if ($adminConfig['platform_id'] != $configCache['platform_id'] || //平台不一致
  643. $adminConfig['ophost_id'] != $configCache['ophost_id'] || //业务域名不一致
  644. $adminConfig['menuophost_id'] != $configCache['menuophost_id'] //菜单域名不一致
  645. ) {
  646. //推送微信菜单
  647. list($status, $message) = WeChatMenu::replaceChannelMenuById($row['id'],
  648. $adminConfig['menuophost_id'], $configCache['is_qrcode_menu']);
  649. if ($status) {
  650. $adminConfig['wx_menu'] = $message;
  651. $message = null;
  652. }
  653. }
  654. }
  655. $res = model('AdminConfig')->save($adminConfig, ['admin_id' => $row->id]);
  656. if ($res === false) {
  657. $this->error(model('AdminConfig')->getError());
  658. }
  659. //清除redis缓存
  660. model('AdminConfig')->delAdminInfoAllCache($row['id']);
  661. if ($message) {
  662. $this->success($message);
  663. }
  664. $this->success();
  665. }
  666. //展示处理
  667. $adminConfig = model('AdminConfig')->where('admin_id', $row['id'])->find();
  668. //检查渠道平台列表
  669. if (!$adminConfig['platform_list']) {
  670. $this->error('没有平台信息');
  671. }
  672. //AdminExtend
  673. $extends = model('Admin_extend')->where('admin_id', $row['id'])->find();
  674. //获取全部平台列表
  675. $platform_lists = model('Platform')->getPlatformList();
  676. //检查渠道是否授权全部平台
  677. $channel_is_auth = model('Ptoken')->checkChannelIsAuth($adminConfig['platform_list'], $row['id']);
  678. //获取业务域名列表与支付域名列表
  679. if ($channel_is_auth) {
  680. $channelAuthPlatformId = model('Platform')->getChannelHaveAuthorizedPlatformId($adminConfig['admin_id'],
  681. $adminConfig['platform_list']);
  682. } else {
  683. $channelAuthPlatformId = $adminConfig['platform_list'];
  684. }
  685. $ophost_list = model("ophost")->getListFormatByPlatformId($channelAuthPlatformId);
  686. $wxpay_list = model("wxpay")->getListFormatByPlatformId();
  687. $menu_wxpay_list = model("wxpay")->getListFormatByPlatformId();
  688. //虚假域名
  689. $fake_ophost_list = $fake_menuophost_list = model("VirtualDomain")->getFakeophostList();
  690. //授权信息-重置
  691. if (in_array($this->group,
  692. [
  693. AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN,
  694. AdminConstants::ADMIN_CONFIG_SUBSCRIBE_METHOD_GUID,
  695. AdminConstants::ADMIN_GROUP_ID_TECHNICAL_SUPPORT,
  696. AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER
  697. ])) {
  698. $auth_platform_list = model("platform")->whereIn('id', $adminConfig['platform_list'])->select();
  699. foreach ($auth_platform_list as $key => $val) {
  700. $auth_platform_list[$key]['is_auth'] = false;
  701. if (model('Ptoken')->getToken($val['id'], $row['id'])) {
  702. $auth_platform_list[$key]['is_auth'] = true;
  703. }
  704. }
  705. $sync = model('SyncUser')->where(['admin_id' => $row['id'], 'appid' => $adminConfig['appid']])->find();
  706. $this->assign('sync', $sync);
  707. $this->assign('auth_json', $adminConfig['json']);
  708. $this->assign('auth_platform_list', $auth_platform_list);
  709. }
  710. $canEditDomain = in_array($this->group, [
  711. AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN,
  712. AdminConstants::ADMIN_GROUP_ID_TECHNICAL_SUPPORT,
  713. AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER
  714. ]);
  715. $this->assign('can_edit_domain', $canEditDomain);
  716. //渠道商开户人列表
  717. $create_list = [];
  718. if (in_array($this->group, [
  719. AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN,
  720. AdminConstants::ADMIN_GROUP_ID_TECHNICAL_SUPPORT,
  721. AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER
  722. ])) {
  723. $create_list = model('AuthGroupAccess')
  724. ->alias('ga')
  725. ->field('a.*')
  726. ->join('admin a', 'a.id=ga.uid', 'LEFT')
  727. ->where(['ga.group_id' => '2'])
  728. ->select();
  729. }
  730. //关联短链域名数量
  731. $row['short_relation_count'] = model('ShortRelation')->getRelationCount($row['id']);
  732. //关联导粉域名数量
  733. $row['guide_relation_count'] = model('GuideRelation')->getRelationCount($row['id']);
  734. if ($adminConfig['palmpay_sub_account_ids']) {
  735. $json = json_decode($adminConfig['palmpay_sub_account_ids'], true);
  736. $adminConfig['palmpay_sub_account_ids'] = implode(',', array_column($json, 'palmpay_sub_account_id'));
  737. }
  738. $this->assign("row", $row);
  739. $this->assign('platforms', $platform_lists);
  740. $this->assign('plist', explode(',', $adminConfig['platform_list']));
  741. $this->assign('adminConfigs', $adminConfig);
  742. $this->assign('platform_id', $adminConfig['platform_id']);
  743. $this->assign('ophost_list', $ophost_list);
  744. $this->assign('wxpay_list', $wxpay_list);
  745. $this->assign('menu_wxpay_list', $menu_wxpay_list);
  746. $this->assign('fake_menuophost_list', $fake_menuophost_list);
  747. $this->assign('fake_ophost_list', $fake_ophost_list);
  748. $this->assign('extends', $extends);
  749. $this->assign('create_list', $create_list);
  750. $this->assignconfig('admin_id', $adminConfig['admin_id']);
  751. $this->assignconfig('channel_is_auth', $channel_is_auth);
  752. return $this->view->fetch();
  753. }
  754. /**
  755. * 重置服务号
  756. */
  757. public function re()
  758. {
  759. $admin_id = $this->request->param('admin_id');
  760. /**
  761. * 清除旧配置
  762. */
  763. if (!$adminConfig = model('AdminConfig')->get(['admin_id' => $admin_id])) {
  764. $this->error('获取信息失败');
  765. }
  766. $adminInfo = collection($adminConfig)->toArray();
  767. $token = collection(model('Ptoken')->where(['admin_id' => $admin_id])->select())->toArray();
  768. Log::info('Reset WeChat Config Data:' . var_export(['admin_config' => $adminInfo, 'ptoken' => $token], true));
  769. $data = [
  770. 'secret' => null,
  771. 'appid' => null,
  772. 'json' => null,
  773. 'txt' => null,
  774. 'refresh_token' => null,
  775. 'wx_menu' => null,
  776. 'is_auth' => 0,
  777. 'wx_ad_source_id' => 0,
  778. 'wx_ad_state' => 0,
  779. 'is_account_self' => '0',
  780. 'custom_goods' => '0',
  781. 'qrcode_image' => null
  782. ];
  783. if (false === model('Ptoken')->where(['admin_id' => $admin_id])->delete()) {
  784. $this->error('删除Token数据失败');
  785. }
  786. if (false === $adminConfig->save($data)) {
  787. $this->error('清除授权配置信息失败');
  788. }
  789. $redis = Redis::instance();
  790. model('AdminConfig')->delAdminInfoAllCache($admin_id);
  791. $redis->del('AA:' . $adminConfig['appid']);
  792. //删除渠道商自定义商品信息,数据库+redis
  793. GoodsService::instance()->delChannelGoodsList($admin_id);
  794. $this->success('重置成功');
  795. }
  796. /**
  797. * 同步微信用户
  798. */
  799. public function sync()
  800. {
  801. $admin_id = $this->request->param('admin_id');
  802. if (!$admin_id) {
  803. $this->error('获取用户ID错误');
  804. }
  805. if (!$adminConfig = model('AdminConfig')->getAdminInfoAll($admin_id)) {
  806. $this->error('获取渠道信息错误');
  807. }
  808. $mq = new Rabbitmq();
  809. $mq->send(['channel_id' => $admin_id, 'appid' => $adminConfig['appid']], self::SYNC_MQ_QUEUE_NAME,
  810. self::SYNC_MQ_EXCHANGE_NAME);
  811. Log::info('syncUser->Queue: Channel_id:' . $admin_id . ' AppID:' . $adminConfig['appid'] . ' 添加微信用户同步任务');
  812. $this->success('微信用户同步任务添加成功');
  813. }
  814. /**
  815. * 删除
  816. */
  817. public function del($ids = "")
  818. {
  819. if ($ids) {
  820. // 避免越权删除管理员
  821. $childrenGroupIds = $this->childrenGroupIds;
  822. $adminList = $this->modelAdmin->where('id', 'in', $ids)->where('id', 'in',
  823. function ($query) use ($childrenGroupIds) {
  824. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  825. })->select();
  826. if ($adminList) {
  827. $deleteIds = [];
  828. foreach ($adminList as $k => $v) {
  829. $deleteIds[] = $v->id;
  830. }
  831. $deleteIds = array_diff($deleteIds, [$this->auth->id]);
  832. if ($deleteIds) {
  833. $this->modelAdmin->where('id', 'in', $deleteIds)->update(['status' => 'hidden']);
  834. //model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  835. $this->success();
  836. }
  837. }
  838. }
  839. $this->error();
  840. }
  841. /**
  842. * 批量更新
  843. * @internal
  844. */
  845. public function multi($ids = "")
  846. {
  847. // 管理员禁止批量操作
  848. $this->error();
  849. }
  850. /**
  851. * 检查平台是否授权
  852. */
  853. public function checkplatformauth()
  854. {
  855. $platform_id = $this->request->get("platform_id");
  856. $channel_id = $this->request->get("channel_id");
  857. if (!is_numeric($platform_id) || !is_numeric($channel_id)) {
  858. $this->error('没有平台ID OR 渠道ID');
  859. }
  860. if ($sourcePtoken = model('Ptoken')->whereIn('platform_id', $platform_id)->where('admin_id',
  861. $channel_id)->select()) {
  862. $this->success();
  863. } else {
  864. $this->error('没有授权');
  865. }
  866. }
  867. /**
  868. * 获取业务域名
  869. * @return \think\response\Json
  870. */
  871. public function ajaxgetophostlistbyplatformid()
  872. {
  873. $platform_id = $this->request->get("platform_id");
  874. if (!is_numeric($platform_id)) {
  875. $res['code'] = 0;
  876. $res['msg'] = "参数错误";
  877. return json($res);
  878. }
  879. $res['code'] = 1;
  880. $res['data'] = model("ophost")->getListFormatByPlatformId($platform_id);
  881. return json($res);
  882. }
  883. /**
  884. * 获取支付域名
  885. * @return \think\response\Json
  886. */
  887. public function ajaxgetwxpaylistbyplatformid()
  888. {
  889. $platform_id = $this->request->get("platform_id");
  890. $ophost_id = $this->request->get("ophost_id");
  891. if (!is_numeric($platform_id) || !is_numeric($ophost_id)) {
  892. $res['code'] = 0;
  893. $res['msg'] = "参数错误";
  894. return json($res);
  895. }
  896. $res['code'] = 1;
  897. $res['data'] = model("wxpay")->getListFormatByPlatformId();
  898. return json($res);
  899. }
  900. public function select()
  901. {
  902. $isIndex = 0;
  903. $operate = $this->request->param('operate') ?? 'add';
  904. //不用的位置调用,选择不用的type
  905. $type = $this->request->param('type',1);
  906. //需要过滤渠道 的资源id,
  907. $sourceId = $this->request->param('source_id',0);
  908. if ($operate) {
  909. if ($operate == 'add') {
  910. //添加
  911. $isIndex = 1;
  912. } elseif ($operate == 'remove') {
  913. //移除
  914. $isIndex = 2;
  915. }
  916. }
  917. $this->assignconfig('source_id',$sourceId);
  918. $this->assignconfig('operate', $operate);
  919. $this->assign('is_index', $isIndex);
  920. $this->assignconfig('is_index', $isIndex);
  921. $this->assignconfig('type', $type);
  922. $this->assignconfig('group_ids', $this->request->param('group_ids'));
  923. if ($this->request->isAjax())
  924. {
  925. $groupIds = [3,7];
  926. if ($this->request->param('group_ids') > 0) {
  927. $groupIds = explode(',', $this->request->param('group_ids'));
  928. }
  929. $authGroupList = model('AuthGroupAccess')->where('group_id', 'in',$groupIds)
  930. ->field('uid,group_id')
  931. ->select();
  932. $groupName = AuthGroup::where('id', 'in', $groupIds)
  933. ->column('id,name');
  934. $adminGroupName = [];
  935. $allIds = [];
  936. foreach ($authGroupList as $k => $v)
  937. {
  938. $allIds[] = $v['uid'];
  939. if (isset($groupName[$v['group_id']]))
  940. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  941. }
  942. //获取添加的channelIds
  943. $oldIds = [];
  944. switch ($type){
  945. case 1:
  946. $oldChannels = GoodsChannelsService::instance()->getChannelsByGoods($sourceId);
  947. $oldIds = $oldChannels != false ? array_column($oldChannels,'channel_id') : [];
  948. break;
  949. case 2:
  950. $oldChannels =PayGuideService::instance()->getChannelByGuide($sourceId);
  951. $oldIds = $oldChannels != false ? array_column($oldChannels,'channel_id') : [];
  952. break;
  953. case 3:
  954. $oldChannels = model('admin/ConfigLightRule')->where('id', $sourceId)->column('channel_ids');
  955. $oldIds = $oldChannels != false ? explode(',', $oldChannels[0]) : [];
  956. break;
  957. default:
  958. break;
  959. }
  960. $admin_where_not_in = $admin_where_in = [];
  961. //如果是 add 取差集,如果是 remove 显示老用户
  962. // $allIds = $operate == 'add' ? array_diff($allIds,$oldIds) : $oldIds;
  963. if($operate == 'add'){
  964. $admin_where_not_in['admin.id'] = ['NOT IN', $oldIds];
  965. }else{
  966. $admin_where_in['admin.id'] = ['IN', $oldIds];
  967. }
  968. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  969. $total = $this->modelAdmin
  970. ->join("auth_group_access", "admin.id = auth_group_access.uid", "inner")
  971. ->where($where)
  972. ->where($admin_where_not_in)
  973. ->where($admin_where_in)
  974. ->where('auth_group_access.group_id', 'in',$groupIds)
  975. ->order($sort, $order)
  976. ->count();
  977. $list = $this->modelAdmin
  978. ->join("auth_group_access", "admin.id = auth_group_access.uid", "inner")
  979. ->where($where)
  980. ->where($admin_where_not_in)
  981. ->where($admin_where_in)
  982. ->where('auth_group_access.group_id', 'in',$groupIds)
  983. ->field(['password', 'salt', 'token'], true)
  984. ->order($sort, $order)
  985. ->limit($offset, $limit)
  986. ->select();
  987. foreach ($list as $k => &$v)
  988. {
  989. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  990. $v['groups'] = implode(',', array_keys($groups));
  991. $v['groups_text'] = implode(',', array_values($groups));
  992. }
  993. unset($v);
  994. $result = array("total" => $total, "rows" => $list);
  995. return json($result);
  996. }
  997. return $this->view->fetch();
  998. }
  999. }