LoginLogic.php 5.9 KB

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