1
0

LoginLogic.php 11 KB

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