Menu.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. /**
  14. * 菜单管理
  15. *
  16. * @icon fa fa-list-alt
  17. */
  18. class Menu extends Backend
  19. {
  20. protected $noNeedRight = ['reset', 'ajaxmofiywxmenu','unsetuserqrcode'];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. }
  25. /**
  26. * 查看
  27. */
  28. public function index()
  29. {
  30. if($this->auth->agent_id){
  31. $channel_id = $this->auth->agent_id;
  32. }else{
  33. $channel_id = $this->auth->channel_id;
  34. }
  35. if($this->auth->agent_id || $this->group == 3){
  36. if(!model('AdminConfig')->checkWechatConfig($this->auth->id)){
  37. $this->error('请先授权微信服务号给本系统,正在跳转授权设置页面~', url('admin/config'));
  38. }
  39. }
  40. $responselist = array();
  41. $all = WechatResponse::all(['admin_id' => $this->auth->id]);
  42. foreach ($all as $k => $v)
  43. {
  44. $responselist[$v['eventkey']] = $v['title'];
  45. }
  46. $this->view->assign('responselist', $responselist);
  47. $model = model('AdminConfig');
  48. $row = $model->get(['admin_id' => $this->auth->id]);
  49. $menu = $row->wx_menu ?? null;
  50. // if(!empty($menu)){
  51. // $this->handleMenu($menu, $channel_id);
  52. // }
  53. if (!$menu) {
  54. $menu = json_decode(str_replace('cpsurl', '', config('site.wx_menu')), true);
  55. $this->handleMenu($menu, $channel_id);
  56. }
  57. $this->view->assign('is_qrcode_menu', $row->is_qrcode_menu);
  58. $this->view->assign('menu', $menu);
  59. $vip_menu = [];
  60. $is_vip_channel=0;
  61. $vip_group_id = model('VipGroupInfo')->where('channel_id','eq',$this->auth->id)->column('group_id');
  62. if(!empty($vip_group_id)){
  63. $is_vip_channel = 1;
  64. $vip_menu_arr = model('VipGroup')
  65. ->alias('vg')
  66. ->join('vip_menu vm','vg.admin_id=vm.admin_id')
  67. ->where('vg.id','eq',$vip_group_id[0])
  68. ->column('wx_menu');
  69. if(!empty($vip_menu_arr)){
  70. $vip_menu = $vip_menu_arr[0];
  71. $vip_menu = str_replace('\"','"',$vip_menu);
  72. $vip_menu = str_replace('"[','[',$vip_menu);
  73. $vip_menu = str_replace(']"',']',$vip_menu);
  74. $vip_menu = str_replace('cpsurl','',$vip_menu);
  75. $vip_menu = json_decode($vip_menu,true);
  76. $this->handleMenu($vip_menu, $channel_id);
  77. }
  78. }
  79. $this->view->assign('vip_menu', $vip_menu);
  80. $this->view->assign('is_vip_channel', $is_vip_channel);
  81. return $this->view->fetch();
  82. }
  83. private function handleMenu(&$menu, $channel_id){
  84. foreach($menu as $key => $val) {
  85. if (isset($val['url'])) {
  86. $menu[$key]['url'] = getCurrentDomain($channel_id, $val['url'],[],true);
  87. }
  88. if (isset($val['sub_button']) && !empty($val['sub_button'])) {
  89. foreach ($val['sub_button'] as $sub_k => $sub_v) {
  90. if (isset($sub_v['url'])) {
  91. $menu[$key]['sub_button'][$sub_k]['url'] = getCurrentDomain($channel_id, $sub_v['url'],[],true);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * 修改
  99. */
  100. public function edit($ids = null)
  101. {
  102. $menu = $this->request->post("menu");
  103. if(strpos($menu,'"name":""')){
  104. $this->error("菜单名称不能为空");
  105. }
  106. if(empty($menu)){
  107. $this->error("菜单不能为空");
  108. }
  109. $menu = json_decode($menu, true);
  110. $model = model('AdminConfig');
  111. $model->save(['wx_menu' => $menu], ['admin_id' => $this->auth->id]);
  112. $row = $model->get(['admin_id' => $this->auth->id]);
  113. if($row->is_qrcode_menu){
  114. WxResponseService::instance()->reStructWxMenu($this->auth->id);
  115. }
  116. $this->success();
  117. }
  118. public function reset(){
  119. if($this->auth->agent_id){
  120. $channel_id = $this->auth->agent_id;
  121. }else{
  122. $channel_id = $this->auth->channel_id;
  123. }
  124. if($this->auth->agent_id || $this->group == 3){
  125. if(!model('AdminConfig')->checkWechatConfig($this->auth->id)){
  126. $this->error('请先授权微信服务号给本系统,正在跳转授权设置页面~', url('admin/config'));
  127. }
  128. }
  129. $wx_menu = WeChatMenu::getSystemDefaultMenu($channel_id);
  130. if(empty($wx_menu)){
  131. $this->error('菜单不能为空!');
  132. }
  133. if(!WeChatMenu::checkWeChatMenu($wx_menu)){
  134. $this->error('菜单参数配置不完整,请检查是否有空菜单!');
  135. }
  136. $update = model('AdminConfig')->where('admin_id',$this->auth->id)->update(['wx_menu'=>json_encode($wx_menu)]);
  137. if($update !== false){
  138. model('AdminConfig')->delAdminInfoAllCache($this->auth->id);
  139. // 同步二维码菜单
  140. $row = model('AdminConfig')->getAdminInfoAll($this->auth->id);
  141. if($row['is_qrcode_menu']){
  142. WxResponseService::instance()->reStructWxMenu($this->auth->id);
  143. }
  144. list($status,$message) = WeChatMenu::pushWeChatMenu($channel_id,$wx_menu);
  145. if($status){
  146. $this->success('恢复默认菜单成功');
  147. }else{
  148. $this->error($message);
  149. }
  150. }else{
  151. $this->error('恢复默认菜单失败,更新数据写入失败');
  152. }
  153. }
  154. /**
  155. * 同步
  156. */
  157. public function sync($ids = NULL)
  158. {
  159. $hasError = false;
  160. $model = model('AdminConfig');
  161. $row = $model->get(['admin_id' => $this->auth->id]);
  162. if($row->is_qrcode_menu){
  163. $row = model('AdminWxmenu')->get(['admin_id' => $this->auth->id]);
  164. $menu = json_decode($row->wx_qrcode_menu, true);
  165. }else{
  166. $menu = $row->wx_menu;
  167. }
  168. if(empty($menu)){
  169. $this->error('菜单不能为空');
  170. }
  171. /**
  172. * 校验menu数据格式
  173. */
  174. foreach ($menu as $k => $v)
  175. {
  176. $hasError = false;
  177. $model = model('AdminConfig');
  178. $row = $model->getAdminInfoAll($this->auth->id);
  179. if($row['is_qrcode_menu']){
  180. $row = model('AdminWxmenu')->get(['admin_id' => $this->auth->id]);
  181. $menu = json_decode($row->wx_qrcode_menu, true);
  182. }else{
  183. $menu = $row['wx_menu'];
  184. }
  185. /**
  186. * 校验menu数据格式
  187. */
  188. foreach ($menu as $k => $v)
  189. {
  190. if (isset($v['sub_button']))
  191. {
  192. foreach ($v['sub_button'] as $m => $n)
  193. {
  194. if (isset($n['key']) && !$n['key'])
  195. {
  196. $hasError = true;
  197. break 2;
  198. }
  199. }
  200. }
  201. else if (isset($v['key']) && !$v['key'])
  202. {
  203. $hasError = true;
  204. break;
  205. }
  206. }
  207. }
  208. if (!$hasError) {
  209. $response = [];
  210. try{
  211. $adminConfig = model('AdminConfig')->getAdminInfoAll($this->auth->id);
  212. $wechat = new WeChatObject($adminConfig);
  213. $officialAccount = $wechat->getOfficialAccount();
  214. $officialAccount->menu->delete(); //删除全部菜单 主要是掌中云个性化菜单
  215. $ret = $officialAccount->menu->create($menu);
  216. if($ret && $ret['errcode'] == 0){
  217. $menu = WeChatMenu::rechargeMenuFilter($menu);
  218. if(!empty($menu)){
  219. $ret = $officialAccount->menu->create($menu,['client_platform_type' => 1]);
  220. }
  221. $response = $ret;
  222. }else{
  223. $this->error($ret['errmsg']);
  224. }
  225. }catch (\Exception $e){
  226. $this->error($e->getMessage());
  227. }
  228. if($response && isset($response['errcode']) && $response['errcode'] == 0){
  229. $this->success();
  230. }elseif($response>0){
  231. $this->success();
  232. }else{
  233. $this->error($response['errmsg']);
  234. }
  235. }
  236. else
  237. {
  238. $this->error('菜单参数配置不完整,请检查是否有空菜单!');
  239. }
  240. }
  241. /**
  242. * 切换微信菜单方式
  243. */
  244. public function ajaxMofiyWxMenu()
  245. {
  246. $admin_id = $this->auth->id;
  247. $qrcode_menu_value = $this->request->param('qrcode_menu_value');
  248. $redis = Redis::instance();
  249. $redis_key = AdminConfig::CACHE_KEY_ADMIN_INFO.$admin_id;
  250. if ($qrcode_menu_value == '1') {
  251. // 修改菜单 链接方式
  252. model('AdminConfig')->update(['is_qrcode_menu' => '0'], ['admin_id' => $admin_id]);
  253. $redis->del($redis_key);
  254. //发布菜单
  255. $this->sync();
  256. return json(['code'=> 1, 'msg' => 'success']);
  257. } else {
  258. // 修改菜单 图片二维码
  259. model('AdminConfig')->update(['is_qrcode_menu' => '1'], ['admin_id' => $admin_id]);
  260. $redis->del($redis_key);
  261. //重构微信菜单:遍历微信菜单, 如果是url ,根据url 生成图片 ,上传资源 ,
  262. WxResponseService::instance()->reStructWxMenu($admin_id);
  263. // 发布菜单 同步微信二维码菜单
  264. WxResponseService::instance()->syncQrcodeMenu($admin_id);
  265. return json(['code'=> 1, 'msg' => 'success']);
  266. }
  267. }
  268. public function unSetUserQrCode(){
  269. $redis = Redis::instance();
  270. $redis_key = AdminConfig::CACHE_KEY_ADMIN_INFO.$this->auth->id;
  271. model('AdminConfig')->update(['is_qrcode_menu' => '0'], ['admin_id' => $this->auth->id]);
  272. $redis->del($redis_key);
  273. $this->success();
  274. }
  275. }