LoginLogic.php 6.8 KB

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