LoginLogic.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace app\workerapi\logic;
  3. use app\common\enum\LoginEnum;
  4. use app\common\enum\notice\NoticeEnum;
  5. use app\common\enum\user\UserTerminalEnum;
  6. use app\common\logic\BaseLogic;
  7. use app\common\model\master_worker\MasterWorker;
  8. use app\common\model\master_worker\MasterWorkerAuth;
  9. use app\common\model\master_worker_register\MasterWorkerRegister;
  10. use app\common\service\FileService;
  11. use app\common\service\sms\SmsDriver;
  12. use app\common\service\wechat\WeChatMnpService;
  13. use think\facade\Config;
  14. use app\workerapi\service\MasterWokerTokenService;
  15. /**
  16. * @author 林海涛
  17. * @date ${DATA}
  18. */
  19. class LoginLogic extends BaseLogic
  20. {
  21. /**
  22. * @notes 确认手机号
  23. * @param $params
  24. * @return bool
  25. * @author 段誉
  26. * @date 2022/9/21 17:28
  27. */
  28. public static function confirmMobile(array $params)
  29. {
  30. try {
  31. // 验证码请求
  32. $sceneId = NoticeEnum::GCSSJHM_CAPTCHA;
  33. // 校验短信
  34. $checkSmsCode = (new SmsDriver())->verify($params['mobile'], $params['code'], $sceneId);
  35. if (!$checkSmsCode) {
  36. throw new \Exception('验证码错误');
  37. }
  38. return true;
  39. } catch (\Exception $e) {
  40. self::setError($e->getMessage());
  41. return false;
  42. }
  43. }
  44. public static function register(array $params)
  45. {
  46. try {
  47. MasterWorkerRegister::create([
  48. 'maintain_exp_type' => $params['maintain_exp_type'],
  49. 'other_exp_type' => $params['other_exp_type'],
  50. 'city' => $params['city'],
  51. 'vehicle_type' => $params['vehicle_type'],
  52. 'name' => $params['name'],
  53. 'age' => $params['age'],
  54. 'mobile' => $params['mobile'],
  55. ]);
  56. return true;
  57. } catch (\Exception $e) {
  58. self::setError($e->getMessage());
  59. return false;
  60. }
  61. }
  62. public static function login($params)
  63. {
  64. try {
  65. // 账号/手机号 密码登录
  66. $where = ['account' => $params['account']];
  67. if ($params['scene'] == LoginEnum::MOBILE_CAPTCHA) {
  68. //手机验证码登录
  69. $where = ['mobile' => $params['account']];
  70. }
  71. $user = MasterWorker::where($where)->findOrEmpty();
  72. if ($user->isEmpty()) {
  73. throw new \Exception('用户不存在');
  74. }
  75. //更新登录信息
  76. $user->login_time = time();
  77. $user->login_ip = request()->ip();
  78. $user->save();
  79. //设置token
  80. $userInfo = MasterWokerTokenService::setToken($user->id, 1);
  81. //返回登录信息
  82. $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
  83. $avatar = FileService::getFileUrl($avatar);
  84. return [
  85. 'nickname' => $userInfo['nickname'],
  86. 'sn' => $userInfo['sn'],
  87. 'mobile' => $userInfo['mobile'],
  88. 'avatar' => $avatar,
  89. 'token' => $userInfo['token'],
  90. ];
  91. } catch (\Exception $e) {
  92. self::setError($e->getMessage());
  93. return false;
  94. }
  95. }
  96. public static function logout($userInfo)
  97. {
  98. //token不存在,不注销
  99. if (!isset($userInfo['token'])) {
  100. return false;
  101. }
  102. //设置token过期
  103. return MasterWokerTokenService::expireToken($userInfo['token']);
  104. }
  105. public static function mnpAuthLogin($params)
  106. {
  107. try {
  108. //通过code获取微信openid
  109. $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
  110. $response['user_id'] = $params['user_id'];
  111. $response['terminal'] = UserTerminalEnum::WECHAT_MMP;
  112. return self::createAuth($response);
  113. } catch (\Exception $e) {
  114. self::$error = $e->getMessage();
  115. return false;
  116. }
  117. }
  118. /**
  119. * @notes 生成授权记录
  120. * @param $response
  121. * @return bool
  122. * @throws \Exception
  123. * @author 段誉
  124. * @date 2022/9/16 10:43
  125. */
  126. public static function createAuth($response)
  127. {
  128. //先检查openid是否有记录
  129. $isAuth = MasterWorkerAuth::where('openid', '=', $response['openid'])->findOrEmpty();
  130. if (!$isAuth->isEmpty()) {
  131. if($isAuth->user_id != $response['user_id']) {
  132. throw new \Exception('该微信已被绑定');
  133. }
  134. if($isAuth->user_id == 0) {
  135. //更新操作
  136. $isAuth->user_id = $response['user_id'];
  137. $isAuth->save();
  138. return true;
  139. }
  140. if($isAuth->user_id == $response['user_id']) {
  141. return true;
  142. }
  143. }
  144. if (isset($response['unionid']) && !empty($response['unionid'])) {
  145. //在用unionid找记录,防止生成两个账号,同个unionid的问题
  146. $userAuth = MasterWorkerAuth::where(['unionid' => $response['unionid']])
  147. ->findOrEmpty();
  148. if (!$userAuth->isEmpty() && $userAuth->user_id != $response['user_id']) {
  149. throw new \Exception('该微信已被绑定');
  150. }
  151. }
  152. //如果没有授权,直接生成一条微信授权记录
  153. MasterWorkerAuth::create([
  154. 'user_id' => $response['user_id'],
  155. 'openid' => $response['openid'],
  156. 'unionid' => $response['unionid'] ?? '',
  157. 'terminal' => $response['terminal'],
  158. ]);
  159. return true;
  160. }
  161. }