PullWxInfo.Class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * 每分钟执行一次,拉取已授权账号信息,将要过期的信息,刷新
  4. * authorizer_access_token 有效期为 2 小时
  5. * 1.获取已授权账号信息
  6. * 2.若授权即将过期,调用刷新令牌接口,重新获取令牌
  7. * Created by PhpStorm.
  8. * User: XiaoMing
  9. * Date: 2020/4/20
  10. * Time: 14:11
  11. */
  12. namespace JinDouYun\Controller\Cron;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use Mall\Framework\Factory;
  15. use JinDouYun\Dao\Oem\DOem;
  16. use Util\WeiXin\Oplatform;
  17. class PullWxInfo
  18. {
  19. private $oplatformConfigData;
  20. public function __construct()
  21. {
  22. echo date('Y-m-d H:i:s') . ':拉取已授权账号信息' . PHP_EOL;
  23. /*$oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
  24. if (empty($oplatformConfigData)) {
  25. exit('配置错误');
  26. }
  27. $this->oplatformConfigData = $oplatformConfigData;*/
  28. }
  29. /**
  30. * 对所有代理的微信开发平台授权自行自动更新操作
  31. */
  32. public function autoAuthorizerToken()
  33. {
  34. $objDOem = new DOem();
  35. $dbResult = $objDOem->select(' weixinOpenAppid != ""', 'weixinOpen', 'id asc');
  36. if( $dbResult === false ){
  37. echo $dbResult->error();exit();
  38. }
  39. if( empty($dbResult) ){
  40. echo '没有微信开放平台需要刷新';exit();
  41. }
  42. foreach ($dbResult as $key => $value){
  43. $this->oplatformConfigData = json_decode($value['weixinOpen'], true);
  44. self::pullWx();
  45. }
  46. }
  47. /**
  48. * 拉取已授权用户信息,效验过期时间
  49. * @throws \Exception
  50. */
  51. public function pullWx()
  52. {
  53. $objOplatform = new Oplatform($this->oplatformConfigData['appid'], $this->oplatformConfigData['token'], $this->oplatformConfigData['encodingAesKey'], $this->oplatformConfigData['appSecret']);
  54. $pullResult = $objOplatform->apiGetAuthorizerList();
  55. if (!$pullResult->isSuccess()) {
  56. echo $pullResult->getData();
  57. }
  58. $userData = $pullResult->getData();
  59. if (empty($userData)) {
  60. echo '拉取已经授权用户信息为空';
  61. }
  62. if (!isset($userData['list']) || empty($userData['list'])) {
  63. echo '拉取已经授权用户信息为空';
  64. }
  65. $time = time();
  66. foreach ($userData['list'] as $val) {
  67. //获取上次刷新的时间
  68. $expire = Factory::cache('default')->hget('appid_expire', $val['authorizer_appid']);//上次刷新的时间
  69. if ((($expire + 6480) < $time) || empty($expire)) {
  70. //已过期,刷新token
  71. $result = self::refAuth($val);
  72. if ($result->isSuccess()) {
  73. Factory::cache('default')->hset('appid_expire', $val['authorizer_appid'], $time);//本次刷新的时间
  74. echo $val['authorizer_appid'] . '刷新令牌成功'.PHP_EOL;
  75. } else {
  76. echo $val['authorizer_appid'] . '刷新令牌失败' . $result->getData().PHP_EOL;
  77. }
  78. }
  79. }
  80. }
  81. /**
  82. * 刷新令牌
  83. * @param $params
  84. * @return ResultWrapper
  85. */
  86. private function refAuth($params)
  87. {
  88. $objOplatform = new Oplatform($this->oplatformConfigData['appid'], $this->oplatformConfigData['token'], $this->oplatformConfigData['encodingAesKey'], $this->oplatformConfigData['appSecret']);
  89. $result = $objOplatform->apiAuthorizerToken($params['authorizer_appid'], $params['refresh_token']);
  90. if (!$result->isSuccess()) {
  91. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  92. }
  93. return ResultWrapper::success($result->getData());
  94. }
  95. }