From 9c175fdb780cbf6543a79e76a3250b8a88185e49 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Mon, 25 Nov 2024 18:02:40 +0800 Subject: [PATCH] feat : wx login --- app/Constants/ApiCode.php | 19 ++++++ app/Constants/Common/ThirdCode.php | 4 +- app/Controller/Api/UserController.php | 14 +++-- app/Middleware/Api/JwtAuthMiddleware.php | 42 ++++++++++++- app/Model/User.php | 10 +++- app/Service/Api/User/BindPhoneByWxService.php | 30 ++++++++++ .../ServiceTrait/Api/GetUserInfoTrait.php | 60 +++++++++++++++++++ app/Service/ServiceTrait/Api/WxMiniTrait.php | 5 ++ 8 files changed, 175 insertions(+), 9 deletions(-) create mode 100644 app/Constants/ApiCode.php create mode 100644 app/Service/Api/User/BindPhoneByWxService.php create mode 100644 app/Service/ServiceTrait/Api/GetUserInfoTrait.php diff --git a/app/Constants/ApiCode.php b/app/Constants/ApiCode.php new file mode 100644 index 0000000..4ba78d9 --- /dev/null +++ b/app/Constants/ApiCode.php @@ -0,0 +1,19 @@ +handle(); } } diff --git a/app/Middleware/Api/JwtAuthMiddleware.php b/app/Middleware/Api/JwtAuthMiddleware.php index 352ba94..e9feede 100644 --- a/app/Middleware/Api/JwtAuthMiddleware.php +++ b/app/Middleware/Api/JwtAuthMiddleware.php @@ -2,7 +2,45 @@ namespace App\Middleware\Api; -class JwtAuthMiddleware -{ +use App\Constants\ApiCode; +use App\Lib\ApiReturn; +use App\Lib\Crypto\CryptoFactory; +use Hyperf\Context\Context; +use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; +class JwtAuthMiddleware implements MiddlewareInterface +{ + public function __construct( + protected HttpResponse $response, + protected ApiReturn $apiReturn, + protected CryptoFactory $cryptoFactory, + ){} + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + // 获取头部token + $authorization = $request->getHeaderLine('Authorization'); + + if (empty($authorization)){ + return $this->response->json( + $this->apiReturn->error(ApiCode::getMessage(ApiCode::LOGIN_ERROR), ApiCode::LOGIN_ERROR) + ); + } + + $authorization = str_replace("Bearer ", "", $authorization); + $userJwt = $this->cryptoFactory->cryptoClass('jwt', $authorization)->decrypt(); + if (empty($userJwt)) { + return $this->response->json( + $this->apiReturn->error(ApiCode::getMessage(ApiCode::LOGIN_TOKEN_ERROR), ApiCode::LOGIN_TOKEN_ERROR) + ); + } + + Context::set('user_id',$userJwt['data']->id); + + return $handler->handle($request); + } } \ No newline at end of file diff --git a/app/Model/User.php b/app/Model/User.php index 5fbf2e0..f2a787b 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Model; +use Hyperf\Database\Model\Builder; use Hyperf\DbConnection\Model\Model; /** @@ -47,5 +48,12 @@ class User extends Model const CREATED_AT = 'create_time'; const UPDATED_AT = 'update_time'; - + /** + * @param int $id + * @return Builder|\Hyperf\Database\Model\Model|null + */ + public function getInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('id', $id)->first(); + } } diff --git a/app/Service/Api/User/BindPhoneByWxService.php b/app/Service/Api/User/BindPhoneByWxService.php new file mode 100644 index 0000000..0667317 --- /dev/null +++ b/app/Service/Api/User/BindPhoneByWxService.php @@ -0,0 +1,30 @@ +checkBindPhone($this->userId); + + + + return $this->return->success(); + } +} \ No newline at end of file diff --git a/app/Service/ServiceTrait/Api/GetUserInfoTrait.php b/app/Service/ServiceTrait/Api/GetUserInfoTrait.php new file mode 100644 index 0000000..76bcc04 --- /dev/null +++ b/app/Service/ServiceTrait/Api/GetUserInfoTrait.php @@ -0,0 +1,60 @@ +userModel->getInfoById($userId); + if (!$userInfo) { + return false; + } + + Context::set($key, $userInfo); + + return $userInfo; + } + + /** + * @param $userId + * @return void + */ + protected function checkBindPhone($userId): void + { + $userInfo = $this->getUserInfo($userId); + + if (!empty($userInfo->phone)) throw new ErrException('该账户已绑定手机号,无需重新绑定。若需更换手机号,请联系客服重置后再绑定'); + } +} \ No newline at end of file diff --git a/app/Service/ServiceTrait/Api/WxMiniTrait.php b/app/Service/ServiceTrait/Api/WxMiniTrait.php index 72728c0..952576a 100644 --- a/app/Service/ServiceTrait/Api/WxMiniTrait.php +++ b/app/Service/ServiceTrait/Api/WxMiniTrait.php @@ -76,4 +76,9 @@ trait WxMiniTrait throw new ErrException($e->getMessage()); } } + + public function jsCodeGetPhoneNumber(string $code): mixed + { + + } } \ No newline at end of file