123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace App\Traits;
- use App\Services\LoginService;
- use App\Services\Eav\EavSetService;
- use App\Services\Eav\MaterialService;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Request;
- use Illuminate\Support\Facades\Validator;
- /**
- * 微信公众号 登录及绑定
- *
- * Trait WechatLoginTrait2
- * @package App\Traits
- */
- trait WechatLoginTrait2
- {
- /**
- * 是否开启登录日志记录
- *
- * @var bool
- */
- private $is_open_login_log = false;
- /**
- * 微信公众号登录,如果已经绑定了,则直接登录,没有绑定则需要绑定手机号
- */
- public function bind()
- {
- $code = Request::input('code');
- $state = Request::input('state');
- if (empty($code) || empty($state)) {
- die();
- }
- // 有可能会返回多次,为了防止这种情况,添加缓存进行解决
- $wechat_cache_key = 'wechat_cache_key2:' . $code;
- if ($url = Cache::get($wechat_cache_key)) {
- Log::info($wechat_cache_key . '已经存在!');
- die();
- } else {
- Cache::put($wechat_cache_key, true, 10);
- }
- $app = $this->getOpenPlatform();
- $officialAccount = $app->officialAccount($this->storeInfo['g_authorizer_appid'], $this->storeInfo['g_authorizer_refresh_token']);
- $userObj = $officialAccount->oauth->userFromCode($code); // 获取 OAuth 授权结果用户信息
- $userArr = $userObj->toArray();
- $wx_unionid = $userArr['raw']['unionid'] ?? '';
- $wx_h5_openid = $userArr['raw']['openid'];
- //根据openid判断该用户是否已经存在,如果存在则登录跳转
- $flag = false;
- $materialService = new MaterialService(new EavSetService($this->memberSetAlias));
- $memberInfo = $materialService->elasticClientService()->queryOne(['store_id' => $this->storeId, 'wx_h5_openid' => $wx_h5_openid]);
- if ($memberInfo) {
- $flag = true;
- } else {
- if ($wx_unionid) {
- $memberInfo = $materialService->elasticClientService()->queryOne(['store_id' => $this->storeId, 'wx_unionid' => $wx_unionid]);
- if ($memberInfo) {
- $flag = true;
- }
- }
- }
- $toUrl = 'https://' . $state . '.tiaotiaoyu168.com';
- if ($flag) {
- // 更新信息
- $materialService->update($memberInfo['id'], ['wx_unionid' => $wx_unionid, 'wx_h5_openid' => $wx_h5_openid, 'wx_info' => json_encode($userArr)]);
- $loginService = new LoginService($this->siteAliasName);
- $encryptArr = $loginService->setLoginCookie($memberInfo);
- //微信登录的时候,保存的微信接口返回的微信用户信息
- $key = 'wechat:userInfo:' . $encryptArr['token'];
- Cache::put($key, $userArr, 7200);
- // 随机值保存,前端通过该值获取登录凭证,防止在url显示登录凭证
- $code = md5(microtime() . mt_rand());
- Cache::put('WX_CODE:' . $code, $encryptArr, 7200);
- $url = toRoute($toUrl . '/pages/public/login_empty?&code=' . $code);
- } else {
- //该用户不存在,要求用户先绑定手机号
- $key = 'wechat:userInfo:' . $code;
- Cache::put($key, $userArr, 7200);
- $url = toRoute($toUrl . '/pages/public/login_empty?&code=' . $code . '&is_need=1');
- }
- Cache::forget($wechat_cache_key);
- return redirect($url);
- }
- /**
- * 微信公账号登录,客户绑定手机号
- */
- public function bindIn()
- {
- $requestData = Request::all();
- $rule = [
- 'key' => 'required',
- 'mobile' => 'required',
- 'sms_code' => 'required',
- ];
- $msg = [
- 'key.required' => '参数错误!',
- 'mobile.required' => '请填写手机号!',
- 'sms_code.required' => '请填写6位短信验证码!',
- 'sms_code.size' => '短信验证码必须是6个字符!',
- ];
- $validator = Validator::make($requestData, $rule, $msg);
- if ($validator->fails()) {
- $errorMessage = $validator->errors()->all();
- return response()->json(['result' => false, 'code' => 2001, 'msg' => $errorMessage[0] ?? '']);
- }
- //判断验证短信验证码是否正确
- $result_bool = $this->smsService->checkSmsCode($requestData['mobile'], $requestData['sms_code']);
- if (!$result_bool) {
- return $this->responseMessage(2002, '短信验证码错误,请确认后再试!');
- }
- //获取微信的信息
- $key2 = 'wechat:userInfo:' . $requestData['key'];
- $weInfo = Cache::get($key2);
- if (empty($weInfo)) {
- return $this->responseMessage(2003, '微信登录失败,请重试!');
- }
- $wx_h5_openid = $weInfo['raw']['openid'];
- $token = $this->saveBindAppMember($weInfo, $requestData['mobile'], '', $wx_h5_openid);
- return $this->responseMessage(1001, 'success', $token);
- }
- }
|