LoginLogic.php 5.9 KB

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