AuthService.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Modules\Admin\Services;
  3. use App\Exceptions\ClientException;
  4. use App\Models\User;
  5. use Illuminate\Support\Facades\Auth;
  6. class AuthService
  7. {
  8. public function login($data)
  9. {
  10. $u = User::wherePhone($data['username'])->orWhere("email",$data['username'])->first();
  11. if (is_null($u)) {
  12. throw new ClientException("账号或密码错误,请重新输入");
  13. }
  14. if (!$u->checkPassword($data['password'])) {
  15. throw new ClientException("账号或密码错误,请重新输入!");
  16. }
  17. if ($u->status != User::STATUS_OK) {
  18. throw new ClientException("当前用户被禁用,请联系管理员");
  19. }
  20. return [
  21. "admin_token" => $this->encryptToken($u->id),
  22. ];
  23. }
  24. public function profile()
  25. {
  26. $u = Auth::user();
  27. return [
  28. "id" => $u->id,
  29. "phone" => $u->phone,
  30. "email" => $u->email,
  31. "group" => $u->group->name,
  32. ];
  33. }
  34. public function changePassword($data)
  35. {
  36. $u = Auth::user();
  37. if (!$u->checkPassword($data['old_password'])) {
  38. throw new ClientException("原密码错误");
  39. }
  40. $u->password = $u->hashPassword($data['password']);
  41. $u->save();
  42. return true;
  43. }
  44. public function encryptToken($uid)
  45. {
  46. return md5($uid) . $uid;
  47. }
  48. public function decryptToken($token)
  49. {
  50. return substr($token, 32);
  51. }
  52. }