LoginLogic.php 6.5 KB

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