AuthService.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\admin\service\auth;
  3. use app\model\SysUser;
  4. use support\Db;
  5. use support\Request;
  6. use Tinywan\Jwt\JwtToken;
  7. class AuthService
  8. {
  9. public static function login($params)
  10. {
  11. DB::beginTransaction();
  12. try {
  13. $user = SysUser::findByLoginName($params['username']);
  14. if (!$user || $user->user_login_pass != md5(sha1($params['password']))) {
  15. throw new \Exception('账号 / 密码错误');
  16. }
  17. // 禁用用户
  18. if ($user->user_status == 'DISABLED') {
  19. throw new \Exception('当前账户已禁用,请联系管理员');
  20. }
  21. switch ($user->user_status) {
  22. case 'DISABLED':
  23. throw new \Exception('当前账户已禁用,请联系管理员');
  24. break;
  25. case 'PENDING':
  26. // 待激活用户登录后自动激活
  27. $user->user_status = 'ACTIVED';
  28. if (!$user->save()) {
  29. throw new \Exception('用户状态修改失败');
  30. }
  31. break;
  32. default:
  33. break;
  34. }
  35. $extend = [
  36. 'id' => $user->user_id,
  37. 'client' => 'admin'
  38. ];
  39. $token = JwtToken::generateToken($extend);
  40. // 提交事务
  41. DB::commit();
  42. return json_success('', $token);
  43. } catch (\Exception $e) {
  44. // 回滚事务
  45. DB::rollBack();
  46. return json_fail($e->getMessage());
  47. }
  48. }
  49. /**
  50. * @Desc 刷新token
  51. * @Author Gorden
  52. * @Date 2024/2/21 17:10
  53. *
  54. * @return \support\Response
  55. */
  56. public static function refreshToken()
  57. {
  58. $token = JwtToken::refreshToken();
  59. return json_success('Token已刷新', $token);
  60. }
  61. public static function userInfo(Request $request)
  62. {
  63. $user = SysUser::select('join_user_role_id', 'join_user_dept_id', 'user_status', 'user_category', 'user_name', 'user_login_name', 'user_mobile', 'user_remark', 'user_extend_json', 'user_addtimes')
  64. ->where('user_id', $request->adminId)
  65. ->first()
  66. ->toArray();
  67. return json_success('', $user);
  68. }
  69. }