123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /*
- * 更改入口域名,批量更改入口域名下的admin_config菜单,更换成默认的业务域名
- */
- 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\console\Command;
- use think\console\Input;
- use think\console\input\Option;
- use think\console\Output;
- use app\common\model\AdminConfig;
- use app\common\model\Platform;
- use app\common\model\Ophost;
- use app\common\model\Ptoken;
- use think\Log;
- use think\Exception;
- use think\Request;
- class EntryhostMenu extends Command
- {
- protected $message = '';
- protected function configure()
- {
- $this->setName('EntryhostMenu')
- ->addOption("find","f",Option::VALUE_REQUIRED)
- ->addOption("entryhostid","e",Option::VALUE_REQUIRED)
- ->addOption("delay","d",Option::VALUE_REQUIRED)
- ->setDescription('更改入口域名替换微信菜单地址');
- }
- //替换微信菜单地址
- protected function execute(Input $input, Output $output)
- {
- Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
- $find = $input->getOption('find');
- $entryhostid = $input->getOption('entryhostid');
- $delay = $input->getOption('delay');
- if ($find === null || !is_string($find)) {
- throw new Exception('Please input correct find type');
- }
- if ($entryhostid === null) {
- throw new Exception('Please input correct replace type');
- }
- $entryhostid = intval($entryhostid);
- //获取列表
- $admin_config = new AdminConfig();
- $ophostModel = new Ophost();
- $platformModel = new Platform();
- $ptokenModel = new Ptoken();
- $adminlist = $admin_config
- ->field('admin_id,appid,wx_menu')
- ->where('entryhost_id',$entryhostid)
- ->select();
- if($adminlist){
- foreach ($adminlist as $v) {
- if(empty($v['wx_menu'])){
- continue;
- }
-
- //替换关键字
- $v['wx_menu'] = json_encode($v['wx_menu'],JSON_UNESCAPED_UNICODE);
- if(!strpos($v['wx_menu'],$find)){
- Log::info(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 无匹配字符");
- $output->writeln(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 无匹配字符");
- continue;
- }
- try {
- $info = $admin_config->where('admin_id',$v['admin_id'])->find()->toArray();
- $pid = '';
- if(!empty($info['platform_id'])){
- $pid = $info['platform_id'];
- }
- $platformInfo = $platformModel->getInfo($pid);
- $platformId = $platformInfo['id']; //得到默认的三方平台ID
- $refresh_token = $ptokenModel->getToken($platformId,$v['admin_id']);
- if(empty($refresh_token)){
- Log::info(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 无refresh_token");
- continue;
- }
- $ophostInfo = $ophostModel->getInfo($platformId);
- $ophost_host = $ophostInfo['host'];
- $replace = $info['appid'].'.'.$ophost_host;
- $v['wx_menu'] = str_replace($find,$replace,$v['wx_menu']);
- $v['wx_menu'] = json_decode($v['wx_menu'],true);
- //更新菜单
- $adminConfig = model("AdminConfig")->getAdminInfoAll($v['admin_id']);
- $wechat = new WeChatObject($adminConfig);
- $officialAccount = $wechat->getOfficialAccountByPlatform($pid,$info['appid'], $refresh_token);
- // $officialAccount->menu->delete(); //删除全部菜单 主要是掌中云个性化菜单
- $ret = $officialAccount->menu->create($v['wx_menu']);
- if ($ret['errcode'] == 0) {
- Log::info(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 完成");
- $output->writeln(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 完成");
- //更新数据
- $update = $admin_config->update(['wx_menu' => $v['wx_menu']], ["admin_id" => $v['admin_id']]);
- if ($update) {
- model('AdminConfig')->delAdminInfoAllCache($v['admin_id']);
- Log::info(date('Y-m-d H:i:s') . "替换wx_menu admin_id:{$v['admin_id']} 更新成功");
- $output->writeln(date('Y-m-d H:i:s') . "替换wx_menu admin_id:{$v['admin_id']} 更新成功");
- } else {
- Log::error(date('Y-m-d H:i:s') . "替换wx_menu admin_id:{$v['admin_id']} 更新失败");
- $output->writeln(date('Y-m-d H:i:s') . "替换wx_menu admin_id:{$v['admin_id']} 更新失败");
- }
- } else {
- Log::error(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 失败 " . $ret['errmsg']);
- $output->writeln(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 失败 " . $ret['errmsg']);
- }
- if (!empty($delay) && is_numeric($delay)) {
- sleep($delay);
- }
- }catch (\Exception $exception){
- Log::error(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 失败 触发异常:" . $exception->getMessage());
- }
- }
- }
- Log::info(date('Y-m-d H:i:s')."admin_config 替换 wx_menu 处理结束");
- $output->writeln(date('Y-m-d H:i:s')."admin_config 替换 wx_menu 处理结束");
- }
- }
|