first(['uid']); return $data ? $this->response($data) : $this->error()->tokenDoesNotExist(); } public function findBy(array $conditions, array $fields){ $this->setLog(self::TYPENAME.'findBy 查找条件', $this->startTime(), $conditions); $result = User::where(function($query) use($conditions){ if (isset($conditions['id'])){ $query->where('id', $conditions['id']); } })->first($fields); return $result ? $this->response($result) : $this->error()->dataDoesNotExist(); } public function wxWebLoginOrReg(string $code, string $ip, $wxUserInfo) { if(!$wxUserInfo) { $accessInfo = WxUserFacade::getAccessToken($code); self::setLog(self::TYPENAME, $this->startTime(), [$accessInfo->getData()->data]); $accessInfo = $accessInfo->getData()->data; //获取微信网页用户信息 $wxUserInfo = WxUserFacade::getUserInfo($accessInfo->access_token, $accessInfo->openid); $wxUserInfo = $wxUserInfo->getData()->data; } $data = [ // 'third_openid' => ThirdScaleFacade::customerCreate($wxUserInfo->nick_name, $wxUserInfo->openid, $wxUserInfo->sex), 'wx_openid' => $wxUserInfo->openid, 'wx_nickname' => $wxUserInfo->nick_name, 'wx_sex' => $wxUserInfo->sex, 'wx_province' => $wxUserInfo->province, 'wx_city' => $wxUserInfo->city, 'wx_country' => $wxUserInfo->country, 'wx_headimgurl' => $wxUserInfo->head_img_url, ]; $has = User::where('wx_openid', $wxUserInfo->openid)->exists(); //第三方统一量表注册用户 if (!$has){ $data['third_openid'] = ThirdScaleFacade::customerCreate($wxUserInfo->nick_name, $wxUserInfo->openid, $wxUserInfo->sex); } $user = User::updateOrCreate(['wx_openid' => $wxUserInfo->openid], $data); $token = $wxUserInfo->openid . config('console.apiAppid') . time(); $log = [ 'uid' => $user->id, 'token' => hash('sha256', $token), 'platform' => UserLoginLog::PLATFORM_1, 'login_ip' => '127.0.0.1', 'role' => UserLoginLog::ROLE_1 ]; $login['log'] = UserLoginLog::create($log); $login['user'] = $user; return $this->response($login); } public function wxMPLogin(string $code) { $login = []; //获取小程序用户openid $sessionInfo = WxUserFacade::getMPSession($code); $accessInfo = $sessionInfo->getData()->data; $user = User::where('wx_openid', $accessInfo->openid)->first(); if($user) { $token = $user->wx_openid . config('console.apiAppid') . time(); $log = [ 'uid' => $user->id, 'token' => hash('sha256', $token), 'platform' => UserLoginLog::PLATFORM_6, 'login_ip' => '127.0.0.1', 'role' => UserLoginLog::ROLE_1 ]; $login['log'] = UserLoginLog::create($log); $login['user'] = $user; } else { return $this->error()->dataDoesNotExist();; } return $this->response($login); } public function wxMPRegister(string $code, array $wxUserInfo) { //获取小程序用户openid $sessionInfo = WxUserFacade::getMPSession($code); $accessInfo = $sessionInfo->getData()->data; $has = User::where('wx_openid', $accessInfo->openid)->exists(); //第三方统一量表注册用户 if (!$has){ $data = [ 'wx_openid' => $accessInfo->openid, 'wx_nickname' => $wxUserInfo['nickName'], 'wx_sex' => $wxUserInfo['gender'], 'wx_province' => $wxUserInfo['province'], 'wx_city' => $wxUserInfo['city'], 'wx_country' => $wxUserInfo['country'], 'wx_headimgurl' => $wxUserInfo['avatarUrl'], ]; $data['third_openid'] = ThirdScaleFacade::customerCreate($wxUserInfo['nickName'], $accessInfo->openid, $wxUserInfo['gender']); } $this->setLog('userinfo',$this->startTime, $data); $user = User::create($data); $token = $accessInfo->openid . config('console.apiAppid') . time(); $log = [ 'uid' => $user->id, 'token' => hash('sha256', $token), 'platform' => UserLoginLog::PLATFORM_6, 'login_ip' => '127.0.0.1', 'role' => UserLoginLog::ROLE_1 ]; $login['log'] = UserLoginLog::create($log); $login['user'] = $user; return $this->response($login); } public function appLoginOrReg(string $username, string $password, string $loginIp, int $platform) { try { //第三方登陆 $thirdLogin = ThirdUpFacade::login($username, $password); //登陆成功后返回的opend_id $thirdOpenid = $thirdLogin->getData()->data->data->open_id; //本站登陆数据 $data = [ 'third_up_openid' => $thirdOpenid, 'third_up_nickname' => $username ]; $has = User::where('third_up_openid', $thirdOpenid)->exists(); //第三方统一量表注册用户 if (!$has){ $thirdScaleOpenId = ThirdScaleFacade::customerCreate($username, $thirdOpenid, 0); $data['third_openid'] = $thirdScaleOpenId; } $user = User::updateOrCreate(['third_up_openid' => $thirdOpenid], $data); $login = $this->_loginLog($thirdOpenid, $user, $platform, $loginIp); return $this->response($login); } catch (\Exception $e) { $this->error()->fail($e->getMessage()); } } private function _loginLog($openId, $user, $platform, $loginIp) { $token = $openId . config('console.apiAppid') . time(); $log = [ 'uid' => $user->id, 'token' => hash('sha256', $token), 'platform' => $platform, 'login_ip' => $loginIp, 'role' => UserLoginLog::ROLE_1 ]; $login['log'] = UserLoginLog::create($log); $login['user'] = $user; return $login; } public function loginOut(string $token) { $result = UserLoginLog::where('token', $token)->delete(); return $result ? $this->response() : $this->error()->fail(); } public function updateBy(array $conditions, array $data) { $result = User::where(function($query) use($conditions){ if (isset($conditions['id'])){ $query->where('id', $conditions['id']); } })->update($data); return $result ? $this->response($result) : $this->error()->fail(); } }