LoginLogic.php 7.3 KB

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