UserFacadeRepository.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace App\Repositories\Eloquent;
  3. use App\Repositories\Contracts\UserInterface;
  4. use App\Models\UserLoginLog;
  5. use App\Models\User;
  6. // use App\Facades\WxUserFacade;
  7. use App\Facades\ThirdScaleFacade;
  8. use App\Facades\ThirdUpFacade;
  9. use App\Facades\WxUserFacade;
  10. /**
  11. * 用户
  12. * @author lilin
  13. *
  14. */
  15. class UserFacadeRepository extends BaseRepository implements UserInterface
  16. {
  17. public function findLoginBy($token)
  18. {
  19. $data = UserLoginLog::where('token', $token)->first(['uid']);
  20. return $data ? $this->response($data) : $this->error()->tokenDoesNotExist();
  21. }
  22. public function findBy(array $conditions, array $fields){
  23. $this->setLog(self::TYPENAME.'findBy 查找条件', $this->startTime(), $conditions);
  24. $result = User::where(function($query) use($conditions){
  25. if (isset($conditions['id'])){
  26. $query->where('id', $conditions['id']);
  27. }
  28. })->first($fields);
  29. return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
  30. }
  31. public function wxWebLoginOrReg(string $code, string $ip, $wxUserInfo)
  32. {
  33. if(!$wxUserInfo) {
  34. $accessInfo = WxUserFacade::getAccessToken($code);
  35. self::setLog(self::TYPENAME, $this->startTime(), [$accessInfo->getData()->data]);
  36. $accessInfo = $accessInfo->getData()->data;
  37. //获取微信网页用户信息
  38. $wxUserInfo = WxUserFacade::getUserInfo($accessInfo->access_token, $accessInfo->openid);
  39. $wxUserInfo = $wxUserInfo->getData()->data;
  40. }
  41. $data = [
  42. // 'third_openid' => ThirdScaleFacade::customerCreate($wxUserInfo->nick_name, $wxUserInfo->openid, $wxUserInfo->sex),
  43. 'wx_openid' => $wxUserInfo->openid,
  44. 'wx_nickname' => $wxUserInfo->nick_name,
  45. 'wx_sex' => $wxUserInfo->sex,
  46. 'wx_province' => $wxUserInfo->province,
  47. 'wx_city' => $wxUserInfo->city,
  48. 'wx_country' => $wxUserInfo->country,
  49. 'wx_headimgurl' => $wxUserInfo->head_img_url,
  50. ];
  51. $has = User::where('wx_openid', $wxUserInfo->openid)->exists();
  52. //第三方统一量表注册用户
  53. if (!$has){
  54. $data['third_openid'] = ThirdScaleFacade::customerCreate($wxUserInfo->nick_name, $wxUserInfo->openid, $wxUserInfo->sex);
  55. }
  56. $user = User::updateOrCreate(['wx_openid' => $wxUserInfo->openid], $data);
  57. $token = $wxUserInfo->openid . config('console.apiAppid') . time();
  58. $log = [
  59. 'uid' => $user->id,
  60. 'token' => hash('sha256', $token),
  61. 'platform' => UserLoginLog::PLATFORM_1,
  62. 'login_ip' => '127.0.0.1',
  63. 'role' => UserLoginLog::ROLE_1
  64. ];
  65. $login['log'] = UserLoginLog::create($log);
  66. $login['user'] = $user;
  67. return $this->response($login);
  68. }
  69. public function wxMPLogin(string $code)
  70. {
  71. $login = [];
  72. //获取小程序用户openid
  73. $sessionInfo = WxUserFacade::getMPSession($code);
  74. $accessInfo = $sessionInfo->getData()->data;
  75. $user = User::where('wx_openid', $accessInfo->openid)->first();
  76. if($user) {
  77. $token = $user->wx_openid . config('console.apiAppid') . time();
  78. $log = [
  79. 'uid' => $user->id,
  80. 'token' => hash('sha256', $token),
  81. 'platform' => UserLoginLog::PLATFORM_6,
  82. 'login_ip' => '127.0.0.1',
  83. 'role' => UserLoginLog::ROLE_1
  84. ];
  85. $login['log'] = UserLoginLog::create($log);
  86. $login['user'] = $user;
  87. } else {
  88. return $this->error()->dataDoesNotExist();;
  89. }
  90. return $this->response($login);
  91. }
  92. public function wxMPRegister(string $code, array $wxUserInfo)
  93. {
  94. //获取小程序用户openid
  95. $sessionInfo = WxUserFacade::getMPSession($code);
  96. $accessInfo = $sessionInfo->getData()->data;
  97. $has = User::where('wx_openid', $accessInfo->openid)->exists();
  98. //第三方统一量表注册用户
  99. if (!$has){
  100. $data = [
  101. 'wx_openid' => $accessInfo->openid,
  102. 'wx_nickname' => $wxUserInfo['nickName'],
  103. 'wx_sex' => $wxUserInfo['gender'],
  104. 'wx_province' => $wxUserInfo['province'],
  105. 'wx_city' => $wxUserInfo['city'],
  106. 'wx_country' => $wxUserInfo['country'],
  107. 'wx_headimgurl' => $wxUserInfo['avatarUrl'],
  108. ];
  109. $data['third_openid'] = ThirdScaleFacade::customerCreate($wxUserInfo['nickName'], $accessInfo->openid, $wxUserInfo['gender']);
  110. }
  111. $this->setLog('userinfo',$this->startTime, $data);
  112. $user = User::create($data);
  113. $token = $accessInfo->openid . config('console.apiAppid') . time();
  114. $log = [
  115. 'uid' => $user->id,
  116. 'token' => hash('sha256', $token),
  117. 'platform' => UserLoginLog::PLATFORM_6,
  118. 'login_ip' => '127.0.0.1',
  119. 'role' => UserLoginLog::ROLE_1
  120. ];
  121. $login['log'] = UserLoginLog::create($log);
  122. $login['user'] = $user;
  123. return $this->response($login);
  124. }
  125. public function appLoginOrReg(string $username, string $password, string $loginIp, int $platform)
  126. {
  127. try {
  128. //第三方登陆
  129. $thirdLogin = ThirdUpFacade::login($username, $password);
  130. //登陆成功后返回的opend_id
  131. $thirdOpenid = $thirdLogin->getData()->data->data->open_id;
  132. //本站登陆数据
  133. $data = [
  134. 'third_up_openid' => $thirdOpenid,
  135. 'third_up_nickname' => $username
  136. ];
  137. $has = User::where('third_up_openid', $thirdOpenid)->exists();
  138. //第三方统一量表注册用户
  139. if (!$has){
  140. $thirdScaleOpenId = ThirdScaleFacade::customerCreate($username, $thirdOpenid, 0);
  141. $data['third_openid'] = $thirdScaleOpenId;
  142. }
  143. $user = User::updateOrCreate(['third_up_openid' => $thirdOpenid], $data);
  144. $login = $this->_loginLog($thirdOpenid, $user, $platform, $loginIp);
  145. return $this->response($login);
  146. } catch (\Exception $e) {
  147. $this->error()->fail($e->getMessage());
  148. }
  149. }
  150. private function _loginLog($openId, $user, $platform, $loginIp)
  151. {
  152. $token = $openId . config('console.apiAppid') . time();
  153. $log = [
  154. 'uid' => $user->id,
  155. 'token' => hash('sha256', $token),
  156. 'platform' => $platform,
  157. 'login_ip' => $loginIp,
  158. 'role' => UserLoginLog::ROLE_1
  159. ];
  160. $login['log'] = UserLoginLog::create($log);
  161. $login['user'] = $user;
  162. return $login;
  163. }
  164. public function loginOut(string $token)
  165. {
  166. $result = UserLoginLog::where('token', $token)->delete();
  167. return $result ? $this->response() : $this->error()->fail();
  168. }
  169. public function updateBy(array $conditions, array $data)
  170. {
  171. $result = User::where(function($query) use($conditions){
  172. if (isset($conditions['id'])){
  173. $query->where('id', $conditions['id']);
  174. }
  175. })->update($data);
  176. return $result ? $this->response($result) : $this->error()->fail();
  177. }
  178. }