LoginLogic.php 6.0 KB

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