1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- /**
- * Created by: PhpStorm
- * User: lytian
- * Date: 2019/11/28
- * Time: 13:59
- */
- namespace app\admin\command;
- use app\common\library\Redis;
- use app\common\library\WeChatObject;
- use app\main\helper\ArrayHelper;
- use EasyWeChat\Kernel\Exceptions\HttpException;
- use EasyWeChat\Kernel\Exceptions\RuntimeException;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\Log;
- use think\Request;
- class CheckChannelRefreshToken extends BaseCommand
- {
- protected function configure()
- {
- $this->setName('CheckChannelRefreshToken')
- ->setDescription('检测渠道的refresh_token');
- }
- protected function execute(Input $input, Output $output)
- {
- Request::instance()->module('admin');
- $platfromList = model("Platform")->getPlatformList();
- foreach ($platfromList as $platform)
- {
- try {
- $openPlatform = (new WeChatObject(null))->getPlatform($platform['id']);
- $do = true;
- $i = 0;
- $limit = 20;
- while ($do) {
- $offset = $limit * $i;
- $result = $openPlatform->getAuthorizers($offset, $limit);
- if (isset($result['list']) && $result['list']) {
- echo count($result['list']).PHP_EOL;
- $rows = ArrayHelper::index($result['list'], 'authorizer_appid');
- $appids = array_keys($rows);
- $appStr = implode(',', $appids);
- $adminInfos = model("AdminConfig")->field("admin_id, appid")->where('appid', 'in', $appStr)->select();
- if ($adminInfos) {
- foreach ($adminInfos as $adminInfo) {
- $maps = [
- 'platform_id' => $platform['id'],
- 'admin_id' => $adminInfo['admin_id'],
- ];
- $ptokenRow = model("Ptoken")->where($maps)->find();
- if (empty($ptokenRow)) {
- continue;
- }
- if ($ptokenRow['refresh_token'] != $rows[$adminInfo['appid']]['refresh_token']) {
- echo "需要刷新记录:admin_id: {$adminInfo['admin_id']} platform_id:{$adminInfo['admin_id']} 记录id:{$ptokenRow['id']}".PHP_EOL;
- if (model("Ptoken")->update(['refresh_token' => $rows[$adminInfo['appid']]['refresh_token'], 'updatetime' => time()], ['id' => $ptokenRow['id']])) {
- Redis::instance()->del('ANI:'.$adminInfo['admin_id']);
- Log::info("refersh_token更新成功 platform_id:{$platform['id']} admin_id:{$adminInfo['admin_id']}");
- } else {
- Log::info("refersh_token更新失败 platform_id:{$platform['id']} admin_id:{$adminInfo['admin_id']}");
- }
- }
- unset($ptokenRow);
- }
- }
- unset($adminInfos);
- unset($rows);
- } else {
- $do = false;
- }
- unset($result);
- $i++;
- }
- unset($openPlatform);
- } catch (HttpException $e) {
- Log::error("三方平台拉取授权公众号列表异常:id:{$platform['id']} msg:".$e->getMessage());
- continue;
- } catch (RuntimeException $e) {
- Log::error("三方平台拉取授权公众号列表异常:id:{$platform['id']} msg:".$e->getMessage());
- continue;
- }
- }
- }
- }
|