where('admin_id',$channel_id)->value('wx_menu'); return $wx_menu; } /** * 获取系统默认菜单 * @param $channel_id * @return mixed */ static public function getSystemDefaultMenu($channel_id){ $menu = json_decode(str_replace('cpsurl', '', config('site.wx_menu')), true); foreach($menu as $key => $val) { if (isset($val['url'])) { $menu[$key]['url'] = getCurrentDomain($channel_id, $val['url']); } 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']); } } } } return $menu; } static function replaceChannelMenuById($channel_id,$ophost_id, $is_qrcode_menu = '0'){ $adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id); $oldophost = ''; //获取新域名信息 $relace_host = model('Ophost')->where('id',$ophost_id)->column('platform_id,host'); $ophost = current($relace_host); $platform_id = key($relace_host); if(empty($adminConfig['wx_menu'])){ return [false,'替换渠道菜单失败,渠道菜单信息为空']; } //检查菜单是否为数组 if(is_array($adminConfig['wx_menu'])){ $adminConfig['wx_menu'] = json_encode($adminConfig['wx_menu'], JSON_UNESCAPED_UNICODE); } //查找以前菜单业务域名并替换 if($adminConfig['menuophost_id']){ $oldophost = model("ophost")->where("id", $adminConfig['menuophost_id'])->value("host"); if (strpos($adminConfig['wx_menu'], $oldophost)){ $adminConfig['wx_menu'] = str_replace($oldophost, $ophost, $adminConfig['wx_menu']); } } if ($is_qrcode_menu == '1') { // 使用二维码菜单 重新生成二维码 WxResponseService::instance()->reStructWxMenu($channel_id, $oldophost, $ophost); } //查找以前的业务域名并替换 if($adminConfig['ophost_id']){ $oldophost = model("ophost")->where("id", $adminConfig['ophost_id'])->value("host"); if (strpos($adminConfig['wx_menu'], $oldophost)){ $adminConfig['wx_menu'] = str_replace($oldophost, $ophost, $adminConfig['wx_menu']); } } //转成数组 if(!is_array($adminConfig['wx_menu'])){ $adminConfig['wx_menu'] = json_decode($adminConfig['wx_menu'],true); } //推送菜单 list($status,$message) = self::pushWeChatMenu($channel_id,$adminConfig['wx_menu']); Log::info(date('Y-m-d H:i:s')."Replace Channel WeChat Menu ChannelID:{$channel_id} PlatformID:{$platform_id} Source: {$oldophost} To: {$ophost} ISOK:".intval($status)." Message:".$message); //失败返回错误信息 if(!$status){ return [false,$message]; } //成功返回菜单数组 return [true,$adminConfig['wx_menu']]; } /** * 检查菜单是否合法 * @param $menu * @return bool */ static public function checkWeChatMenu($menu){ if(!$menu) return false; $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 = false; break 2; }else{ $hasError = true; continue; } } }else{ if(isset($v['key']) && !$v['key']){ $hasError = false; break; }else{ $hasError = true; continue; } } } return $hasError; } /** * 推送菜单到微信 * @param $channel_id * @param $menu * @param $platform_id * @return array * @throws \Exception */ static public function pushWeChatMenu($channel_id,array $menu,$platform_id = null){ try{ $adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id); $weChat = new WeChatObject($adminConfig); $officialAccount = $weChat->getOfficialAccount(); if($platform_id){ $officialAccount = $weChat->getOfficialAccountByPlatform($platform_id); } if(empty($menu)){ throw new \Exception('菜单不能为空'); } $officialAccount->menu->delete(); //删除全部菜单 主要是掌中云个性化菜单 if ($adminConfig['is_qrcode_menu'] == '1') { // 菜单链接生成二维码图片 $row = model('AdminWxmenu')->get(['admin_id' => $channel_id]); $menu = json_decode($row->wx_qrcode_menu, true); $ret = $officialAccount->menu->create($menu); } else { $ret = $officialAccount->menu->create($menu); if($ret && $ret['errcode'] == 0){ $menu = self::rechargeMenuFilter($menu); if(!empty($menu)){ $ret = $officialAccount->menu->create($menu,['client_platform_type' => 1]); } }else{ throw new \Exception($ret['errmsg']); } } if($ret && isset($ret['errcode']) && $ret['errcode'] == 0){ return [true,'ok']; }elseif($ret>0){ return [true,'ok']; }else{ throw new \Exception($ret['errmsg']); } }catch (\Exception $e){ return [false,$e->getMessage()]; } } /** * 过滤IOS系统的带有充值链接的微信菜单 * @param $menu * @return mixed */ static public function rechargeMenuFilter(&$menu){ $rs = false; foreach($menu as $kk=>&$me){ if(!isset($me['sub_button'])){ if($me['type'] == 'view' && strpos($me['url'],'recharge')!==false){ unset($menu[$kk]); } }else{ if(count($me['sub_button']) == 1 && $me['sub_button'][0]['type'] == 'view' && strpos($me['sub_button'][0]['url'],'recharge')!==false){ unset($menu[$kk]); }else{ foreach($me['sub_button'] as $k=>&$m){ if(isset($m['type']) && $m['type'] == 'view' && strpos($m['url'],'recharge')!==false){ unset($menu[$kk]['sub_button'][$k]); } } $me['sub_button'] = array_values($me['sub_button']); } } } if(!empty($menu)){ $menu = array_values($menu); $rs = self::checkWeChatMenu($menu); } if($rs){ return $menu; }else{ return []; } } }