LoginLogic.php 6.9 KB

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