123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace App\Modules\Mini\Controllers;
- use App\Base\BaseController;
- use App\Base\Validation\Phone;
- use App\Exceptions\ClientException;
- use App\Models\Cart;
- use App\Models\User\User;
- use App\Models\User\UserFav;
- use App\Modules\Mini\Services\AuthService;
- use App\Modules\Mini\Services\CaptchaService;
- use App\Modules\Mini\Services\EmailService;
- use App\Modules\Mini\Services\WechatService;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\Auth;
- class AuthController extends BaseController
- {
- public function codeToSession()
- {
- $params = $this->valid([
- "code" => "required",
- ]);
- $sessionData = app(WechatService::class)->codeToSession($params['code']);
- $token = app(AuthService::class)->authByOpenid($sessionData['openid']);
- return $this->ok([
- "token" => $token,
- "openid" => $sessionData['openid'],
- "sessionKey" => $sessionData['session_key'],
- ]);
- }
- public function decryptUserInfo()
- {
- $params = $this->valid([
- "iv" => "required",
- "encrypted_data" => "required",
- "session_key" => "required",
- ]);
- app(WechatService::class)->decryptUserInfo($params);
- /** @var User $user */
- $user = \Auth::user();
- return $this->ok($user->format());
- }
- public function updateUserInfo()
- {
- $params = $this->valid([
- "avatar" => "string",
- "name" => "string",
- "email" => "string",
- "captcha" => "string",
- "password" => "string",
- ]);
- if ($captcha = Arr::get($params, 'captcha')) {
- if (!app(CaptchaService::class)->checkCaptcha($params['email'], $captcha)) {
- throw new ClientException("验证码错误");
- }
- }
- $user = app(AuthService::class)->updateUserInfo(Auth::user(), $params);
- return $this->ok($user->format());
- }
- public function decryptPhone()
- {
- $params = $this->valid([
- "iv" => "required",
- "encrypted_data" => "required",
- "session_key" => "required",
- ]);
- app(WechatService::class)->decryptPhone($params);
- /** @var User $user */
- $user = \Auth::user();
- return $this->ok($user->format());
- }
- public function profile()
- {
- /** @var User $user */
- $user = Auth::user();
- return $this->ok([
- "id" => $user->id,
- "token" => app(AuthService::class)->encryptToken($user->id),
- "name" => $user->name,
- "avatar" => $user->avatar,
- "company" => [
- "id" => optional($user->company)->id ?? 0,
- "name" => optional($user->company)->name ?? "",
- ],
- "email" => $user->email ?? "",
- "phone" => $user->phone ?? "",
- "cart_total" => Cart::where("user_id", $user->id)->count(),
- "fav_total" => UserFav::where("user_id", $user->id)->count(),
- ]);
- }
- public function test()
- {
- $id = request()->input("id");
- return $this->ok(app(AuthService::class)->encryptToken($id));
- }
- public function emailCaptcha()
- {
- $params = $this->valid([
- "email" => "required|email",
- "source" => "required",
- ]);
- return $this->ok(app(EmailService::class)->emailCaptcha($params['email'], $params['source']));
- }
- public function register()
- {
- $params = $this->valid([
- "email" => "required|email",
- "phone" => new Phone(),
- "password" => "required",
- "captcha" => "required",
- ]);
- if (!app(CaptchaService::class)->checkCaptcha($params['email'], $params['captcha'])) {
- throw new ClientException("验证码错误");
- }
- $user = app(AuthService::class)->register($params);
- return $this->ok($user->format());
- }
- public function login()
- {
- $params = $this->valid([
- "userinfo" => "required",
- "password" => "required",
- ]);
- $user = app(AuthService::class)->login($params);
- return $this->ok($user->format());
- }
- public function resetPassword()
- {
- $params = $this->valid([
- "email" => "required|email",
- "password" => "required|min:6",
- "captcha" => "required",
- ]);
- if (!app(CaptchaService::class)->checkCaptcha($params['email'], $params['captcha'])) {
- throw new ClientException("验证码错误");
- }
- $user = app(AuthService::class)->resetPassword($params);
- return $this->ok($user->format());
- }
- }
|