LoginLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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\model\setting\PostageRegion;
  14. use app\common\service\FileService;
  15. use app\common\service\sms\SmsDriver;
  16. use app\common\service\wechat\WeChatMnpService;
  17. use app\common\service\wechat\WorkerWeChatMnpService;
  18. use think\facade\Config;
  19. use app\workerapi\service\MasterWokerTokenService;
  20. use think\facade\Log;
  21. /**
  22. * @author 林海涛
  23. * @date ${DATA}
  24. */
  25. class LoginLogic extends BaseLogic
  26. {
  27. /**
  28. * @notes 确认手机号
  29. * @param $params
  30. * @return bool
  31. * @author 段誉
  32. * @date 2022/9/21 17:28
  33. */
  34. public static function confirmMobile(array $params)
  35. {
  36. try {
  37. // 验证码请求
  38. $sceneId = NoticeEnum::GCSSJHM_CAPTCHA;
  39. // 校验短信
  40. $checkSmsCode = (new SmsDriver())->verify($params['mobile'], $params['code'], $sceneId);
  41. if (!$checkSmsCode) {
  42. throw new \Exception('验证码错误');
  43. }
  44. return true;
  45. } catch (\Exception $e) {
  46. self::setError($e->getMessage());
  47. return false;
  48. }
  49. }
  50. public static function confirmCredential(array $params)
  51. {
  52. try {
  53. if(isset($params['is_credential']) && $params['is_credential'] == 1){
  54. empty($params['credential_images']) && throw new \Exception('缺少证书');
  55. }
  56. return true;
  57. } catch (\Exception $e) {
  58. self::setError($e->getMessage());
  59. return false;
  60. }
  61. }
  62. public static function register(array $params)
  63. {
  64. try {
  65. // 通过 $params['city'] 查询省市区
  66. $postageRegion = array_column(getPostageRegion(), null, 'id');
  67. $params['province'] = $postageRegion[$params['city']]['pid'];
  68. $params['province'] && $params['area_name'] = $postageRegion[$params['province']]['name'].$postageRegion[$params['city']]['name'];
  69. $master = MasterWorkerRegister::where('mobile',$params['mobile'])->findOrEmpty();
  70. if(!$master->isEmpty() and $master->status==1){
  71. throw new \Exception('该手机号已入驻');
  72. }
  73. if(!$master->isEmpty() and $master->status==0){
  74. throw new \Exception('该手机号后台审核中');
  75. }
  76. if(!$master->isEmpty() and $master->status==2){
  77. $master->save([
  78. 'maintain_exp_type' => isset($params['maintain_exp_type'])?$params['maintain_exp_type']:0,
  79. 'other_exp_type' => isset($params['other_exp_type'])?$params['other_exp_type']:'',
  80. 'other_exp_name' => (isset($params['other_exp_name']) && isset($params['other_exp_type']) && $params['other_exp_type'] == 4)?$params['other_exp_name']:'',
  81. 'province' => $params['province']??0,
  82. 'city' => $params['city']??0,
  83. 'area_name' => $params['area_name']??'',
  84. 'vehicle_type' => $params['vehicle_type'],
  85. 'name' => $params['name'],
  86. 'age' => $params['age'],
  87. 'sex' => $params['sex']??0,
  88. 'mobile' => $params['mobile'],
  89. 'is_credential' => !empty($params['is_credential'])?$params['is_credential']:0,
  90. 'credential_name' => !empty($params['credential_name'])?$params['credential_name']:'',
  91. 'credential_images'=>!empty($params['credential_images'])?json_encode($params['credential_images'],JSON_UNESCAPED_UNICODE):'',
  92. 'lon' => !empty($params['lon'])?$params['lon']:0,
  93. 'lat' => !empty($params['lat'])?$params['lat']:0,
  94. 'address' => !empty($params['address'])?$params['address']:'',
  95. 'status'=>0,
  96. 'sale_id' => $params['sale_id']??0,
  97. ]);
  98. }else{
  99. MasterWorkerRegister::create([
  100. 'maintain_exp_type' => isset($params['maintain_exp_type'])?$params['maintain_exp_type']:0,
  101. 'other_exp_type' => isset($params['other_exp_type'])?$params['other_exp_type']:'',
  102. 'other_exp_name' => (isset($params['other_exp_name']) && isset($params['other_exp_type']) && $params['other_exp_type'] == 4)?$params['other_exp_name']:'',
  103. 'province' => $params['province'],
  104. 'city' => $params['city'],
  105. 'area_name' => $params['area_name'],
  106. 'vehicle_type' => $params['vehicle_type'],
  107. 'name' => $params['name'],
  108. 'age' => $params['age'],
  109. 'sex' => $params['sex']??0,
  110. 'mobile' => $params['mobile'],
  111. 'is_credential' => !empty($params['is_credential'])?$params['is_credential']:0,
  112. 'credential_name' => !empty($params['credential_name'])?$params['credential_name']:'',
  113. 'credential_images'=>!empty($params['credential_images'])?json_encode($params['credential_images'],JSON_UNESCAPED_UNICODE):'',
  114. 'lon' => !empty($params['lon'])?$params['lon']:0,
  115. 'lat' => !empty($params['lat'])?$params['lat']:0,
  116. 'address' => !empty($params['address'])?$params['address']:'',
  117. 'sale_id' => $params['sale_id']??0,
  118. ]);
  119. }
  120. return true;
  121. } catch (\Exception $e) {
  122. self::setError($e->getMessage());
  123. return false;
  124. }
  125. }
  126. public static function login($params)
  127. {
  128. try {
  129. // 账号/手机号 密码登录
  130. $where = ['account' => $params['account']];
  131. if ($params['scene'] == LoginEnum::MOBILE_CAPTCHA) {
  132. //手机验证码登录
  133. $where = ['mobile' => $params['account']];
  134. }
  135. $user = MasterWorker::where($where)->findOrEmpty();
  136. if ($user->isEmpty()) {
  137. $worker_register = MasterWorkerRegister::where('mobile',$params['account'])->findOrEmpty();
  138. if(!$worker_register->isEmpty() && $worker_register->status==0){
  139. throw new \Exception('您的入驻信息正在审核中,客服将在1-2个工作日内联系您进行入驻操作');
  140. }
  141. throw new \Exception('请点击下方的工程师入驻');
  142. }
  143. //更新登录信息
  144. $user->login_time = time();
  145. $user->login_ip = request()->ip();
  146. $user->save();
  147. //设置token
  148. $userInfo = MasterWokerTokenService::setToken($user->id, 1);
  149. //返回登录信息
  150. $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
  151. $avatar = FileService::getFileUrl($avatar);
  152. //验证是否上传身份证
  153. $is_id_card = MasterWorkerInfo::where('worker_id',$user->id)->findOrEmpty()->toArray();
  154. //判断是否填写银行信息
  155. $is_bank = BankAccount::where('worker_id',$user->id)->findOrEmpty()->toArray();
  156. //监测是否签署服务合作协议
  157. $pdf = MasterWorkerAgree::where(['agree_type'=>'master_service_content','worker_id'=>$user->id])->whereIn('audit_state','0,1')->value('pdf_url');
  158. return [
  159. 'nickname' => $userInfo['nickname'],
  160. 'sn' => $userInfo['sn'],
  161. 'mobile' => $userInfo['mobile'],
  162. 'avatar' => $avatar,
  163. 'team_id' => $userInfo['team_id'],
  164. 'team_role' => $userInfo['team_role'],
  165. 'token' => $userInfo['token'],
  166. 'is_id_card'=>!empty($is_id_card)?1:0,
  167. 'is_bank'=>!empty($is_bank)?1:0,
  168. 'is_service_agree'=>!empty($pdf)?1:0
  169. ];
  170. } catch (\Exception $e) {
  171. self::setError($e->getMessage());
  172. return false;
  173. }
  174. }
  175. public static function logout($userInfo)
  176. {
  177. //token不存在,不注销
  178. if (!isset($userInfo['token'])) {
  179. return false;
  180. }
  181. //设置token过期
  182. return MasterWokerTokenService::expireToken($userInfo['token']);
  183. }
  184. public static function mnpAuthLogin($params)
  185. {
  186. try {
  187. //通过code获取微信openid
  188. $response = (new WorkerWeChatMnpService())->getMnpResByCode($params['code']);
  189. $response['user_id'] = $params['user_id'];
  190. $response['terminal'] = UserTerminalEnum::WECHAT_MMP;
  191. return self::createAuth($response);
  192. } catch (\Exception $e) {
  193. self::$error = $e->getMessage();
  194. return false;
  195. }
  196. }
  197. /**
  198. * @notes 生成授权记录
  199. * @param $response
  200. * @return bool
  201. * @throws \Exception
  202. * @author 段誉
  203. * @date 2022/9/16 10:43
  204. */
  205. public static function createAuth($response)
  206. {
  207. //判定同一worker_id是否只有一个openid
  208. $userAuth = MasterWorkerAuth::where(['worker_id' => $response['user_id']])
  209. ->findOrEmpty();
  210. if(!$userAuth->isEmpty() && !empty($userAuth->openid) && $userAuth->openid != $response['openid']){
  211. throw new \Exception('该账号已绑定其他微信');
  212. }
  213. //先检查openid是否有记录
  214. $isAuth = MasterWorkerAuth::where('openid', '=', $response['openid'])->findOrEmpty();
  215. if (!$isAuth->isEmpty()) {
  216. if($isAuth->worker_id != $response['user_id']) {
  217. throw new \Exception('该微信已被绑定');
  218. }
  219. if($isAuth->worker_id == 0) {
  220. //更新操作
  221. $isAuth->worker_id = $response['user_id'];
  222. $isAuth->save();
  223. return true;
  224. }
  225. if($isAuth->worker_id == $response['user_id']) {
  226. return true;
  227. }
  228. }
  229. if (isset($response['unionid']) && !empty($response['unionid'])) {
  230. //在用unionid找记录,防止生成两个账号,同个unionid的问题
  231. $userAuth = MasterWorkerAuth::where(['unionid' => $response['unionid']])
  232. ->findOrEmpty();
  233. if (!$userAuth->isEmpty() && $userAuth->worker_id != $response['user_id']) {
  234. throw new \Exception('该微信已被绑定');
  235. }
  236. }
  237. //如果没有授权,直接生成一条微信授权记录
  238. MasterWorkerAuth::create([
  239. 'worker_id' => $response['user_id'],
  240. 'openid' => $response['openid'],
  241. 'unionid' => $response['unionid'] ?? '',
  242. 'terminal' => $response['terminal'],
  243. ]);
  244. return true;
  245. }
  246. }