LoginLogic.php 8.8 KB

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