EntryhostMenu.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * 更改入口域名,批量更改入口域名下的admin_config菜单,更换成默认的业务域名
  4. */
  5. namespace app\admin\command;
  6. use app\common\library\Redis;
  7. use app\common\library\WeChatObject;
  8. use EasyWeChat\Factory;
  9. use Symfony\Component\Cache\Simple\RedisCache;
  10. use think\console\Command;
  11. use think\console\Input;
  12. use think\console\input\Option;
  13. use think\console\Output;
  14. use app\common\model\AdminConfig;
  15. use app\common\model\Platform;
  16. use app\common\model\Ophost;
  17. use app\common\model\Ptoken;
  18. use think\Log;
  19. use think\Exception;
  20. use think\Request;
  21. class EntryhostMenu extends Command
  22. {
  23. protected $message = '';
  24. protected function configure()
  25. {
  26. $this->setName('EntryhostMenu')
  27. ->addOption("find","f",Option::VALUE_REQUIRED)
  28. ->addOption("entryhostid","e",Option::VALUE_REQUIRED)
  29. ->addOption("delay","d",Option::VALUE_REQUIRED)
  30. ->setDescription('更改入口域名替换微信菜单地址');
  31. }
  32. //替换微信菜单地址
  33. protected function execute(Input $input, Output $output)
  34. {
  35. Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
  36. $find = $input->getOption('find');
  37. $entryhostid = $input->getOption('entryhostid');
  38. $delay = $input->getOption('delay');
  39. if ($find === null || !is_string($find)) {
  40. throw new Exception('Please input correct find type');
  41. }
  42. if ($entryhostid === null) {
  43. throw new Exception('Please input correct replace type');
  44. }
  45. $entryhostid = intval($entryhostid);
  46. //获取列表
  47. $admin_config = new AdminConfig();
  48. $ophostModel = new Ophost();
  49. $platformModel = new Platform();
  50. $ptokenModel = new Ptoken();
  51. $adminlist = $admin_config
  52. ->field('admin_id,appid,wx_menu')
  53. ->where('entryhost_id',$entryhostid)
  54. ->select();
  55. if($adminlist){
  56. foreach ($adminlist as $v) {
  57. if(empty($v['wx_menu'])){
  58. continue;
  59. }
  60. //替换关键字
  61. $v['wx_menu'] = json_encode($v['wx_menu'],JSON_UNESCAPED_UNICODE);
  62. if(!strpos($v['wx_menu'],$find)){
  63. Log::info(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 无匹配字符");
  64. $output->writeln(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 无匹配字符");
  65. continue;
  66. }
  67. try {
  68. $info = $admin_config->where('admin_id',$v['admin_id'])->find()->toArray();
  69. $pid = '';
  70. if(!empty($info['platform_id'])){
  71. $pid = $info['platform_id'];
  72. }
  73. $platformInfo = $platformModel->getInfo($pid);
  74. $platformId = $platformInfo['id']; //得到默认的三方平台ID
  75. $refresh_token = $ptokenModel->getToken($platformId,$v['admin_id']);
  76. if(empty($refresh_token)){
  77. Log::info(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 无refresh_token");
  78. continue;
  79. }
  80. $ophostInfo = $ophostModel->getInfo($platformId);
  81. $ophost_host = $ophostInfo['host'];
  82. $replace = $info['appid'].'.'.$ophost_host;
  83. $v['wx_menu'] = str_replace($find,$replace,$v['wx_menu']);
  84. $v['wx_menu'] = json_decode($v['wx_menu'],true);
  85. //更新菜单
  86. $adminConfig = model("AdminConfig")->getAdminInfoAll($v['admin_id']);
  87. $wechat = new WeChatObject($adminConfig);
  88. $officialAccount = $wechat->getOfficialAccountByPlatform($pid,$info['appid'], $refresh_token);
  89. // $officialAccount->menu->delete(); //删除全部菜单 主要是掌中云个性化菜单
  90. $ret = $officialAccount->menu->create($v['wx_menu']);
  91. if ($ret['errcode'] == 0) {
  92. Log::info(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 完成");
  93. $output->writeln(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 完成");
  94. //更新数据
  95. $update = $admin_config->update(['wx_menu' => $v['wx_menu']], ["admin_id" => $v['admin_id']]);
  96. if ($update) {
  97. model('AdminConfig')->delAdminInfoAllCache($v['admin_id']);
  98. Log::info(date('Y-m-d H:i:s') . "替换wx_menu admin_id:{$v['admin_id']} 更新成功");
  99. $output->writeln(date('Y-m-d H:i:s') . "替换wx_menu admin_id:{$v['admin_id']} 更新成功");
  100. } else {
  101. Log::error(date('Y-m-d H:i:s') . "替换wx_menu admin_id:{$v['admin_id']} 更新失败");
  102. $output->writeln(date('Y-m-d H:i:s') . "替换wx_menu admin_id:{$v['admin_id']} 更新失败");
  103. }
  104. } else {
  105. Log::error(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 失败 " . $ret['errmsg']);
  106. $output->writeln(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 失败 " . $ret['errmsg']);
  107. }
  108. if (!empty($delay) && is_numeric($delay)) {
  109. sleep($delay);
  110. }
  111. }catch (\Exception $exception){
  112. Log::error(date('Y-m-d H:i:s') . "微信菜单创建 admin_id:{$v['admin_id']} 失败 触发异常:" . $exception->getMessage());
  113. }
  114. }
  115. }
  116. Log::info(date('Y-m-d H:i:s')."admin_config 替换 wx_menu 处理结束");
  117. $output->writeln(date('Y-m-d H:i:s')."admin_config 替换 wx_menu 处理结束");
  118. }
  119. }