AuthController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Modules\Mini\Controllers;
  3. use App\Base\BaseController;
  4. use App\Base\Validation\Phone;
  5. use App\Exceptions\ClientException;
  6. use App\Models\Cart;
  7. use App\Models\User\User;
  8. use App\Models\User\UserFav;
  9. use App\Modules\Mini\Services\AuthService;
  10. use App\Modules\Mini\Services\CaptchaService;
  11. use App\Modules\Mini\Services\EmailService;
  12. use App\Modules\Mini\Services\WechatService;
  13. use Illuminate\Support\Arr;
  14. use Illuminate\Support\Facades\Auth;
  15. class AuthController extends BaseController
  16. {
  17. public function codeToSession()
  18. {
  19. $params = $this->valid([
  20. "code" => "required",
  21. ]);
  22. $sessionData = app(WechatService::class)->codeToSession($params['code']);
  23. $token = app(AuthService::class)->authByOpenid($sessionData['openid']);
  24. return $this->ok([
  25. "token" => $token,
  26. "openid" => $sessionData['openid'],
  27. "sessionKey" => $sessionData['session_key'],
  28. ]);
  29. }
  30. public function decryptUserInfo()
  31. {
  32. $params = $this->valid([
  33. "iv" => "required",
  34. "encrypted_data" => "required",
  35. "session_key" => "required",
  36. ]);
  37. app(WechatService::class)->decryptUserInfo($params);
  38. /** @var User $user */
  39. $user = \Auth::user();
  40. return $this->ok($user->format());
  41. }
  42. public function updateUserInfo()
  43. {
  44. $params = $this->valid([
  45. "avatar" => "string",
  46. "name" => "string",
  47. "email" => "string",
  48. "captcha" => "string",
  49. "password" => "string",
  50. ]);
  51. if ($captcha = Arr::get($params, 'captcha')) {
  52. if (!app(CaptchaService::class)->checkCaptcha($params['email'], $captcha)) {
  53. throw new ClientException("验证码错误");
  54. }
  55. }
  56. $user = app(AuthService::class)->updateUserInfo(Auth::user(), $params);
  57. return $this->ok($user->format());
  58. }
  59. public function decryptPhone()
  60. {
  61. $params = $this->valid([
  62. "iv" => "required",
  63. "encrypted_data" => "required",
  64. "session_key" => "required",
  65. ]);
  66. app(WechatService::class)->decryptPhone($params);
  67. /** @var User $user */
  68. $user = \Auth::user();
  69. return $this->ok($user->format());
  70. }
  71. public function profile()
  72. {
  73. /** @var User $user */
  74. $user = Auth::user();
  75. return $this->ok([
  76. "id" => $user->id,
  77. "token" => app(AuthService::class)->encryptToken($user->id),
  78. "name" => $user->name,
  79. "avatar" => $user->avatar,
  80. "company" => [
  81. "id" => optional($user->company)->id ?? 0,
  82. "name" => optional($user->company)->name ?? "",
  83. ],
  84. "email" => $user->email ?? "",
  85. "phone" => $user->phone ?? "",
  86. "cart_total" => Cart::where("user_id", $user->id)->count(),
  87. "fav_total" => UserFav::where("user_id", $user->id)->count(),
  88. ]);
  89. }
  90. public function test()
  91. {
  92. $id = request()->input("id");
  93. return $this->ok(app(AuthService::class)->encryptToken($id));
  94. }
  95. public function emailCaptcha()
  96. {
  97. $params = $this->valid([
  98. "email" => "required|email",
  99. "source" => "required",
  100. ]);
  101. return $this->ok(app(EmailService::class)->emailCaptcha($params['email'], $params['source']));
  102. }
  103. public function register()
  104. {
  105. $params = $this->valid([
  106. "email" => "required|email",
  107. "phone" => new Phone(),
  108. "password" => "required",
  109. "captcha" => "required",
  110. ]);
  111. if (!app(CaptchaService::class)->checkCaptcha($params['email'], $params['captcha'])) {
  112. throw new ClientException("验证码错误");
  113. }
  114. $user = app(AuthService::class)->register($params);
  115. return $this->ok($user->format());
  116. }
  117. public function login()
  118. {
  119. $params = $this->valid([
  120. "userinfo" => "required",
  121. "password" => "required",
  122. ]);
  123. $user = app(AuthService::class)->login($params);
  124. return $this->ok($user->format());
  125. }
  126. public function resetPassword()
  127. {
  128. $params = $this->valid([
  129. "email" => "required|email",
  130. "password" => "required|min:6",
  131. "captcha" => "required",
  132. ]);
  133. if (!app(CaptchaService::class)->checkCaptcha($params['email'], $params['captcha'])) {
  134. throw new ClientException("验证码错误");
  135. }
  136. $user = app(AuthService::class)->resetPassword($params);
  137. return $this->ok($user->format());
  138. }
  139. }