request->isAjax()) { exit(JsonService::fail($msg, $url)->getContent()); } else { $this->assign(compact('title', 'msg', 'url')); exit($this->fetch('public/error')); } } /** * 操作成功 弹窗提示 * @param $msg * @param int $url */ protected function successful($msg = '操作成功', $url = 0, $title = '成功提醒') { if ($this->request->isAjax()) { exit(JsonService::successful($msg, $url)->getContent()); } else { $this->assign(compact('title', 'msg', 'url')); exit($this->fetch('public/success')); } } public function _empty($name) { $url = strtolower($name) == 'index' ? Url::build('Index/index', '', true, true) : 0; return $this->failed('请求页面不存在!', $url); } /**判断redis状态 * @param $msg */ public function serRedisPwd($msg) { if (strpos($msg, 'username-password pair') !== false) { return $this->failed('请检查redis密码!', 'http://help.crmeb.net/crmeb_zsff/2291680'); } elseif (strpos($msg, 'Connection refused') !== false) { return $this->failed('请安装redis软件或连接被拒绝!'); } elseif (strpos($msg, 'NOAUTH Authentication required') !== false) { return $this->failed('redis认证错误!', 'http://help.crmeb.net/crmeb_zsff/2000690'); } elseif (strpos($msg, 'not support: redis') !== false) { return $this->failed('请安装redis扩展!', 'http://help.crmeb.net/crmeb_zsff/1863590'); } else { return $this->failed('请设置redis密码!', 'http://help.crmeb.net/crmeb_zsff/1907223'); } } protected function oauth($spread_uid = 0, $is_h5 = false) { /* exit($this->failed('微信用户信息获取失败!!')); */ $openid = Session::get('loginOpenid', 'wap'); if($openid&&$is_h5) return WechatUser::where('openid',$openid)->find()['uid']; if ($openid) return $openid; if (!UtilService::isWechatBrowser()) exit($this->failed('请在微信客户端打开链接')); //if ($this->request->isAjax()) exit($this->failed('请登陆!')); $errorNum = (int)Cookie::get('_oen'); if ($errorNum && $errorNum > 3) exit($this->failed('微信用户信息获取失败!!')); try { $original = WechatService::oauthService()->getAccessToken($this->request->get('code')); if (!WechatUser::be(['openid' => $original->openid]) && $original->scope === 'snsapi_base') { exit(WechatService::oauthService()->scopes(['snsapi_userinfo']) ->redirect($this->request->url(true))->send()); } $wechatInfo = WechatSubscribe::baseParseGet($original->access_token, $original->openid); //获取单个用户信息 if (isset($wechatInfo['errcode']) && $wechatInfo['errcode'] == 48001) { if($is_h5){ exit("登陆失败"); } exit(WechatService::oauthService()->scopes(['snsapi_userinfo']) ->redirect($this->request->url(true))->send()); } } catch (\Exception $e) { //Cookie::set('_oen', ++$errorNum, 900); if($is_h5){ exit("登陆失败"); } exit(WechatService::oauthService()->scopes(['snsapi_base']) ->redirect($this->request->url(true))->send()); } if (!isset($wechatInfo['nickname']) || $wechatInfo['nickname'] == '') { exit(WechatService::oauthService()->scopes(['snsapi_userinfo']) ->redirect($this->request->url(true))->send()); } if (isset($wechatInfo['openid']) && $wechatInfo['openid']) { $wechatInfoData = WechatService::getUserInfo($wechatInfo['openid']); $wechatInfo['subscribe'] = $wechatInfoData['subscribe']; $wechatInfo['subscribe_time'] = $wechatInfoData['subscribe_time']; $wechatInfo['groupid'] = $wechatInfoData['groupid']; if (isset($wechatInfoData['tagid_list'])) $wechatInfo['tagid_list'] = implode(',', $wechatInfoData['tagid_list']); } else { if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']); if (!WechatUser::be(['openid' => $wechatInfo['openid']])) $wechatInfo['subscribe'] = 0; } Cookie::delete('_oen'); $openid = $wechatInfo['openid']; $wechatInfo['spread_uid'] = $spread_uid; if (!$is_h5) { HookService::afterListen('wechat_oauth', $openid, $wechatInfo, false, UserBehavior::class); Session::set('loginOpenid', $openid, 'wap'); Cookie::set('is_login', 1); return $openid; }else{ return UserBehavior::wechatOauthAfter($openid, $wechatInfo,true); } } /** * APP微信授权登录 * @param $code * @param $spid * @param $spread * @param $iv * @param $encryptedData * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException */ public function newAuthWxApp($code, $spid, $spread, $iv, $encryptedData) { $userInfoCong=(array)json_decode($encryptedData); $userInfo['unionId'] = isset($userInfoCong['unionId']) ? $userInfoCong['unionId'] : ''; $userInfo['openId'] = $openid = $userInfoCong['openId']; $userInfo['spid'] = $spid; $userInfo['code'] = $spread; $userInfo['login_type'] = 'wxapp'; $userInfo['nickName'] = $userInfoCong['nickName']; $userInfo['avatarUrl'] =$userInfoCong['avatarUrl']; $createData = $this->routineOauth($userInfo); /** @var WechatUserServices $wechatUserServices */ $wechatUserServices = new WechatUser; $user = $wechatUserServices->getAuthUserInfo($openid, 'wxapp'); try { if (!$user) { // 新用户获取后台默认头像 $createData['headimgurl'] =$userInfoCong['avatarUrl']; $createData[1]['headimgurl'] = $userInfoCong['avatarUrl']; // 新用户获取后台默认名称 $createData['nickname'] =$userInfoCong['nickName']; $createData[1]['nickname'] = $userInfoCong['nickName']; $user = $wechatUserServices->wechatOauthAfter($createData); } else { //更新用户信息 $wechatUserServices->wechatUpdata([$user['uid'], $createData[1]]); } $tokenService = new JwtService(); $token['token'] = $tokenService->getToken((int)$user['uid']); if ($token) { /** @var UserVisitServices $visitServices */ /* $visitServices = app()->make(UserVisitServices::class); $visitServices->loginSaveVisit($user); */ $token['userInfo'] = User::where('uid', $user['uid'])->find(); return $token; } else throw new ValidateException('登录失败'); } catch (\Throwable $th) { return $th->getMessage(); } } /** * 小程序授权登录 * @param $code * @param $spid * @param $spread * @param $iv * @param $encryptedData * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException */ public function newAuth($code, $spid, $spread, $iv, $encryptedData,$username,$currentAvatar) { if (!$code) throw new ValidateException('授权失败,参数有误'); $session_key = ''; try { $userInfoCong = MiniProgramService::getUserInfo($code); $session_key = $userInfoCong['session_key']; } catch (\Exception $e) { throw new ValidateException('获取session_key失败,请检查您的配置!:' . $e->getMessage() . 'line' . $e->getLine()); } if (!isset($userInfoCong['openid'])) { throw new ValidateException('openid获取失败'); } try { //解密获取用户信息 $userInfo = MiniProgramService::encryptor($session_key, $iv, $encryptedData); } catch (\Exception $e) { if ($e->getCode() == '-41003') { throw new ValidateException('获取会话密匙失败'); } } if (!isset($userInfoCong['openid'])) { throw new ValidateException('openid获取失败'); } $userInfo['unionId'] = isset($userInfoCong['unionid']) ? $userInfoCong['unionid'] : ''; $userInfo['openId'] = $openid = $userInfoCong['openid']; $userInfo['spid'] = $spid; $userInfo['code'] = $spread; $userInfo['session_key'] = $session_key; $userInfo['login_type'] = 'routine'; $createData = $this->routineOauth($userInfo); /** @var WechatUserServices $wechatUserServices */ $wechatUserServices = new WechatUser; $user = $wechatUserServices->getAuthUserInfo($openid, 'routine'); //获取是否强制绑定手机号 /* $storeUserMobile = sys_config('store_user_mobile'); if ($storeUserMobile && !$user) { $userInfoKey = md5($openid . '_' . time() . '_rouine'); Cache::setTokenBucket($userInfoKey, $createData, 7200); return ['key' => $userInfoKey]; } else */ try { if (!$user) { // // 新用户获取后台默认头像 // $createData['headimgurl'] =SystemConfig::getValue('wechat_login_image'); // $createData[1]['headimgurl'] = SystemConfig::getValue('wechat_login_image'); // // 新用户获取后台默认名称 // $createData['nickname'] =SystemConfig::getValue('login_nickname'); // $createData[1]['nickname'] = SystemConfig::getValue('login_nickname'); // 新用户获取后台默认头像 $createData['headimgurl'] =$currentAvatar; $createData[1]['headimgurl'] = $currentAvatar; // 新用户获取后台默认名称 $createData['nickname'] =$username; $createData[1]['nickname'] = $username; $user = $wechatUserServices->wechatOauthAfter($createData); } else { //更新用户信息 // unset($createData[1]['nickname']); // unset($createData[1]['headimgurl']); if($currentAvatar && $username){ User::where('uid',$user['uid'])->update([ 'avatar' => $currentAvatar, 'nickname' => $username ]); } $wechatUserServices->wechatUpdata([$user['uid'], $createData[1]]); } $tokenService = new JwtService(); $token['token'] = $tokenService->getToken((int)$user['uid']); if ($token) { /** @var UserVisitServices $visitServices */ /* $visitServices = app()->make(UserVisitServices::class); $visitServices->loginSaveVisit($user); */ $token['userInfo'] = User::where('uid', $user['uid'])->find(); return $token; } else throw new ValidateException('登录失败'); } catch (\Throwable $th) { return $th->getMessage(); } } /** * 快手小程序授权登录 * @param $code * @param $spid * @param $spread * @param $iv * @param $encryptedData * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException */ public function newAuth_KuaiShou($code, $spid, $spread, $iv, $encryptedData) { $KuaiMiniProgramService=new KuaiMiniProgramService(); if (!$code) throw new ValidateException('授权失败,参数有误'); $session_key = ''; try { $userInfoCong = $KuaiMiniProgramService->getUserInfo($code); $session_key = $userInfoCong['session_key']; } catch (\Exception $e) { throw new ValidateException('获取session_key失败,请检查您的配置!:' . $e->getMessage() . 'line' . $e->getLine()); } if (!isset($userInfoCong['open_id'])) { throw new ValidateException('openid获取失败'); } try { //解密获取用户信息 $userInfo = KuaiMiniProgramService::encryptor($session_key, $iv, $encryptedData); } catch (\Exception $e) { if ($e->getCode() == '-41003') { throw new ValidateException('获取会话密匙失败'); } } $userInfo['unionId'] = isset($userInfoCong['unionid']) ? $userInfoCong['unionid'] : ''; $userInfo['openId'] = $openid = $userInfoCong['open_id']; $userInfo['spid'] = $spid; $userInfo['code'] = $spread; $userInfo['session_key'] = $session_key; $userInfo['login_type'] = 'kuai_routine'; $createData = $this->routineOauth($userInfo); $wechatUserServices = new KuaishouUser(); $user = $wechatUserServices->getAuthUserInfo($openid, 'kuai_routine'); try { if (!$user) { // 新用户获取后台默认头像 $createData['headimgurl'] = $userInfo['avatarUrl']; $createData[1]['headimgurl'] = $userInfo['avatarUrl']; // 新用户获取后台默认名称 $createData['nickname'] =$userInfo['nickName']; $createData[1]['nickname'] = $userInfo['nickName']; $user = $wechatUserServices->wechatOauthAfter($createData); } else { //更新用户信息 $wechatUserServices->wechatUpdata([$user['uid'], $createData[1]]); } $tokenService = new JwtService(); $token['token'] = $tokenService->getToken((int)$user['uid']); if ($token) { $token['userInfo'] = User::where('uid', $user['uid'])->find(); return $token; } else throw new ValidateException('登录失败'); } catch (\Throwable $th) { return $th->getMessage(); } } /** * 抖音头条小程序授权登录 * @param $code * @param $spid * @param $spread * @param $iv * @param $encryptedData * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException */ public function newAuth_TouTiao($code, $spid, $spread, $iv, $encryptedData) { $TouMiniProgramService=new TouMiniProgramService (); if (!$code) throw new ValidateException('授权失败,参数有误'); $session_key = ''; try { $userInfoCong = $TouMiniProgramService->getUserInfo($code); $session_key = $userInfoCong['session_key']; } catch (\Exception $e) { throw new ValidateException('获取session_key失败,请检查您的配置!:' . $e->getMessage() . 'line' . $e->getLine()); } if (!isset($userInfoCong['openid'])) { throw new ValidateException('openid获取失败'); } try { //解密获取用户信息 $userInfo = TouMiniProgramService::encryptor($session_key, $iv, $encryptedData); } catch (\Exception $e) { if ($e->getCode() == '-41003') { throw new ValidateException('获取会话密匙失败'); } } $userInfo['unionId'] = isset($userInfoCong['unionid']) ? $userInfoCong['unionid'] : ''; $userInfo['openId'] = $openid = $userInfoCong['openid']; $userInfo['spid'] = $spid; $userInfo['code'] = $spread; $userInfo['session_key'] = $session_key; $userInfo['login_type'] = 'kuai_routine'; $createData = $this->routineOauth($userInfo); $toutiaoUser= new ToutiaoUser(); $user = $toutiaoUser->getAuthUserInfo($openid, 'kuai_routine'); try { if (!$user) { // 新用户获取后台默认头像 $createData['headimgurl'] = $userInfo['avatarUrl']; $createData[1]['headimgurl'] = $userInfo['avatarUrl']; // 新用户获取后台默认名称 $createData['nickname'] =$userInfo['nickName']; $createData[1]['nickname'] = $userInfo['nickName']; $user = $toutiaoUser->wechatOauthAfter($createData); } else { //更新用户信息 $toutiaoUser->wechatUpdata([$user['uid'], $createData[1]]); } $tokenService = new JwtService(); $token['token'] = $tokenService->getToken((int)$user['uid']); if ($token) { $token['userInfo'] = User::where('uid', $user['uid'])->find(); return $token; } else throw new ValidateException('登录失败'); } catch (\Throwable $th) { return $th->getMessage(); } } /** * 小程序创建用户后返回uid * @param $routineInfo * @return mixed */ public function routineOauth($routine) { $routineInfo['nickname'] = filter_emoji($routine['nickName']); //姓名 $routineInfo['sex'] =isset($routine['gender'])??0 ; //性别 $routineInfo['language'] = isset($routine['language'])??''; //语言 $routineInfo['city'] = isset($routine['city'])??''; //城市 $routineInfo['province'] =isset( $routine['province'])??''; //省份 $routineInfo['country'] = isset($routine['country'])??''; //国家 $routineInfo['headimgurl'] = $routine['avatarUrl']; //头像 $routineInfo['openid'] = $routine['openId']; $routineInfo['session_key'] =isset($routine['session_key'])??''; //会话密匙 $routineInfo['unionid'] = $routine['unionId']; //用户在开放平台的唯一标识符 $routineInfo['user_type'] = $routine['login_type']; //用户类型 $routineInfo['phone'] = $routine['phone'] ?? $routine['purePhoneNumber'] ?? ''; $spid = $routine['spid'] ?? 0; //绑定关系uid //获取是否有扫码进小程序 /** @var QrcodeServices $qrcode */ /* $qrcode = app()->make(QrcodeServices::class); if (isset($routine['code']) && $routine['code'] && ($info = $qrcode->get($routine['code']))) { $spid = $info['third_id']; } */ return [$routine['openId'], $routineInfo, $spid, $routine['login_type'] ?? 'routine', $routine['login_type'] ?? 'routine']; } }