Vipmenu.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use app\common\controller\Backend;
  4. use app\common\library\WeChatObject;
  5. use app\common\model\AdminConfig;
  6. use app\common\model\WechatResponse;
  7. use app\common\utility\DiyQRCode;
  8. use app\common\utility\WeChatMenu;
  9. use app\main\service\WxResponseService;
  10. use EasyWeChat\Factory;
  11. use app\common\library\Redis;
  12. use Symfony\Component\Cache\Simple\RedisCache;
  13. use think\Log;
  14. /**
  15. * 菜单管理
  16. *
  17. * @icon fa fa-list-alt
  18. */
  19. class Vipmenu extends Backend
  20. {
  21. protected $noNeedRight = ['reset', 'ajaxmofiywxmenu', 'usergroup', 'saveto','savetoall'];
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. $this->model = model('VipMenu');
  26. parent::_initialize();
  27. }
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. $responselist = array();
  34. $all = WechatResponse::all(['admin_id' => $this->auth->id]);
  35. foreach ($all as $k => $v)
  36. {
  37. $responselist[$v['eventkey']] = $v['title'];
  38. }
  39. $this->view->assign('responselist', $responselist);
  40. $vrow = $this->model->get(['admin_id' => $this->auth->id]);
  41. $menu = $vrow->wx_menu ?? null;
  42. $menu = str_replace('\"','"',$menu);
  43. $menu = str_replace('"[','[',$menu);
  44. $menu = str_replace(']"',']',$menu);
  45. if (!$menu) {
  46. $menu = json_decode(config('site.wx_menu'),true);
  47. }
  48. if(!is_array($menu)){
  49. $menu=json_decode($menu,true);
  50. }
  51. $this->view->assign('menu', $menu);
  52. $sync_record = model('VipMenuSyncRecord')
  53. ->where('admin_id','eq',$this->auth->id)
  54. ->order('createtime','desc')
  55. ->limit(1)
  56. ->select();
  57. $sync_record = collection($sync_record)->toArray();
  58. if(empty($sync_record)){
  59. $this->view->assign('isall', 1);
  60. $this->view->assign('sync_list', []);
  61. $this->view->assign('group_ids_str', '');
  62. }else{
  63. if($sync_record[0]['sync_type'] ==1){
  64. $this->view->assign('isall', 1);
  65. $this->view->assign('sync_list', []);
  66. $this->view->assign('group_ids_str', '');
  67. }else{
  68. $sync_list = model('VipGroup')->where('id','in',$sync_record[0]['group_ids'])->select();
  69. $sync_list = collection($sync_list)->toArray();
  70. foreach($sync_list as &$kp){
  71. $kp['createtime'] = date('Y-m-d H:i:s',$kp['createtime']);
  72. $kp['updatetime'] = date('Y-m-d H:i:s',$kp['updatetime']);
  73. }
  74. $this->view->assign('isall', 2);
  75. $this->view->assign('sync_list', $sync_list);
  76. $this->view->assign('group_ids_str', $sync_record[0]['group_ids']);
  77. $this->assignconfig('group_ids_js', explode(',',$sync_record[0]['group_ids']));
  78. $this->assignconfig('gzhHtmlArr', $sync_list);
  79. }
  80. }
  81. return $this->view->fetch();
  82. }
  83. /**
  84. * 保存
  85. */
  86. public function edit($ids = NULL)
  87. {
  88. $menu = $this->request->post("menu");
  89. if(empty($menu)){
  90. $this->error('菜单不能为空!');
  91. }
  92. $group_type = $this->request->post("group_type");
  93. $group_ids = $this->request->post("group_ids");
  94. if($group_type==2 && empty($group_ids)){
  95. $this->error('请选择分组!');
  96. }
  97. if($group_type == 1){
  98. // $group_id_arr = model('VipGroup')->where('admin_id','eq',$this->auth->id)->column('id');
  99. // if(count($group_id_arr) == 1){
  100. // $group_id_str = $group_id_arr[0];
  101. // }elseif (empty($group_id_arr)){
  102. // $this->error('您账号下没有分组!');
  103. // }else{
  104. // $group_id_str = implode(',',$group_id_arr);
  105. // }
  106. $group_id_str = '';
  107. }else{
  108. $group_id_str = $group_ids;
  109. }
  110. model('VipMenuSyncRecord')->insert(['sync_type'=>$group_type,'group_ids'=>$group_id_str,'admin_id'=>$this->auth->id,'createtime'=>time()]);
  111. $this->_hanlderSave($menu);
  112. $this->success();
  113. }
  114. /**
  115. * 保存并发布
  116. */
  117. public function saveto(){
  118. $menu = $this->request->post("menu");
  119. if(empty($menu)){
  120. $this->error('菜单不能为空!');
  121. }
  122. $group_type = $this->request->post("group_type");
  123. $group_ids = $this->request->post("group_ids");
  124. if($group_type==2 && empty($group_ids)){
  125. $this->error('请选择分组!');
  126. }
  127. if($group_type == 1){
  128. $where['admin_id_master'] = ['eq',$this->auth->id];
  129. $where['flag'] = ['eq',1];
  130. $channel_ids = model('VipAdminBind')->where($where)->column('admin_id_slave');
  131. }else{
  132. if(!empty($group_ids)){
  133. $tmp_ids = explode(',',$group_ids);
  134. if(count($tmp_ids) ==1){
  135. $where['vg.id'] = ['eq',$tmp_ids[0]];
  136. }else{
  137. $where['vg.id'] = ['in',$group_ids];
  138. }
  139. $channel_ids = model('VipGroup')->alias('vg')
  140. ->join('vip_group_info vgi','vg.id=vgi.group_id','left')
  141. ->where($where)
  142. ->column("vgi.channel_id");
  143. }else{
  144. $channel_ids = [];
  145. }
  146. }
  147. if(empty($channel_ids)){
  148. $this->error('账号下不存在任何渠道');
  149. }
  150. $this->_hanlderSave(json_decode($menu,true));
  151. $time = time();
  152. if(count($channel_ids)==1){
  153. $code = $this->sync($menu,$channel_ids[0],$time,$group_type,$group_ids);
  154. if($code['code']==1){
  155. $this->success();
  156. }else{
  157. $this->error($code['msg']);
  158. }
  159. }else{
  160. $code = $this->syncall($menu,$channel_ids,$time,$group_type,$group_ids);
  161. if($code == 1){
  162. $this->success();
  163. }elseif($code == 2){
  164. $this->error('菜单参数配置不完整,请检查是否有空菜单!');
  165. }elseif($code == 3){
  166. $this->error('菜单同步失败!');
  167. }
  168. }
  169. }
  170. /**
  171. * 同步
  172. */
  173. public function sync($menu,$channel_id,$time,$group_type,$group_ids)
  174. {
  175. $hasVipError = 0;
  176. $hasError = false;
  177. $adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id);
  178. if($adminConfig['is_qrcode_menu']==1){
  179. return ['code'=>6,'msg'=>'该账户已设置为二维码菜单,请修改为链接菜单后重试'];
  180. }
  181. if(empty($menu)){
  182. return ['code'=>10,'msg'=>'菜单不能为空'];
  183. }
  184. $menu = json_decode(str_replace('cpsurl', '', $menu), true);
  185. foreach($menu as $key => $val) {
  186. if (isset($val['url'])) {
  187. $menu[$key]['url'] = getCurrentDomain($channel_id, $val['url'],[],true);
  188. if(empty($adminConfig['vip_state']) && strpos($val['url'],'recharge/vip')!==false){
  189. $hasVipError++;
  190. }
  191. }
  192. if (isset($val['sub_button']) && !empty($val['sub_button'])) {
  193. foreach ($val['sub_button'] as $sub_k => $sub_v) {
  194. if (isset($sub_v['url'])) {
  195. $menu[$key]['sub_button'][$sub_k]['url'] = getCurrentDomain($channel_id, $sub_v['url'],[],true);
  196. if(empty($adminConfig['vip_state']) && strpos($sub_v['url'],'recharge/vip')!==false){
  197. $hasVipError++;
  198. }
  199. }
  200. }
  201. }
  202. }
  203. /**
  204. * 校验menu数据格式
  205. */
  206. foreach ($menu as $k => $v){
  207. $hasError = false;
  208. /**
  209. * 校验menu数据格式
  210. */
  211. foreach ($menu as $k => $v){
  212. if (isset($v['sub_button'])){
  213. foreach ($v['sub_button'] as $m => $n){
  214. if (isset($n['key']) && !$n['key']){
  215. $hasError = true;
  216. break 2;
  217. }
  218. }
  219. }else if (isset($v['key']) && !$v['key']){
  220. $hasError = true;
  221. break;
  222. }
  223. }
  224. }
  225. if (!$hasError && $hasVipError == 0) {
  226. $response = [];
  227. try{
  228. model('AdminConfig')->where('admin_id',$channel_id)->update(['wx_menu'=>json_encode($menu)]);
  229. $adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id);
  230. $wechat = new WeChatObject($adminConfig);
  231. $officialAccount = $wechat->getOfficialAccount();
  232. if(is_null($officialAccount)){
  233. return ['code'=>2,'msg'=>'分组包含未授权账户,请核实后重新提交'];
  234. }
  235. $officialAccount->menu->delete(); //删除全部菜单 主要是掌中云个性化菜单
  236. $ret = $officialAccount->menu->create($menu);
  237. if($ret && $ret['errcode'] == 0){
  238. $menu = WeChatMenu::rechargeMenuFilter($menu);
  239. if(!empty($menu)){
  240. $ret = $officialAccount->menu->create($menu,['client_platform_type' => 1]);
  241. }
  242. }else{
  243. return ['code'=>4,'msg'=>$response['errmsg']];
  244. }
  245. $response = $ret;
  246. }catch (\Exception $e){
  247. return ['code'=>3,'msg'=>$e->getMessage()];
  248. }
  249. if($response && isset($response['errcode']) && $response['errcode'] == 0){
  250. $this->success();
  251. }elseif($response>0){
  252. $this->success();
  253. }else{
  254. return ['code'=>4,'msg'=>$response['errmsg']];
  255. }
  256. }elseif(!$hasError && $hasVipError != 0){
  257. return ['code'=>5,'msg'=>'该账户未开通VIP充值管理菜单,请核对后重试'];
  258. }else{
  259. $this->error('菜单参数配置不完整,请检查是否有空菜单!');
  260. }
  261. return ['code'=>1];
  262. }
  263. /**
  264. * 同步全部渠道
  265. */
  266. public function syncall($menu,$channel_ids,$time,$group_type,$group_ids)
  267. {
  268. $success = 0;
  269. $error = 0;
  270. if(empty($menu)){
  271. $error++;
  272. }
  273. $origin_menu = json_decode(str_replace('cpsurl', '', $menu), true);
  274. foreach($channel_ids as $channel_id){
  275. $menu = $origin_menu;
  276. $hasVipError = 0;
  277. $hasError = false;
  278. $adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id);
  279. //是二维码菜单的公众号跳过
  280. if($adminConfig['is_qrcode_menu']==1){
  281. $error++;
  282. log::info('二维码菜单的公众号跳过.channel_id:'.$channel_id);
  283. }else{
  284. foreach($menu as $key => $val) {
  285. if (isset($val['url'])) {
  286. $menu[$key]['url'] = getCurrentDomain($channel_id, $val['url'],[],true);
  287. if(empty($adminConfig['vip_state']) && strpos($val['url'],'recharge/vip')!==false){
  288. $hasVipError++;
  289. }
  290. }
  291. if (isset($val['sub_button']) && !empty($val['sub_button'])) {
  292. foreach ($val['sub_button'] as $sub_k => $sub_v) {
  293. if (isset($sub_v['url'])) {
  294. $menu[$key]['sub_button'][$sub_k]['url'] = getCurrentDomain($channel_id, $sub_v['url'],[],true);
  295. if(empty($adminConfig['vip_state']) && strpos($sub_v['url'],'recharge/vip')!==false){
  296. $hasVipError++;
  297. }
  298. }
  299. }
  300. }
  301. }
  302. /**
  303. * 校验menu数据格式
  304. */
  305. foreach ($menu as $k => $v){
  306. $hasError = false;
  307. /**
  308. * 校验menu数据格式
  309. */
  310. foreach ($menu as $k => $v){
  311. if (isset($v['sub_button'])){
  312. foreach ($v['sub_button'] as $m => $n){
  313. if (isset($n['key']) && !$n['key']){
  314. $hasError = true;
  315. break 2;
  316. }
  317. }
  318. }else if (isset($v['key']) && !$v['key']){
  319. $hasError = true;
  320. break;
  321. }
  322. }
  323. }
  324. if (!$hasError && $hasVipError == 0) {
  325. $response = [];
  326. try{
  327. model('AdminConfig')->where('admin_id',$channel_id)->update(['wx_menu'=>json_encode($menu)]);
  328. $adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id);
  329. $wechat = new WeChatObject($adminConfig);
  330. $officialAccount = $wechat->getOfficialAccount();
  331. if(!is_null($officialAccount)){
  332. $officialAccount->menu->delete(); //删除全部菜单 主要是掌中云个性化菜单
  333. $ret = $officialAccount->menu->create($menu);
  334. if($ret && $ret['errcode'] == 0){
  335. $menu = WeChatMenu::rechargeMenuFilter($menu);
  336. if(!empty($menu)){
  337. $ret = $officialAccount->menu->create($menu,['client_platform_type' => 1]);
  338. }
  339. $response = $ret;
  340. }else{
  341. $error++;
  342. }
  343. }
  344. }catch (\Exception $e){
  345. $error++;
  346. log::info('微信菜单同步渠道失败1:'.$e->getMessage());
  347. }
  348. if($response && isset($response['errcode']) && $response['errcode'] == 0){
  349. //$this->success();
  350. }elseif($response>0){
  351. //$this->success();
  352. }else{
  353. $error++;
  354. log::info('微信菜单同步渠道失败2:'.json_encode($response));
  355. }
  356. }
  357. elseif(!$hasError && $hasVipError != 0){
  358. $error++;
  359. log::info('微信菜单同步渠道失败3:该账户未开通VIP充值管理菜单,请核对后重试');
  360. }
  361. else{
  362. return 2;
  363. }
  364. }
  365. }
  366. if($error<=0){
  367. $this->_handlerRecord($group_type,$group_ids,$this->auth->id,$time);
  368. return 1;
  369. }else{
  370. return 3;
  371. }
  372. }
  373. /**
  374. * 同步记录
  375. */
  376. protected function _handlerRecord($group_type,$group_ids,$admin_id,$time){
  377. if($group_type==1){
  378. // $where['admin_id'] = ['eq',$this->auth->id];
  379. // $group_arr = model('VipGroup')->where($where)->column('id');
  380. // if(count($group_arr)==1){
  381. // $group_str = $group_arr[0];
  382. // }else{
  383. // $group_str = implode(',',$group_arr);
  384. // }
  385. $group_str = '';
  386. }else{
  387. $group_str = $group_ids;
  388. }
  389. model('VipMenuSyncRecord')->insert(['sync_type'=>$group_type,'group_ids'=>$group_str,'admin_id'=>$admin_id,'createtime'=>$time]);
  390. }
  391. /**
  392. * 重置菜单
  393. */
  394. public function reset(){
  395. $menu = config('site.wx_menu');
  396. $group_type = $this->request->post("group_type");
  397. $group_ids = $this->request->post("group_ids");
  398. if($group_type==2 && empty($group_ids)){
  399. $this->error('请选择分组!');
  400. }
  401. if($group_type == 1){
  402. $where['admin_id_master'] = ['eq',$this->auth->id];
  403. $where['flag'] = ['eq',1];
  404. $channel_ids = model('VipAdminBind')->where($where)->column('admin_id_slave');
  405. }else{
  406. if(!empty($group_ids)){
  407. $tmp_ids = explode(',',$group_ids);
  408. if(count($tmp_ids) ==1){
  409. $where['vg.id'] = ['eq',$tmp_ids[0]];
  410. }else{
  411. $where['vg.id'] = ['in',$group_ids];
  412. }
  413. $channel_ids = model('VipGroup')->alias('vg')
  414. ->join('vip_group_info vgi','vg.id=vgi.group_id','left')
  415. ->where($where)
  416. ->column("vgi.channel_id");
  417. }else{
  418. $channel_ids = [];
  419. }
  420. }
  421. $time = time();
  422. if(empty($channel_ids)){
  423. $this->error('账号下不存在任何渠道');
  424. }
  425. $this->_hanlderSave($menu);
  426. if(count($channel_ids)==1){
  427. $this->sync($menu,$channel_ids[0],$time,$group_type,$group_ids);
  428. $this->success();
  429. }else{
  430. $code = $this->syncall($menu,$channel_ids,$time,$group_type,$group_ids);
  431. if($code == 1){
  432. $this->success();
  433. }elseif($code == 2){
  434. $this->error('菜单参数配置不完整,请检查是否有空菜单!');
  435. }elseif($code == 3){
  436. $this->error('菜单同步失败!');
  437. }
  438. }
  439. }
  440. /**
  441. * 新增保存vip_menu
  442. */
  443. public function _hanlderSave($menu){
  444. //判断url是否以cpsurl开头
  445. if(!is_array($menu)){
  446. $tmp_menu = json_decode($menu,true);
  447. }else{
  448. $tmp_menu = $menu;
  449. }
  450. foreach($tmp_menu as $me){
  451. if(isset($me['sub_button'])){
  452. foreach($me['sub_button'] as $m){
  453. if(isset($m['url']) && empty($m['url'])){
  454. $this->error("请填写跳转URL");
  455. }
  456. if(isset($m['url']) && strpos($m['url'],'cpsurl')!==0){
  457. $this->error("跳转网页链接必须以cpsurl/开头");
  458. }
  459. }
  460. }
  461. }
  462. $menu = json_encode($menu);
  463. if(strpos($menu,'"name":""')){
  464. $this->error("菜单名称不能为空");
  465. }
  466. if(empty($menu)){
  467. $this->error("菜单不能为空");
  468. }
  469. $rs = $this->model->get(['admin_id' => $this->auth->id]);
  470. if(!empty($rs)){
  471. $data = [
  472. 'wx_menu' => $menu,
  473. 'updatetime' => time()
  474. ];
  475. $this->model->save($data, ['admin_id' => $this->auth->id]);
  476. }else{
  477. $data = [
  478. 'admin_id' => $this->auth->id,
  479. 'wx_menu' => $menu,
  480. 'updatetime' => time()
  481. ];
  482. $this->model->save($data);
  483. }
  484. }
  485. /**
  486. * 公众号分组
  487. */
  488. public function usergroup()
  489. {
  490. $in_ids = $this->request->request('in_ids');
  491. //设置过滤方法
  492. $this->request->filter(['strip_tags']);
  493. if ($this->request->isAjax())
  494. {
  495. //如果发送的来源是Selectpage,则转发到Selectpage
  496. if ($this->request->request('pkey_name'))
  497. {
  498. return $this->selectpage();
  499. }
  500. //过滤ID
  501. $in_ids = $this->request->request('in_ids');
  502. if(!empty($in_ids)){
  503. $whereid['vip_group.id']= ['not in',$in_ids];
  504. }else{
  505. $whereid = [];
  506. }
  507. $model = model('VipGroup');
  508. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  509. $total = $model
  510. ->join('vip_group_info','vip_group.id=vip_group_info.group_id','left')
  511. ->join('admin_config','vip_group_info.channel_id=admin_config.admin_id','left')
  512. ->where('vip_group.admin_id','eq',$this->auth->id)
  513. ->where($where)
  514. ->where($whereid)
  515. ->order($sort, $order)
  516. ->group('vip_group.id')
  517. ->count();
  518. $list = $model
  519. ->join('vip_group_info','vip_group.id=vip_group_info.group_id','left')
  520. ->join('admin_config','vip_group_info.channel_id=admin_config.admin_id','left')
  521. ->where('vip_group.admin_id','eq',$this->auth->id)
  522. ->where($where)
  523. ->where($whereid)
  524. ->field(['vip_group.id','vip_group.name','vip_group.createtime','vip_group.updatetime'])
  525. ->order($sort, $order)
  526. ->group('vip_group.id')
  527. ->limit($offset, $limit)
  528. ->select();
  529. foreach($list as $li){
  530. $li['createtime'] = date('Y-m-d H:i:s',$li['createtime']);
  531. $li['updatetime'] = date('Y-m-d H:i:s',$li['updatetime']);
  532. }
  533. $result = array("total" => $total, "rows" => $list);
  534. return json($result);
  535. }
  536. $this->assignconfig('in_ids', $in_ids);
  537. return $this->view->fetch();
  538. }
  539. }