AuthService.php 1.5 KB

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