AuthController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Modules\Pc\Controllers;
  3. use App\Base\BaseController;
  4. use App\Base\Validation\Phone;
  5. use App\Exceptions\ClientException;
  6. use App\Models\User\User;
  7. use App\Modules\Mini\Services\AuthService;
  8. use App\Modules\Mini\Services\CaptchaService;
  9. use App\Modules\Mini\Services\EmailService;
  10. use Illuminate\Support\Facades\Auth;
  11. class AuthController extends BaseController
  12. {
  13. public function emailCaptcha()
  14. {
  15. $params = $this->valid([
  16. "email" => "required|email",
  17. "source" => "required",
  18. ]);
  19. return $this->ok(app(EmailService::class)->emailCaptcha($params['email'], $params['source']));
  20. }
  21. public function profile()
  22. {
  23. /** @var User $user */
  24. $user = Auth::user();
  25. return $this->ok([
  26. "id" => $user->id,
  27. "token" => app(AuthService::class)->encryptToken($user->id),
  28. "name" => $user->name,
  29. "avatar" => $user->avatar,
  30. "company" => [
  31. "id" => optional($user->company)->id ?? 0,
  32. "name" => optional($user->company)->name ?? "",
  33. ],
  34. ]);
  35. }
  36. public function register()
  37. {
  38. $params = $this->valid([
  39. "email" => "required|email",
  40. "phone" => ["required", new Phone()],
  41. "password" => "required",
  42. "captcha" => "required",
  43. ]);
  44. if (!app(CaptchaService::class)->checkCaptcha($params['email'], $params['captcha'])) {
  45. throw new ClientException("验证码错误");
  46. }
  47. $user = app(AuthService::class)->register($params);
  48. return $this->ok([
  49. "token" => app(AuthService::class)->encryptToken($user->id),
  50. "name" => $user->name,
  51. "phone" => $user->phone,
  52. ]);
  53. }
  54. public function login()
  55. {
  56. $params = $this->valid([
  57. "userinfo" => "required",
  58. "password" => "required",
  59. ]);
  60. app(AuthService::class)->login($params);
  61. return $this->ok([
  62. "token" => "token",
  63. "name" => "",
  64. "phone" => "",
  65. ]);
  66. }
  67. public function resetPassword()
  68. {
  69. return $this->ok([
  70. "token" => "token",
  71. "name" => "",
  72. "phone" => "",
  73. ]);
  74. }
  75. }