CheckChannelRefreshToken.php 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Created by: PhpStorm
  4. * User: lytian
  5. * Date: 2019/11/28
  6. * Time: 13:59
  7. */
  8. namespace app\admin\command;
  9. use app\common\library\Redis;
  10. use app\common\library\WeChatObject;
  11. use app\main\helper\ArrayHelper;
  12. use EasyWeChat\Kernel\Exceptions\HttpException;
  13. use EasyWeChat\Kernel\Exceptions\RuntimeException;
  14. use think\console\Command;
  15. use think\console\Input;
  16. use think\console\Output;
  17. use think\Log;
  18. use think\Request;
  19. class CheckChannelRefreshToken extends BaseCommand
  20. {
  21. protected function configure()
  22. {
  23. $this->setName('CheckChannelRefreshToken')
  24. ->setDescription('检测渠道的refresh_token');
  25. }
  26. protected function execute(Input $input, Output $output)
  27. {
  28. Request::instance()->module('admin');
  29. $platfromList = model("Platform")->getPlatformList();
  30. foreach ($platfromList as $platform)
  31. {
  32. try {
  33. $openPlatform = (new WeChatObject(null))->getPlatform($platform['id']);
  34. $do = true;
  35. $i = 0;
  36. $limit = 20;
  37. while ($do) {
  38. $offset = $limit * $i;
  39. $result = $openPlatform->getAuthorizers($offset, $limit);
  40. if (isset($result['list']) && $result['list']) {
  41. echo count($result['list']).PHP_EOL;
  42. $rows = ArrayHelper::index($result['list'], 'authorizer_appid');
  43. $appids = array_keys($rows);
  44. $appStr = implode(',', $appids);
  45. $adminInfos = model("AdminConfig")->field("admin_id, appid")->where('appid', 'in', $appStr)->select();
  46. if ($adminInfos) {
  47. foreach ($adminInfos as $adminInfo) {
  48. $maps = [
  49. 'platform_id' => $platform['id'],
  50. 'admin_id' => $adminInfo['admin_id'],
  51. ];
  52. $ptokenRow = model("Ptoken")->where($maps)->find();
  53. if (empty($ptokenRow)) {
  54. continue;
  55. }
  56. if ($ptokenRow['refresh_token'] != $rows[$adminInfo['appid']]['refresh_token']) {
  57. echo "需要刷新记录:admin_id: {$adminInfo['admin_id']} platform_id:{$adminInfo['admin_id']} 记录id:{$ptokenRow['id']}".PHP_EOL;
  58. if (model("Ptoken")->update(['refresh_token' => $rows[$adminInfo['appid']]['refresh_token'], 'updatetime' => time()], ['id' => $ptokenRow['id']])) {
  59. Redis::instance()->del('ANI:'.$adminInfo['admin_id']);
  60. Log::info("refersh_token更新成功 platform_id:{$platform['id']} admin_id:{$adminInfo['admin_id']}");
  61. } else {
  62. Log::info("refersh_token更新失败 platform_id:{$platform['id']} admin_id:{$adminInfo['admin_id']}");
  63. }
  64. }
  65. unset($ptokenRow);
  66. }
  67. }
  68. unset($adminInfos);
  69. unset($rows);
  70. } else {
  71. $do = false;
  72. }
  73. unset($result);
  74. $i++;
  75. }
  76. unset($openPlatform);
  77. } catch (HttpException $e) {
  78. Log::error("三方平台拉取授权公众号列表异常:id:{$platform['id']} msg:".$e->getMessage());
  79. continue;
  80. } catch (RuntimeException $e) {
  81. Log::error("三方平台拉取授权公众号列表异常:id:{$platform['id']} msg:".$e->getMessage());
  82. continue;
  83. }
  84. }
  85. }
  86. }