LoginLogic.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\MasterWorkerInfo;
  11. use app\common\model\master_worker_register\MasterWorkerRegister;
  12. use app\common\service\FileService;
  13. use app\common\service\sms\SmsDriver;
  14. use app\common\service\wechat\WeChatMnpService;
  15. use app\common\service\wechat\WorkerWeChatMnpService;
  16. use think\facade\Config;
  17. use app\workerapi\service\MasterWokerTokenService;
  18. use think\facade\Log;
  19. /**
  20. * @author 林海涛
  21. * @date ${DATA}
  22. */
  23. class LoginLogic extends BaseLogic
  24. {
  25. /**
  26. * @notes 确认手机号
  27. * @param $params
  28. * @return bool
  29. * @author 段誉
  30. * @date 2022/9/21 17:28
  31. */
  32. public static function confirmMobile(array $params)
  33. {
  34. try {
  35. // 验证码请求
  36. $sceneId = NoticeEnum::GCSSJHM_CAPTCHA;
  37. // 校验短信
  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. $is_id_card = MasterWorkerInfo::where('worker_id',$user->id)->findOrEmpty()->toArray();
  90. //监测是否签署服务合作协议
  91. $pdf = MasterWorkerAgree::where(['agree_type'=>'master_service_content','worker_id'=>$user->id])->value('pdf_url');
  92. return [
  93. 'nickname' => $userInfo['nickname'],
  94. 'sn' => $userInfo['sn'],
  95. 'mobile' => $userInfo['mobile'],
  96. 'avatar' => $avatar,
  97. 'token' => $userInfo['token'],
  98. 'is_id_card'=>!empty($is_id_card)?1:0,
  99. 'is_service_agree'=>!empty($pdf)?1:0
  100. ];
  101. } catch (\Exception $e) {
  102. self::setError($e->getMessage());
  103. return false;
  104. }
  105. }
  106. public static function logout($userInfo)
  107. {
  108. //token不存在,不注销
  109. if (!isset($userInfo['token'])) {
  110. return false;
  111. }
  112. //设置token过期
  113. return MasterWokerTokenService::expireToken($userInfo['token']);
  114. }
  115. public static function mnpAuthLogin($params)
  116. {
  117. try {
  118. //通过code获取微信openid
  119. $response = (new WorkerWeChatMnpService())->getMnpResByCode($params['code']);
  120. $response['user_id'] = $params['user_id'];
  121. $response['terminal'] = UserTerminalEnum::WECHAT_MMP;
  122. return self::createAuth($response);
  123. } catch (\Exception $e) {
  124. self::$error = $e->getMessage();
  125. return false;
  126. }
  127. }
  128. /**
  129. * @notes 生成授权记录
  130. * @param $response
  131. * @return bool
  132. * @throws \Exception
  133. * @author 段誉
  134. * @date 2022/9/16 10:43
  135. */
  136. public static function createAuth($response)
  137. {
  138. //先检查openid是否有记录
  139. $isAuth = MasterWorkerAuth::where('openid', '=', $response['openid'])->findOrEmpty();
  140. if (!$isAuth->isEmpty()) {
  141. if($isAuth->worker_id != $response['user_id']) {
  142. throw new \Exception('该微信已被绑定');
  143. }
  144. if($isAuth->worker_id == 0) {
  145. //更新操作
  146. $isAuth->worker_id = $response['user_id'];
  147. $isAuth->save();
  148. return true;
  149. }
  150. if($isAuth->worker_id == $response['user_id']) {
  151. return true;
  152. }
  153. }
  154. if (isset($response['unionid']) && !empty($response['unionid'])) {
  155. //在用unionid找记录,防止生成两个账号,同个unionid的问题
  156. $userAuth = MasterWorkerAuth::where(['unionid' => $response['unionid']])
  157. ->findOrEmpty();
  158. if (!$userAuth->isEmpty() && $userAuth->worker_id != $response['user_id']) {
  159. throw new \Exception('该微信已被绑定');
  160. }
  161. }
  162. //如果没有授权,直接生成一条微信授权记录
  163. MasterWorkerAuth::create([
  164. 'worker_id' => $response['user_id'],
  165. 'openid' => $response['openid'],
  166. 'unionid' => $response['unionid'] ?? '',
  167. 'terminal' => $response['terminal'],
  168. ]);
  169. return true;
  170. }
  171. }