AuthService.php 1.7 KB

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