LoginLogic.php 6.0 KB

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