LoginLogic.php 7.3 KB

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