123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- namespace app\admin\controller\wechat;
- use app\common\controller\Backend;
- use app\common\library\WeChatObject;
- use app\common\model\AdminConfig;
- use app\common\model\WechatResponse;
- use app\common\utility\DiyQRCode;
- use app\common\utility\WeChatMenu;
- use app\main\service\WxResponseService;
- use EasyWeChat\Factory;
- use app\common\library\Redis;
- use Symfony\Component\Cache\Simple\RedisCache;
- /**
- * 菜单管理
- *
- * @icon fa fa-list-alt
- */
- class Menu extends Backend
- {
- protected $noNeedRight = ['reset', 'ajaxmofiywxmenu','unsetuserqrcode'];
- public function _initialize()
- {
- parent::_initialize();
- }
- /**
- * 查看
- */
- public function index()
- {
- if($this->auth->agent_id){
- $channel_id = $this->auth->agent_id;
- }else{
- $channel_id = $this->auth->channel_id;
- }
- if($this->auth->agent_id || $this->group == 3){
- if(!model('AdminConfig')->checkWechatConfig($this->auth->id)){
- $this->error('请先授权微信服务号给本系统,正在跳转授权设置页面~', url('admin/config'));
- }
- }
- $responselist = array();
- $all = WechatResponse::all(['admin_id' => $this->auth->id]);
- foreach ($all as $k => $v)
- {
- $responselist[$v['eventkey']] = $v['title'];
- }
- $this->view->assign('responselist', $responselist);
- $model = model('AdminConfig');
- $row = $model->get(['admin_id' => $this->auth->id]);
- $menu = $row->wx_menu ?? null;
- // if(!empty($menu)){
- // $this->handleMenu($menu, $channel_id);
- // }
- if (!$menu) {
- $menu = json_decode(str_replace('cpsurl', '', config('site.wx_menu')), true);
- $this->handleMenu($menu, $channel_id);
- }
- $this->view->assign('is_qrcode_menu', $row->is_qrcode_menu);
- $this->view->assign('menu', $menu);
- $vip_menu = [];
- $is_vip_channel=0;
- $vip_group_id = model('VipGroupInfo')->where('channel_id','eq',$this->auth->id)->column('group_id');
- if(!empty($vip_group_id)){
- $is_vip_channel = 1;
- $vip_menu_arr = model('VipGroup')
- ->alias('vg')
- ->join('vip_menu vm','vg.admin_id=vm.admin_id')
- ->where('vg.id','eq',$vip_group_id[0])
- ->column('wx_menu');
- if(!empty($vip_menu_arr)){
- $vip_menu = $vip_menu_arr[0];
- $vip_menu = str_replace('\"','"',$vip_menu);
- $vip_menu = str_replace('"[','[',$vip_menu);
- $vip_menu = str_replace(']"',']',$vip_menu);
- $vip_menu = str_replace('cpsurl','',$vip_menu);
- $vip_menu = json_decode($vip_menu,true);
- $this->handleMenu($vip_menu, $channel_id);
- }
- }
- $this->view->assign('vip_menu', $vip_menu);
- $this->view->assign('is_vip_channel', $is_vip_channel);
- return $this->view->fetch();
- }
- private function handleMenu(&$menu, $channel_id){
- foreach($menu as $key => $val) {
- if (isset($val['url'])) {
- $menu[$key]['url'] = getCurrentDomain($channel_id, $val['url'],[],true);
- }
- if (isset($val['sub_button']) && !empty($val['sub_button'])) {
- foreach ($val['sub_button'] as $sub_k => $sub_v) {
- if (isset($sub_v['url'])) {
- $menu[$key]['sub_button'][$sub_k]['url'] = getCurrentDomain($channel_id, $sub_v['url'],[],true);
- }
- }
- }
- }
- }
- /**
- * 修改
- */
- public function edit($ids = null)
- {
- $menu = $this->request->post("menu");
- if(strpos($menu,'"name":""')){
- $this->error("菜单名称不能为空");
- }
- if(empty($menu)){
- $this->error("菜单不能为空");
- }
- $menu = json_decode($menu, true);
- $model = model('AdminConfig');
- $model->save(['wx_menu' => $menu], ['admin_id' => $this->auth->id]);
- $row = $model->get(['admin_id' => $this->auth->id]);
- if($row->is_qrcode_menu){
- WxResponseService::instance()->reStructWxMenu($this->auth->id);
- }
- $this->success();
- }
- public function reset(){
- if($this->auth->agent_id){
- $channel_id = $this->auth->agent_id;
- }else{
- $channel_id = $this->auth->channel_id;
- }
- if($this->auth->agent_id || $this->group == 3){
- if(!model('AdminConfig')->checkWechatConfig($this->auth->id)){
- $this->error('请先授权微信服务号给本系统,正在跳转授权设置页面~', url('admin/config'));
- }
- }
- $wx_menu = WeChatMenu::getSystemDefaultMenu($channel_id);
- if(empty($wx_menu)){
- $this->error('菜单不能为空!');
- }
- if(!WeChatMenu::checkWeChatMenu($wx_menu)){
- $this->error('菜单参数配置不完整,请检查是否有空菜单!');
- }
- $update = model('AdminConfig')->where('admin_id',$this->auth->id)->update(['wx_menu'=>json_encode($wx_menu)]);
- if($update !== false){
- model('AdminConfig')->delAdminInfoAllCache($this->auth->id);
- // 同步二维码菜单
- $row = model('AdminConfig')->getAdminInfoAll($this->auth->id);
- if($row['is_qrcode_menu']){
- WxResponseService::instance()->reStructWxMenu($this->auth->id);
- }
- list($status,$message) = WeChatMenu::pushWeChatMenu($channel_id,$wx_menu);
- if($status){
- $this->success('恢复默认菜单成功');
- }else{
- $this->error($message);
- }
- }else{
- $this->error('恢复默认菜单失败,更新数据写入失败');
- }
- }
- /**
- * 同步
- */
- public function sync($ids = NULL)
- {
- $hasError = false;
- $model = model('AdminConfig');
- $row = $model->get(['admin_id' => $this->auth->id]);
- if($row->is_qrcode_menu){
- $row = model('AdminWxmenu')->get(['admin_id' => $this->auth->id]);
- $menu = json_decode($row->wx_qrcode_menu, true);
- }else{
- $menu = $row->wx_menu;
- }
- if(empty($menu)){
- $this->error('菜单不能为空');
- }
- /**
- * 校验menu数据格式
- */
- foreach ($menu as $k => $v)
- {
- $hasError = false;
- $model = model('AdminConfig');
- $row = $model->getAdminInfoAll($this->auth->id);
- if($row['is_qrcode_menu']){
- $row = model('AdminWxmenu')->get(['admin_id' => $this->auth->id]);
- $menu = json_decode($row->wx_qrcode_menu, true);
- }else{
- $menu = $row['wx_menu'];
- }
- /**
- * 校验menu数据格式
- */
- foreach ($menu as $k => $v)
- {
- if (isset($v['sub_button']))
- {
- foreach ($v['sub_button'] as $m => $n)
- {
- if (isset($n['key']) && !$n['key'])
- {
- $hasError = true;
- break 2;
- }
- }
- }
- else if (isset($v['key']) && !$v['key'])
- {
- $hasError = true;
- break;
- }
- }
- }
- if (!$hasError) {
- $response = [];
- try{
- $adminConfig = model('AdminConfig')->getAdminInfoAll($this->auth->id);
- $wechat = new WeChatObject($adminConfig);
- $officialAccount = $wechat->getOfficialAccount();
- $officialAccount->menu->delete(); //删除全部菜单 主要是掌中云个性化菜单
- $ret = $officialAccount->menu->create($menu);
- if($ret && $ret['errcode'] == 0){
- $menu = WeChatMenu::rechargeMenuFilter($menu);
- if(!empty($menu)){
- $ret = $officialAccount->menu->create($menu,['client_platform_type' => 1]);
- }
- $response = $ret;
- }else{
- $this->error($ret['errmsg']);
- }
- }catch (\Exception $e){
- $this->error($e->getMessage());
- }
- if($response && isset($response['errcode']) && $response['errcode'] == 0){
- $this->success();
- }elseif($response>0){
- $this->success();
- }else{
- $this->error($response['errmsg']);
- }
- }
- else
- {
- $this->error('菜单参数配置不完整,请检查是否有空菜单!');
- }
- }
- /**
- * 切换微信菜单方式
- */
- public function ajaxMofiyWxMenu()
- {
- $admin_id = $this->auth->id;
- $qrcode_menu_value = $this->request->param('qrcode_menu_value');
- $redis = Redis::instance();
- $redis_key = AdminConfig::CACHE_KEY_ADMIN_INFO.$admin_id;
- if ($qrcode_menu_value == '1') {
- // 修改菜单 链接方式
- model('AdminConfig')->update(['is_qrcode_menu' => '0'], ['admin_id' => $admin_id]);
- $redis->del($redis_key);
- //发布菜单
- $this->sync();
- return json(['code'=> 1, 'msg' => 'success']);
- } else {
- // 修改菜单 图片二维码
- model('AdminConfig')->update(['is_qrcode_menu' => '1'], ['admin_id' => $admin_id]);
- $redis->del($redis_key);
- //重构微信菜单:遍历微信菜单, 如果是url ,根据url 生成图片 ,上传资源 ,
- WxResponseService::instance()->reStructWxMenu($admin_id);
- // 发布菜单 同步微信二维码菜单
- WxResponseService::instance()->syncQrcodeMenu($admin_id);
- return json(['code'=> 1, 'msg' => 'success']);
- }
- }
- public function unSetUserQrCode(){
- $redis = Redis::instance();
- $redis_key = AdminConfig::CACHE_KEY_ADMIN_INFO.$this->auth->id;
- model('AdminConfig')->update(['is_qrcode_menu' => '0'], ['admin_id' => $this->auth->id]);
- $redis->del($redis_key);
- $this->success();
- }
- }
|