123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
- /*
- * 检测公众号是否被封
- */
- namespace app\admin\command;
- use app\common\library\Redis;
- use app\common\library\WeChatObject;
- use EasyWeChat\Factory;
- use Symfony\Component\Cache\Simple\RedisCache;
- use think\Config;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Option;
- use think\console\Output;
- use app\common\model\AdminConfig;
- use app\common\model\Config as dbconfig;
- use think\Log;
- use think\Exception;
- use think\Request;
- class ChangeMenu extends Command
- {
- protected $message = '';
- protected function configure()
- {
- $this->setName('ChangeMenu')
- ->addOption("find","f",Option::VALUE_REQUIRED,'查找要替换的菜单域名')
- ->addOption("replace","r",Option::VALUE_REQUIRED,'替换菜单域名为当前域名')
- ->addOption("delay","d",Option::VALUE_REQUIRED,'休眠时间')
- ->addOption('channel',"c",Option::VALUE_REQUIRED,'替换的渠道OR配号代理商例: 1 or 1,2,3,4 or 1-100 or 1-20&30-40')
- ->addOption('params','p',Option::VALUE_REQUIRED,'添加参数例: type=1 or type=1&name=ss')
- ->setDescription('替换微信菜单地址');
- }
- protected function execute(Input $input, Output $output){
- $channel_map = null;
- //cli模式下无法获取到当前的项目模块,手动指定一下
- Request::instance()->module('admin');
- //查找替换
- $find = $input->getOption('find');
- //替换地址
- $replace = $input->getOption('replace');
- //获取休眠时间
- $sleep = $input->getOption('delay');
- //获取渠道ID
- $channel_id = $this->getInputChannelParams($input);
- //获取参数
- parse_str(trim($input->getOption('params'),'&'),$params);
- if($channel_id){
- $channel_map['admin_id'] = ['in',$channel_id];
- }
- $output->info('ChangeMenu -----------------------------------> Start');
- $output->info('Params: find = '.$find);
- $output->info('Params: replace = '.$replace);
- $output->info('Params: sleep = '.$sleep);
- $output->info('Params: channel_id = '.$channel_id);
- $output->info('Params: params = '.var_export($params,true));
- Log::info("ChangeMenu->Params: find:{$find},replace:{$replace},sleep:{$sleep},channel_id:{$channel_id},params:".var_export($params,true));
- if(!$adminConfig = model('AdminConfig')->where($channel_map)->field('admin_id,appid,refresh_token,wx_menu')->select()){
- $output->error('ChangeMenu->Error:渠道OR配号代理商为空');
- return;
- }
- $output->info('ChangeMenu->SQL:'.model('AdminConfig')->getLastSql());
- Log::info('ChangeMenu->SQL:'.model('AdminConfig')->getLastSql());
- foreach($adminConfig as $channel){
- //微信菜单为空时跳过
- if(empty($channel['wx_menu'])){
- $output->info('ChangeMenu->Source->Menu:'.date('Y-m-d H:i:s') . " admin_id:{$channel['admin_id']} Menu Is NULL");
- continue;
- }
- Log::info('ChangeMenu->Source->Menu:'.var_export($channel['wx_menu'],true));
- //为查找替换时,值不匹配时处理
- $channel['wx_menu'] = json_encode($channel['wx_menu'],JSON_UNESCAPED_UNICODE);
- if(!empty($find) && !strpos($channel['wx_menu'],$find)){
- $output->info('ChangeMenu->Find->Replace: '.date('Y-m-d H:i:s') . " admin_id:{$channel['admin_id']} Mismatch");
- Log::info('ChangeMenu->Find->Replace: '.date('Y-m-d H:i:s') . " admin_id:{$channel['admin_id']} Mismatch");
- continue;
- }
- //为查找替换时,替换指定地址
- if($find && $replace){
- $channel['wx_menu'] = str_replace($find,$replace,$channel['wx_menu']);
- }
- $menu = json_decode($channel['wx_menu'],true);
- foreach($menu as $key => &$val){
- if(isset($val['url'])){
- //直接替换顶级域名
- if(empty($find) && $replace){
- if(preg_match("/\/\/wx[A-Za-z0-9]+./i", $val['url'])){
- $val['url'] = $this->replaceDomain($val['url'],$replace);
- }
- // //检测入口域名
- // if($isEntry = $this->checkIsEntryHost($val['url'])){
- // $val['url'] = $this->replaceDomain($val['url'],$replace,$isEntry);
- // }
- }
- //参数不为空时,追加参数
- if($params){
- $val['url'] = $this->addUrlParams($val['url'],$params);
- }
- }
- if(isset($val['sub_button']) && !empty($val['sub_button'])){
- foreach($val['sub_button'] as $sub_k => &$sub_v){
- if(isset($sub_v['url'])){
- //直接替换顶级域名
- if(empty($find) && $replace) {
- if (preg_match("/\/\/wx[A-Za-z0-9]+./i", $sub_v['url'])) {
- $sub_v['url'] = $this->replaceDomain($sub_v['url'], $replace);
- }
- // //检测入口域名
- // if($isEntry = $this->checkIsEntryHost($sub_v['url'])){
- // $sub_v['url'] = $this->replaceDomain($sub_v['url'], $replace,$isEntry);
- // }
- }
- //参数不为空时,追加参数
- if($params){
- $sub_v['url'] = $this->addUrlParams($sub_v['url'],$params);
- }
- }
- }
- }
- }
- $channel['wx_menu'] = $menu;
- Log::info('ChangeMenu->Replace->Menu:'.var_export($channel['wx_menu'],true));
- try {
- //更新菜单
- $admin = model('AdminConfig')->getAdminInfoAll($channel['admin_id']);
- if(empty($admin['refresh_token'])){
- Log::info('ChangeMenu->WeChat->Menu: '.date('Y-m-d H:i:s') . " admin_id:{$channel['admin_id']} refresh_token Empty!!");
- continue;
- }
- $wechat = new WeChatObject($admin);
- $officialAccount = $wechat->getOfficialAccount();
- // $officialAccount->menu->delete(); //删除全部菜单 主要是掌中云个性化菜单
- $ret = $officialAccount->menu->create($channel['wx_menu']);
- if ($ret['errcode'] == 0) {
- $output->info('ChangeMenu->WeChat->Menu: '.date('Y-m-d H:i:s') . " admin_id:{$channel['admin_id']} WeChat Update Success");
- Log::info('ChangeMenu->WeChat->Menu: '.date('Y-m-d H:i:s') . " admin_id:{$channel['admin_id']} WeChat Update Success");
- //更新数据
- $update = model('AdminConfig')->update(['wx_menu' => $channel['wx_menu']], ["admin_id" => $channel['admin_id']]);
- if ($update) {
- $output->info('ChangeMenu->WeChat->Menu: '.date('Y-m-d H:i:s') . " admin_id:{$channel['admin_id']} Mysql Update Success");
- Log::info('ChangeMenu->WeChat->Menu: '.date('Y-m-d H:i:s') . " admin_id:{$channel['admin_id']} Mysql Update Success");
- } else {
- $output->info('ChangeMenu->WeChat->Menu: '.date('Y-m-d H:i:s') . " admin_id:{$channel['admin_id']} Mysql Update Fail");
- Log::error('ChangeMenu->WeChat->Menu: '.date('Y-m-d H:i:s') . " admin_id:{$channel['admin_id']} Mysql Update Fail");
- }
- } else {
- $output->info('ChangeMenu->WeChat->Menu: '.date('Y-m-d H:i:s') . " admin_id:{$channel['admin_id']} WeChat Update Fail Error:" . $ret['errmsg']);
- Log::error('ChangeMenu->WeChat->Menu: '.date('Y-m-d H:i:s') . " admin_id:{$channel['admin_id']} WeChat Update Fail Error:" . $ret['errmsg']);
- }
- //休眠
- if (!empty($sleep) && is_numeric($sleep)) {
- sleep($sleep);
- }
- }catch (\Exception $exception){
- Log::error('ChangeMenu->WeChat->Menu: '.date('Y-m-d H:i:s') . " admin_id:{$channel['admin_id']} WeChat Update Fail Error:" . $exception->getMessage());
- }
- }
- $output->info('ChangeMenu -----------------------------------> End');
- }
- /**
- * 检测是否是入口域名
- * @param $url
- * @return bool
- */
- protected function checkIsEntryHost($url){
- $entryHost = model('Entryhost')->getHosts();
- foreach($entryHost as $host){
- if (preg_match("/{$host}/i", $url)) {
- return true;
- }
- }
- return false;
- }
- /**
- * 替换顶级域名
- * @param $source_url
- * @param $replace
- * @param $isEntry
- * @return string
- */
- protected function replaceDomain($source_url,$replace,$isEntry = false){
- $url = '';
- $url_arr = parse_url($source_url);
- if($isEntry){
- $url .= $url_arr['scheme'].'://'.$replace.($url_arr['path'] ?? '');
- }else{
- $app_id = explode('.',$url_arr['host'])[0];
- $url .= $url_arr['scheme'].'://'.$app_id.'.'.$replace.($url_arr['path'] ?? '');
- }
- if(isset($url_arr['query']) && !empty($url_arr['query'])){
- $url .= '?'.$url_arr['query'];
- }
- return $url;
- }
- /**
- * URL地址追加参数
- * @param $url
- * @param $params
- * @return string
- */
- protected function addUrlParams($url,$params){
- $url_arr = parse_url($url);
- parse_str(($url_arr['query'] ?? ''),$url_params);
- $url_params = array_merge($url_params,$params);
- $url_params = array_filter($url_params,function($val){if($val != '')return true;});
- $url = $url_arr['scheme'].'://'.$url_arr['host'].($url_arr['path']??'');
- if($url_params){
- $url .= '?'.http_build_query($url_params);
- }
- return $url;
- }
- /**
- * 获取渠道ID
- * @param Input $input
- * @return string
- */
- protected function getInputChannelParams(Input $input){
- $channel = $input->getOption('channel');
- $channelIds = [];
- if($channel_params = explode('&',$channel)){
- foreach($channel_params as $val){
- if($index = explode('-',$val)){
- if(isset($index[0]) && isset($index[1]) && !empty($index[0]) && !empty($index[1]) && ($index[0] < $index[1])){
- for($i = $index[0];$i<=$index[1];$i++){
- if(!in_array($i,$channelIds)){
- array_push($channelIds,$i);
- }
- }
- }else{
- if(isset($index[0]) && !empty($index[0])){
- if($ids = explode(',',$index[0])){
- $channelIds = array_merge($channelIds,$ids);
- }
- }
- }
- }
- }
- }
- return implode(',',$channelIds);
- }
- }
|