LoginLogic.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\workerapi\logic;
  3. use app\common\enum\LoginEnum;
  4. use app\common\enum\notice\NoticeEnum;
  5. use app\common\logic\BaseLogic;
  6. use app\common\model\master_worker\MasterWorker;
  7. use app\common\model\master_worker_register\MasterWorkerRegister;
  8. use app\common\service\FileService;
  9. use app\common\service\sms\SmsDriver;
  10. use think\facade\Config;
  11. use app\workerapi\service\MasterWokerTokenService;
  12. /**
  13. * @author 林海涛
  14. * @date ${DATA}
  15. */
  16. class LoginLogic extends BaseLogic
  17. {
  18. /**
  19. * @notes 确认手机号
  20. * @param $params
  21. * @return bool
  22. * @author 段誉
  23. * @date 2022/9/21 17:28
  24. */
  25. public static function confirmMobile(array $params)
  26. {
  27. try {
  28. // 验证码请求
  29. $sceneId = NoticeEnum::GCSSJHM_CAPTCHA;
  30. // 校验短信
  31. $checkSmsCode = (new SmsDriver())->verify($params['mobile'], $params['code'], $sceneId);
  32. if (!$checkSmsCode) {
  33. throw new \Exception('验证码错误');
  34. }
  35. return true;
  36. } catch (\Exception $e) {
  37. self::setError($e->getMessage());
  38. return false;
  39. }
  40. }
  41. public static function register(array $params)
  42. {
  43. try {
  44. MasterWorkerRegister::create([
  45. 'maintain_exp_type' => $params['maintain_exp_type'],
  46. 'other_exp_type' => $params['other_exp_type'],
  47. 'city' => $params['city'],
  48. 'vehicle_type' => $params['vehicle_type'],
  49. 'name' => $params['name'],
  50. 'age' => $params['age'],
  51. 'mobile' => $params['mobile'],
  52. ]);
  53. return true;
  54. } catch (\Exception $e) {
  55. self::setError($e->getMessage());
  56. return false;
  57. }
  58. }
  59. public static function login($params)
  60. {
  61. try {
  62. // 账号/手机号 密码登录
  63. $where = ['account' => $params['account']];
  64. if ($params['scene'] == LoginEnum::MOBILE_CAPTCHA) {
  65. //手机验证码登录
  66. $where = ['mobile' => $params['account']];
  67. }
  68. $user = MasterWorker::where($where)->findOrEmpty();
  69. if ($user->isEmpty()) {
  70. throw new \Exception('用户不存在');
  71. }
  72. //更新登录信息
  73. $user->login_time = time();
  74. $user->login_ip = request()->ip();
  75. $user->save();
  76. //设置token
  77. $userInfo = MasterWokerTokenService::setToken($user->id, 1);
  78. //返回登录信息
  79. $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
  80. $avatar = FileService::getFileUrl($avatar);
  81. return [
  82. 'nickname' => $userInfo['nickname'],
  83. 'sn' => $userInfo['sn'],
  84. 'mobile' => $userInfo['mobile'],
  85. 'avatar' => $avatar,
  86. 'token' => $userInfo['token'],
  87. ];
  88. } catch (\Exception $e) {
  89. self::setError($e->getMessage());
  90. return false;
  91. }
  92. }
  93. public static function logout($userInfo)
  94. {
  95. //token不存在,不注销
  96. if (!isset($userInfo['token'])) {
  97. return false;
  98. }
  99. //设置token过期
  100. return MasterWokerTokenService::expireToken($userInfo['token']);
  101. }
  102. }