LoginLogic.php 5.6 KB

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