LoginLogic.php 9.2 KB

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