wechatUnionidJob.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dingran
  5. * Date: 2018/10/16
  6. * Time: 上午10:24
  7. */
  8. namespace app\Jobs;
  9. use app\backend\modules\member\models\Member;
  10. use app\common\helpers\Cache;
  11. use app\common\models\AccountWechats;
  12. use app\frontend\modules\member\models\MemberUniqueModel;
  13. use Illuminate\Bus\Queueable;
  14. use Illuminate\Contracts\Queue\ShouldQueue;
  15. use Illuminate\Queue\InteractsWithQueue;
  16. use Illuminate\Queue\SerializesModels;
  17. class wechatUnionidJob implements ShouldQueue
  18. {
  19. use InteractsWithQueue, Queueable, SerializesModels;
  20. private $uniacid;
  21. private $member_info;
  22. public function __construct($uniacid, $member_info)
  23. {
  24. $this->uniacid = $uniacid;
  25. $this->member_info = $member_info->toArray();
  26. }
  27. public function handle()
  28. {
  29. \Log::debug('-----queque uniacid-----', $this->uniacid);
  30. return $this->synRun($this->uniacid, $this->member_info);
  31. }
  32. public function synRun($uniacid, $member_info)
  33. {
  34. $account = AccountWechats::getAccountByUniacid($uniacid);
  35. $appId = $account->key;
  36. $appSecret = $account->secret;
  37. $global_access_token_url = $this->_getAccessToken($appId, $appSecret);
  38. $global_token = \Curl::to($global_access_token_url)
  39. ->asJsonResponse(true)
  40. ->get();
  41. return $this->requestWechatApi($uniacid, $member_info, $global_token);
  42. }
  43. private function requestWechatApi($uniacid, $member_info, $global_token)
  44. {
  45. if (!is_null($member_info)) {
  46. \Log::debug('------queque member_info-------');
  47. $time = time();
  48. $path = 'logs/' . $time . '_member_openid.log';
  49. $upgrade_path = 'logs/' . $time . '_upgrade_member_openid.log';
  50. $error_path = 'logs/' . $time . '_error_member_openid.log';
  51. collect($member_info)->map(function($item) use ($uniacid, $global_token, $path, $upgrade_path, $error_path) {
  52. \Log::debug('------queuqe coll-----', $item);
  53. if (!is_null($item)) {
  54. $UnionidInfo = MemberUniqueModel::getUnionidInfoByMemberId($uniacid, $item['uid'])->first();
  55. $this->printLog($path, $item['openid'] . '-' . $item['uid']);
  56. if (is_null($UnionidInfo) && !empty($item['openid'])) {
  57. \Log::debug('----start---', [$item['uid']]);
  58. $ids = Cache::get('queque_wechat_ids') ?: [];
  59. if (in_array($item['uid'], $ids)) {
  60. return $item;
  61. }
  62. $global_userinfo_url = $this->_getInfo($global_token['access_token'], $item['openid']);
  63. $user_info = \Curl::to($global_userinfo_url)
  64. ->asJsonResponse(true)
  65. ->get();
  66. if (isset($user_info['errcode'])) {
  67. \Log::debug('----error---', [$item['uid']]);
  68. $this->printLog($error_path, $item['uid'] . '-' . $user_info['errmsg']);
  69. return ['error' => 1, 'msg' => $user_info['errmsg']];
  70. }
  71. if (isset($user_info['unionid'])) {
  72. MemberUniqueModel::insertData(array(
  73. 'uniacid' => $uniacid,
  74. 'unionid' => $user_info['unionid'],
  75. 'member_id' => $item['uid'],
  76. 'type' => 1
  77. ));
  78. array_push($ids, $item['uid']);
  79. Cache::put('queque_wechat_ids', $ids, 60);
  80. \Log::debug('----insert---', [$item['uid']]);
  81. $this->printLog($upgrade_path, $item['openid'] . '-' . $item['uid']);
  82. }
  83. }
  84. return $item;
  85. }
  86. return $item;
  87. });
  88. Cache::setUniacid($uniacid);
  89. if (Cache::has('queque_wechat_page')) {
  90. \Log::debug('----queque cache1----');
  91. $page = Cache::get('queque_wechat_page');
  92. $page++;
  93. \Log::debug('----queque cache1 page----', $page);
  94. Cache::put('queque_wechat_page', $page, 30);
  95. } else {
  96. \Log::debug('----queque cache2----');
  97. Cache::put('queque_wechat_page', 1, 30);
  98. }
  99. }
  100. }
  101. private function printLog($path, $openid)
  102. {
  103. file_put_contents(storage_path($path), $openid . "\r\n", FILE_APPEND);
  104. }
  105. /**
  106. * 获取全局ACCESS TOKEN
  107. * @return string
  108. */
  109. private function _getAccessToken($appId, $appSecret)
  110. {
  111. return 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appId . '&secret=' . $appSecret;
  112. }
  113. /**
  114. * 获取用户信息
  115. *
  116. * 是否关注公众号
  117. *
  118. * @param $accesstoken
  119. * @param $openid
  120. * @return string
  121. */
  122. private function _getInfo($accesstoken, $openid)
  123. {
  124. return 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $accesstoken . '&openid=' . $openid;
  125. }
  126. }