model = model('VipMenu'); parent::_initialize(); } /** * 查看 */ public function index() { $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); $vrow = $this->model->get(['admin_id' => $this->auth->id]); $menu = $vrow->wx_menu ?? null; $menu = str_replace('\"','"',$menu); $menu = str_replace('"[','[',$menu); $menu = str_replace(']"',']',$menu); if (!$menu) { $menu = json_decode(config('site.wx_menu'),true); } if(!is_array($menu)){ $menu=json_decode($menu,true); } $this->view->assign('menu', $menu); $sync_record = model('VipMenuSyncRecord') ->where('admin_id','eq',$this->auth->id) ->order('createtime','desc') ->limit(1) ->select(); $sync_record = collection($sync_record)->toArray(); if(empty($sync_record)){ $this->view->assign('isall', 1); $this->view->assign('sync_list', []); $this->view->assign('group_ids_str', ''); }else{ if($sync_record[0]['sync_type'] ==1){ $this->view->assign('isall', 1); $this->view->assign('sync_list', []); $this->view->assign('group_ids_str', ''); }else{ $sync_list = model('VipGroup')->where('id','in',$sync_record[0]['group_ids'])->select(); $sync_list = collection($sync_list)->toArray(); foreach($sync_list as &$kp){ $kp['createtime'] = date('Y-m-d H:i:s',$kp['createtime']); $kp['updatetime'] = date('Y-m-d H:i:s',$kp['updatetime']); } $this->view->assign('isall', 2); $this->view->assign('sync_list', $sync_list); $this->view->assign('group_ids_str', $sync_record[0]['group_ids']); $this->assignconfig('group_ids_js', explode(',',$sync_record[0]['group_ids'])); $this->assignconfig('gzhHtmlArr', $sync_list); } } return $this->view->fetch(); } /** * 保存 */ public function edit($ids = NULL) { $menu = $this->request->post("menu"); if(empty($menu)){ $this->error('菜单不能为空!'); } $group_type = $this->request->post("group_type"); $group_ids = $this->request->post("group_ids"); if($group_type==2 && empty($group_ids)){ $this->error('请选择分组!'); } if($group_type == 1){ // $group_id_arr = model('VipGroup')->where('admin_id','eq',$this->auth->id)->column('id'); // if(count($group_id_arr) == 1){ // $group_id_str = $group_id_arr[0]; // }elseif (empty($group_id_arr)){ // $this->error('您账号下没有分组!'); // }else{ // $group_id_str = implode(',',$group_id_arr); // } $group_id_str = ''; }else{ $group_id_str = $group_ids; } model('VipMenuSyncRecord')->insert(['sync_type'=>$group_type,'group_ids'=>$group_id_str,'admin_id'=>$this->auth->id,'createtime'=>time()]); $this->_hanlderSave($menu); $this->success(); } /** * 保存并发布 */ public function saveto(){ $menu = $this->request->post("menu"); if(empty($menu)){ $this->error('菜单不能为空!'); } $group_type = $this->request->post("group_type"); $group_ids = $this->request->post("group_ids"); if($group_type==2 && empty($group_ids)){ $this->error('请选择分组!'); } if($group_type == 1){ $where['admin_id_master'] = ['eq',$this->auth->id]; $where['flag'] = ['eq',1]; $channel_ids = model('VipAdminBind')->where($where)->column('admin_id_slave'); }else{ if(!empty($group_ids)){ $tmp_ids = explode(',',$group_ids); if(count($tmp_ids) ==1){ $where['vg.id'] = ['eq',$tmp_ids[0]]; }else{ $where['vg.id'] = ['in',$group_ids]; } $channel_ids = model('VipGroup')->alias('vg') ->join('vip_group_info vgi','vg.id=vgi.group_id','left') ->where($where) ->column("vgi.channel_id"); }else{ $channel_ids = []; } } if(empty($channel_ids)){ $this->error('账号下不存在任何渠道'); } $this->_hanlderSave(json_decode($menu,true)); $time = time(); if(count($channel_ids)==1){ $code = $this->sync($menu,$channel_ids[0],$time,$group_type,$group_ids); if($code['code']==1){ $this->success(); }else{ $this->error($code['msg']); } }else{ $code = $this->syncall($menu,$channel_ids,$time,$group_type,$group_ids); if($code == 1){ $this->success(); }elseif($code == 2){ $this->error('菜单参数配置不完整,请检查是否有空菜单!'); }elseif($code == 3){ $this->error('菜单同步失败!'); } } } /** * 同步 */ public function sync($menu,$channel_id,$time,$group_type,$group_ids) { $hasVipError = 0; $hasError = false; $adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id); if($adminConfig['is_qrcode_menu']==1){ return ['code'=>6,'msg'=>'该账户已设置为二维码菜单,请修改为链接菜单后重试']; } if(empty($menu)){ return ['code'=>10,'msg'=>'菜单不能为空']; } $menu = json_decode(str_replace('cpsurl', '', $menu), true); foreach($menu as $key => $val) { if (isset($val['url'])) { $menu[$key]['url'] = getCurrentDomain($channel_id, $val['url'],[],true); if(empty($adminConfig['vip_state']) && strpos($val['url'],'recharge/vip')!==false){ $hasVipError++; } } 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); if(empty($adminConfig['vip_state']) && strpos($sub_v['url'],'recharge/vip')!==false){ $hasVipError++; } } } } } /** * 校验menu数据格式 */ foreach ($menu as $k => $v){ $hasError = false; /** * 校验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 && $hasVipError == 0) { $response = []; try{ model('AdminConfig')->where('admin_id',$channel_id)->update(['wx_menu'=>json_encode($menu)]); $adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id); $wechat = new WeChatObject($adminConfig); $officialAccount = $wechat->getOfficialAccount(); if(is_null($officialAccount)){ return ['code'=>2,'msg'=>'分组包含未授权账户,请核实后重新提交']; } $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]); } }else{ return ['code'=>4,'msg'=>$response['errmsg']]; } $response = $ret; }catch (\Exception $e){ return ['code'=>3,'msg'=>$e->getMessage()]; } if($response && isset($response['errcode']) && $response['errcode'] == 0){ $this->success(); }elseif($response>0){ $this->success(); }else{ return ['code'=>4,'msg'=>$response['errmsg']]; } }elseif(!$hasError && $hasVipError != 0){ return ['code'=>5,'msg'=>'该账户未开通VIP充值管理菜单,请核对后重试']; }else{ $this->error('菜单参数配置不完整,请检查是否有空菜单!'); } return ['code'=>1]; } /** * 同步全部渠道 */ public function syncall($menu,$channel_ids,$time,$group_type,$group_ids) { $success = 0; $error = 0; if(empty($menu)){ $error++; } $origin_menu = json_decode(str_replace('cpsurl', '', $menu), true); foreach($channel_ids as $channel_id){ $menu = $origin_menu; $hasVipError = 0; $hasError = false; $adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id); //是二维码菜单的公众号跳过 if($adminConfig['is_qrcode_menu']==1){ $error++; log::info('二维码菜单的公众号跳过.channel_id:'.$channel_id); }else{ foreach($menu as $key => $val) { if (isset($val['url'])) { $menu[$key]['url'] = getCurrentDomain($channel_id, $val['url'],[],true); if(empty($adminConfig['vip_state']) && strpos($val['url'],'recharge/vip')!==false){ $hasVipError++; } } 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); if(empty($adminConfig['vip_state']) && strpos($sub_v['url'],'recharge/vip')!==false){ $hasVipError++; } } } } } /** * 校验menu数据格式 */ foreach ($menu as $k => $v){ $hasError = false; /** * 校验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 && $hasVipError == 0) { $response = []; try{ model('AdminConfig')->where('admin_id',$channel_id)->update(['wx_menu'=>json_encode($menu)]); $adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id); $wechat = new WeChatObject($adminConfig); $officialAccount = $wechat->getOfficialAccount(); if(!is_null($officialAccount)){ $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{ $error++; } } }catch (\Exception $e){ $error++; log::info('微信菜单同步渠道失败1:'.$e->getMessage()); } if($response && isset($response['errcode']) && $response['errcode'] == 0){ //$this->success(); }elseif($response>0){ //$this->success(); }else{ $error++; log::info('微信菜单同步渠道失败2:'.json_encode($response)); } } elseif(!$hasError && $hasVipError != 0){ $error++; log::info('微信菜单同步渠道失败3:该账户未开通VIP充值管理菜单,请核对后重试'); } else{ return 2; } } } if($error<=0){ $this->_handlerRecord($group_type,$group_ids,$this->auth->id,$time); return 1; }else{ return 3; } } /** * 同步记录 */ protected function _handlerRecord($group_type,$group_ids,$admin_id,$time){ if($group_type==1){ // $where['admin_id'] = ['eq',$this->auth->id]; // $group_arr = model('VipGroup')->where($where)->column('id'); // if(count($group_arr)==1){ // $group_str = $group_arr[0]; // }else{ // $group_str = implode(',',$group_arr); // } $group_str = ''; }else{ $group_str = $group_ids; } model('VipMenuSyncRecord')->insert(['sync_type'=>$group_type,'group_ids'=>$group_str,'admin_id'=>$admin_id,'createtime'=>$time]); } /** * 重置菜单 */ public function reset(){ $menu = config('site.wx_menu'); $group_type = $this->request->post("group_type"); $group_ids = $this->request->post("group_ids"); if($group_type==2 && empty($group_ids)){ $this->error('请选择分组!'); } if($group_type == 1){ $where['admin_id_master'] = ['eq',$this->auth->id]; $where['flag'] = ['eq',1]; $channel_ids = model('VipAdminBind')->where($where)->column('admin_id_slave'); }else{ if(!empty($group_ids)){ $tmp_ids = explode(',',$group_ids); if(count($tmp_ids) ==1){ $where['vg.id'] = ['eq',$tmp_ids[0]]; }else{ $where['vg.id'] = ['in',$group_ids]; } $channel_ids = model('VipGroup')->alias('vg') ->join('vip_group_info vgi','vg.id=vgi.group_id','left') ->where($where) ->column("vgi.channel_id"); }else{ $channel_ids = []; } } $time = time(); if(empty($channel_ids)){ $this->error('账号下不存在任何渠道'); } $this->_hanlderSave($menu); if(count($channel_ids)==1){ $this->sync($menu,$channel_ids[0],$time,$group_type,$group_ids); $this->success(); }else{ $code = $this->syncall($menu,$channel_ids,$time,$group_type,$group_ids); if($code == 1){ $this->success(); }elseif($code == 2){ $this->error('菜单参数配置不完整,请检查是否有空菜单!'); }elseif($code == 3){ $this->error('菜单同步失败!'); } } } /** * 新增保存vip_menu */ public function _hanlderSave($menu){ //判断url是否以cpsurl开头 if(!is_array($menu)){ $tmp_menu = json_decode($menu,true); }else{ $tmp_menu = $menu; } foreach($tmp_menu as $me){ if(isset($me['sub_button'])){ foreach($me['sub_button'] as $m){ if(isset($m['url']) && empty($m['url'])){ $this->error("请填写跳转URL"); } if(isset($m['url']) && strpos($m['url'],'cpsurl')!==0){ $this->error("跳转网页链接必须以cpsurl/开头"); } } } } $menu = json_encode($menu); if(strpos($menu,'"name":""')){ $this->error("菜单名称不能为空"); } if(empty($menu)){ $this->error("菜单不能为空"); } $rs = $this->model->get(['admin_id' => $this->auth->id]); if(!empty($rs)){ $data = [ 'wx_menu' => $menu, 'updatetime' => time() ]; $this->model->save($data, ['admin_id' => $this->auth->id]); }else{ $data = [ 'admin_id' => $this->auth->id, 'wx_menu' => $menu, 'updatetime' => time() ]; $this->model->save($data); } } /** * 公众号分组 */ public function usergroup() { $in_ids = $this->request->request('in_ids'); //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } //过滤ID $in_ids = $this->request->request('in_ids'); if(!empty($in_ids)){ $whereid['vip_group.id']= ['not in',$in_ids]; }else{ $whereid = []; } $model = model('VipGroup'); list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $total = $model ->join('vip_group_info','vip_group.id=vip_group_info.group_id','left') ->join('admin_config','vip_group_info.channel_id=admin_config.admin_id','left') ->where('vip_group.admin_id','eq',$this->auth->id) ->where($where) ->where($whereid) ->order($sort, $order) ->group('vip_group.id') ->count(); $list = $model ->join('vip_group_info','vip_group.id=vip_group_info.group_id','left') ->join('admin_config','vip_group_info.channel_id=admin_config.admin_id','left') ->where('vip_group.admin_id','eq',$this->auth->id) ->where($where) ->where($whereid) ->field(['vip_group.id','vip_group.name','vip_group.createtime','vip_group.updatetime']) ->order($sort, $order) ->group('vip_group.id') ->limit($offset, $limit) ->select(); foreach($list as $li){ $li['createtime'] = date('Y-m-d H:i:s',$li['createtime']); $li['updatetime'] = date('Y-m-d H:i:s',$li['updatetime']); } $result = array("total" => $total, "rows" => $list); return json($result); } $this->assignconfig('in_ids', $in_ids); return $this->view->fetch(); } }