LoginLogic.php 12 KB

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