FixUserFollow.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: lytian
  5. * Date: 2019/7/25
  6. * Time: 21:46
  7. */
  8. namespace app\admin\command;
  9. use app\common\library\Redis;
  10. use app\common\library\WeChatObject;
  11. use app\common\model\AdminConfig;
  12. use app\main\constants\CacheConstants;
  13. use app\main\service\AdminService;
  14. use app\main\service\OfficialAccountsService;
  15. use app\main\service\OpenPlatformService;
  16. use app\main\service\UserService;
  17. use EasyWeChat\Kernel\Messages\Text;
  18. use think\console\Command;
  19. use think\console\Input;
  20. use think\console\input\Option;
  21. use think\console\Output;
  22. use think\Exception;
  23. use think\Request;
  24. class FixUserFollow extends Command
  25. {
  26. protected function configure()
  27. {
  28. $this->setName('fixUserFollow')
  29. ->setDescription('修复用户的关注状态');
  30. }
  31. protected function execute(Input $input, Output $output)
  32. {
  33. Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
  34. $filename = APP_PATH."1111.txt";
  35. $handle = fopen ($filename, "r");
  36. $adminIds = [];
  37. $i = 1;
  38. while (!feof($handle)) {
  39. $buffer = fgets($handle, 4096);
  40. $content = trim($buffer);
  41. if (empty($content)) {
  42. break;
  43. }
  44. try {
  45. $arr = explode(' ', $content);
  46. $appid = $arr[0];
  47. $openid = $arr[1];
  48. //查询admin_id
  49. if (!isset($adminIds[$appid])) {
  50. $adminInfo = AdminService::instance()
  51. ->getAdminConfigModel()
  52. ->where('appid', $appid)
  53. ->find();
  54. $adminIds[$appid] = $adminInfo['admin_id'];
  55. }
  56. $admin_id = $adminIds[$appid];
  57. $userId = OfficialAccountsService::instance()
  58. ->getOpenidModel()
  59. ->getUserId($admin_id, $openid);
  60. if ($userId) {
  61. if (
  62. UserService::instance()
  63. ->getUserModel()
  64. ->setConnect($userId)
  65. ->update(
  66. ['is_subscribe' => 1, 'subscribe_time' => time()],
  67. ['id' => $userId])
  68. ) {
  69. $userKey = CacheConstants::getUserCacheKey($userId);
  70. Redis::instance()->del($userKey);
  71. //发送客服消息
  72. $adminConfig = new AdminConfig();
  73. $adminInfo = $adminConfig->getAdminInfoAll($admin_id);
  74. $wechat = new WeChatObject($adminInfo);
  75. $officialAccount = $wechat->getOfficialAccount();
  76. $host = AdminService::instance()->getAdminReferralDomain($admin_id)->data;
  77. $msg = '<a href="https://'.$appid.'.'.$host["menu_ophost_host"].'/index/user/recent">最近阅读链接</a>';
  78. $result = $officialAccount
  79. ->customer_service
  80. ->message(new Text($msg))
  81. ->to($openid)
  82. ->send();
  83. echo "第{$i}行--appid:{$appid} --openid:{$openid} --处理结果".json_encode($result, JSON_UNESCAPED_UNICODE);
  84. $adminConfig->getConnection()->free();
  85. $adminConfig->getConnection()->close();
  86. unset($adminConfig);
  87. }
  88. }
  89. }catch (Exception $e) {
  90. echo "第{$i}行--appid:{$appid} --openid:{$openid} --处理结果:".$e->getMessage();
  91. }
  92. $i++;
  93. }
  94. fclose ($handle);
  95. $output->writeln("\n处理完毕!");
  96. }
  97. }