123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- namespace App\Repositories\Eloquent;
- use App\Repositories\Contracts\UserInterface;
- use App\Models\UserLoginLog;
- use App\Models\User;
- // use App\Facades\WxUserFacade;
- use App\Facades\ThirdScaleFacade;
- use App\Facades\ThirdUpFacade;
- use App\Facades\WxUserFacade;
- /**
- * 用户
- * @author lilin
- *
- */
- class UserFacadeRepository extends BaseRepository implements UserInterface
- {
- public function findLoginBy($token)
- {
- $data = UserLoginLog::where('token', $token)->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();
- }
- }
|