WechatLoginTrait2.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App\Traits;
  3. use App\Services\LoginService;
  4. use App\Services\Eav\EavSetService;
  5. use App\Services\Eav\MaterialService;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Request;
  9. use Illuminate\Support\Facades\Validator;
  10. /**
  11. * 微信公众号 登录及绑定
  12. *
  13. * Trait WechatLoginTrait2
  14. * @package App\Traits
  15. */
  16. trait WechatLoginTrait2
  17. {
  18. /**
  19. * 是否开启登录日志记录
  20. *
  21. * @var bool
  22. */
  23. private $is_open_login_log = false;
  24. /**
  25. * 微信公众号登录,如果已经绑定了,则直接登录,没有绑定则需要绑定手机号
  26. */
  27. public function bind()
  28. {
  29. $code = Request::input('code');
  30. $state = Request::input('state');
  31. if (empty($code) || empty($state)) {
  32. die();
  33. }
  34. // 有可能会返回多次,为了防止这种情况,添加缓存进行解决
  35. $wechat_cache_key = 'wechat_cache_key2:' . $code;
  36. if ($url = Cache::get($wechat_cache_key)) {
  37. Log::info($wechat_cache_key . '已经存在!');
  38. die();
  39. } else {
  40. Cache::put($wechat_cache_key, true, 10);
  41. }
  42. $app = $this->getOpenPlatform();
  43. $officialAccount = $app->officialAccount($this->storeInfo['g_authorizer_appid'], $this->storeInfo['g_authorizer_refresh_token']);
  44. $userObj = $officialAccount->oauth->userFromCode($code); // 获取 OAuth 授权结果用户信息
  45. $userArr = $userObj->toArray();
  46. $wx_unionid = $userArr['raw']['unionid'] ?? '';
  47. $wx_h5_openid = $userArr['raw']['openid'];
  48. //根据openid判断该用户是否已经存在,如果存在则登录跳转
  49. $flag = false;
  50. $materialService = new MaterialService(new EavSetService($this->memberSetAlias));
  51. $memberInfo = $materialService->elasticClientService()->queryOne(['store_id' => $this->storeId, 'wx_h5_openid' => $wx_h5_openid]);
  52. if ($memberInfo) {
  53. $flag = true;
  54. } else {
  55. if ($wx_unionid) {
  56. $memberInfo = $materialService->elasticClientService()->queryOne(['store_id' => $this->storeId, 'wx_unionid' => $wx_unionid]);
  57. if ($memberInfo) {
  58. $flag = true;
  59. }
  60. }
  61. }
  62. $toUrl = 'https://' . $state . '.tiaotiaoyu168.com';
  63. if ($flag) {
  64. // 更新信息
  65. $materialService->update($memberInfo['id'], ['wx_unionid' => $wx_unionid, 'wx_h5_openid' => $wx_h5_openid, 'wx_info' => json_encode($userArr)]);
  66. $loginService = new LoginService($this->siteAliasName);
  67. $encryptArr = $loginService->setLoginCookie($memberInfo);
  68. //微信登录的时候,保存的微信接口返回的微信用户信息
  69. $key = 'wechat:userInfo:' . $encryptArr['token'];
  70. Cache::put($key, $userArr, 7200);
  71. // 随机值保存,前端通过该值获取登录凭证,防止在url显示登录凭证
  72. $code = md5(microtime() . mt_rand());
  73. Cache::put('WX_CODE:' . $code, $encryptArr, 7200);
  74. $url = toRoute($toUrl . '/pages/public/login_empty?&code=' . $code);
  75. } else {
  76. //该用户不存在,要求用户先绑定手机号
  77. $key = 'wechat:userInfo:' . $code;
  78. Cache::put($key, $userArr, 7200);
  79. $url = toRoute($toUrl . '/pages/public/login_empty?&code=' . $code . '&is_need=1');
  80. }
  81. Cache::forget($wechat_cache_key);
  82. return redirect($url);
  83. }
  84. /**
  85. * 微信公账号登录,客户绑定手机号
  86. */
  87. public function bindIn()
  88. {
  89. $requestData = Request::all();
  90. $rule = [
  91. 'key' => 'required',
  92. 'mobile' => 'required',
  93. 'sms_code' => 'required',
  94. ];
  95. $msg = [
  96. 'key.required' => '参数错误!',
  97. 'mobile.required' => '请填写手机号!',
  98. 'sms_code.required' => '请填写6位短信验证码!',
  99. 'sms_code.size' => '短信验证码必须是6个字符!',
  100. ];
  101. $validator = Validator::make($requestData, $rule, $msg);
  102. if ($validator->fails()) {
  103. $errorMessage = $validator->errors()->all();
  104. return response()->json(['result' => false, 'code' => 2001, 'msg' => $errorMessage[0] ?? '']);
  105. }
  106. //判断验证短信验证码是否正确
  107. $result_bool = $this->smsService->checkSmsCode($requestData['mobile'], $requestData['sms_code']);
  108. if (!$result_bool) {
  109. return $this->responseMessage(2002, '短信验证码错误,请确认后再试!');
  110. }
  111. //获取微信的信息
  112. $key2 = 'wechat:userInfo:' . $requestData['key'];
  113. $weInfo = Cache::get($key2);
  114. if (empty($weInfo)) {
  115. return $this->responseMessage(2003, '微信登录失败,请重试!');
  116. }
  117. $wx_h5_openid = $weInfo['raw']['openid'];
  118. $token = $this->saveBindAppMember($weInfo, $requestData['mobile'], '', $wx_h5_openid);
  119. return $this->responseMessage(1001, 'success', $token);
  120. }
  121. }