AuthService.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace app\admin\service\user;
  3. use app\model\SysUser;
  4. use support\Db;
  5. use Tinywan\Jwt\JwtToken;
  6. class AuthService
  7. {
  8. public static function login($params)
  9. {
  10. DB::beginTransaction();
  11. try {
  12. $user = SysUser::findByLoginName($params['username']);
  13. if (!$user || $user->user_login_pass != md5(sha1($params['password']))) {
  14. throw new \Exception('账号 / 密码错误');
  15. }
  16. // 禁用用户
  17. if ($user->user_status == 'DISABLED') {
  18. throw new \Exception('当前账户已禁用,请联系管理员');
  19. }
  20. switch ($user->user_status) {
  21. case 'DISABLED':
  22. throw new \Exception('当前账户已禁用,请联系管理员');
  23. break;
  24. case 'PENDING':
  25. // 待激活用户登录后自动激活
  26. if (SysUser::where('user_id', $user->user_id)->update(['user_status' => 'ACTIVED'])) {
  27. throw new \Exception('用户状态修改失败');
  28. }
  29. break;
  30. default:
  31. break;
  32. }
  33. $extend = [
  34. 'id' => $user->user_id,
  35. 'client' => 'admin'
  36. ];
  37. $token = JwtToken::generateToken($extend);
  38. // 提交事务
  39. DB::commit();
  40. return json_success('', $token);
  41. } catch (\Exception $e) {
  42. // 回滚事务
  43. DB::rollBack();
  44. return json_fail($e->getMessage());
  45. }
  46. }
  47. }