WechatOpen.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\Console\Commands;
  3. use app\backend\modules\member\models\Member;
  4. use app\common\models\AccountWechats;
  5. use app\frontend\modules\member\models\MemberUniqueModel;
  6. use Illuminate\Console\Command;
  7. class WechatOpen extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'syn:wechatUnionid {uniacid}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '微信开发平台同步Unionid';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $uniacid = $this->argument('uniacid');
  38. return $this->synRun($uniacid);
  39. }
  40. private function synRun($uniacid)
  41. {
  42. $member_info = Member::getQueueAllMembersInfo($uniacid);
  43. $account = AccountWechats::getAccountByUniacid($uniacid);
  44. $appId = $account->key;
  45. $appSecret = $account->secret;
  46. $global_access_token_url = $this->_getAccessToken($appId, $appSecret);
  47. $global_token = \Curl::to($global_access_token_url)
  48. ->asJsonResponse(true)
  49. ->get();
  50. return $this->requestWechatApi($uniacid, $member_info, $global_token);
  51. }
  52. private function requestWechatApi($uniacid, $member_info, $global_token)
  53. {
  54. if (!is_null($member_info)) {
  55. $time = time();
  56. $path = 'logs/' . $time . '_member_openid.log';
  57. $upgrade_path = 'logs/' . $time . '_upgrade_member_openid.log';
  58. $error_path = 'logs/' . $time . '_error_member_openid.log';
  59. collect($member_info)->map(function($item) use ($uniacid, $global_token, $path, $upgrade_path, $error_path) {
  60. try {
  61. $item = $item->first();
  62. if (!is_null($item->hasOneFans)) {
  63. $UnionidInfo = MemberUniqueModel::getUnionidInfoByMemberId($item->hasOneFans->uid)->first();
  64. $this->printLog($path, $item->hasOneFans->openid . '-' . $item->hasOneFans->uid);
  65. if (is_null($UnionidInfo) && !empty($item->hasOneFans->openid)) {
  66. \Log::debug('----start---', [$item->yzMember->member_id]);
  67. $global_userinfo_url = $this->_getInfo($global_token['access_token'], $item->hasOneFans->openid);
  68. $user_info = \Curl::to($global_userinfo_url)
  69. ->asJsonResponse(true)
  70. ->get();
  71. if (isset($user_info['errcode'])) {
  72. \Log::debug('----error---', [$item->yzMember->member_id]);
  73. $this->printLog($error_path, $item->yzMember->member_id . '-' . $user_info['errmsg']);
  74. return ['error' => 1, 'msg' => $user_info['errmsg']];
  75. }
  76. if (isset($user_info['unionid'])) {
  77. MemberUniqueModel::insertData(array(
  78. 'uniacid' => $uniacid,
  79. 'unionid' => $user_info['unionid'],
  80. 'member_id' => $item->hasOneFans->uid,
  81. 'type' => 1
  82. ));
  83. \Log::debug('----insert---', [$item->yzMember->member_id]);
  84. $this->printLog($upgrade_path, $item->hasOneFans->openid . '-' . $item->yzMember->member_id);
  85. }
  86. }
  87. }
  88. } catch (\Exception $e) {
  89. throw $e;
  90. }
  91. });
  92. }
  93. }
  94. private function printLog($path, $openid)
  95. {
  96. file_put_contents(storage_path($path), $openid . "\r\n", FILE_APPEND);
  97. }
  98. /**
  99. * 获取全局ACCESS TOKEN
  100. * @return string
  101. */
  102. private function _getAccessToken($appId, $appSecret)
  103. {
  104. return 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appId . '&secret=' . $appSecret;
  105. }
  106. /**
  107. * 获取用户信息
  108. *
  109. * 是否关注公众号
  110. *
  111. * @param $accesstoken
  112. * @param $openid
  113. * @return string
  114. */
  115. private function _getInfo($accesstoken, $openid)
  116. {
  117. return 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $accesstoken . '&openid=' . $openid;
  118. }
  119. }