From 045fb4685569a2f3787bd7bf8ce2fc81080d5496 Mon Sep 17 00:00:00 2001 From: wuhui_zzw <1760308791@qq.com> Date: Wed, 13 Mar 2024 12:02:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E9=85=92=E9=81=93?= =?UTF-8?q?=E9=A6=86token=E8=BF=87=E6=9C=9F=E5=90=8E=E6=9C=AA=E6=AD=A3?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86=EF=BC=8C=E5=AF=BC=E8=87=B4=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=99=BB=E5=BD=95=E7=8A=B6=E6=80=81=E4=B9=9F=E8=A2=AB?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BA=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/middleware/ShopTokenMiddleware.php | 12 ++++++------ .../system/merchant/MerchantAdminRepository.php | 6 ++---- app/controller/api/store/merchant/Merchant.php | 2 +- config/admin.php | 2 +- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/app/common/middleware/ShopTokenMiddleware.php b/app/common/middleware/ShopTokenMiddleware.php index 4ccda6c..038dc24 100644 --- a/app/common/middleware/ShopTokenMiddleware.php +++ b/app/common/middleware/ShopTokenMiddleware.php @@ -32,13 +32,13 @@ class ShopTokenMiddleware extends BaseMiddleware{ $repository->checkToken($token); $payload = $service->decode($token); } catch (Throwable $e) {//Token 过期 - throw new AuthException('token 已过期'); + throw new ValidateException('token 已过期'); } - if ('mer' != $payload->jti[1]) throw new AuthException('无效的 token'); + if ('mer' != $payload->jti[1]) throw new ValidateException('无效的 token'); $admin = $repository->get($payload->jti[0]); - if (!$admin) throw new AuthException('账号不存在'); - if (!$admin['status']) throw new AuthException('账号已被禁用'); + if (!$admin) throw new ValidateException('账号不存在'); + if (!$admin['status']) throw new ValidateException('账号已被禁用'); /** * @var MerchantRepository $merchantRepository @@ -48,7 +48,7 @@ class ShopTokenMiddleware extends BaseMiddleware{ $merchant = $merchantRepository->get($admin->mer_id); if (!$merchant || !$merchant['status']) - throw new AuthException('商户已被锁定'); + throw new ValidateException('商户已被锁定'); } catch (Throwable $e) { if ($force) throw $e; @@ -56,7 +56,7 @@ class ShopTokenMiddleware extends BaseMiddleware{ return false; }); $request->macros(['merchantType', 'shopMerId'], function () { - throw new AuthException('请登录'); + throw new ValidateException('请登录'); }); return; } diff --git a/app/common/repositories/system/merchant/MerchantAdminRepository.php b/app/common/repositories/system/merchant/MerchantAdminRepository.php index 1e39884..dccf143 100644 --- a/app/common/repositories/system/merchant/MerchantAdminRepository.php +++ b/app/common/repositories/system/merchant/MerchantAdminRepository.php @@ -239,11 +239,9 @@ class MerchantAdminRepository extends BaseRepository public function checkToken(string $token) { $has = Cache::has('mer_' . $token); - if (!$has) - throw new AuthException('无效的token'); + if (!$has) throw new ValidateException('token 已经失效,请重新登录!'); $lastTime = Cache::get('mer_' . $token); - if (($lastTime + (intval(Config::get('admin.token_valid_exp', 15))) * 60) < time()) - throw new AuthException('token 已过期'); + if (($lastTime + (intval(Config::get('admin.token_valid_exp', 15))) * 60) < time()) throw new ValidateException('token 已过期'); } /** diff --git a/app/controller/api/store/merchant/Merchant.php b/app/controller/api/store/merchant/Merchant.php index dadf081..5b4d261 100644 --- a/app/controller/api/store/merchant/Merchant.php +++ b/app/controller/api/store/merchant/Merchant.php @@ -173,7 +173,7 @@ class Merchant extends BaseController return app('json')->success([ 'token' => $tokenInfo['token'], - 'exp' => $tokenInfo['out'], + 'exp' => $tokenInfo['exp'], 'mer_id' => $admin['mer_id'], 'merchant_type' => $admin['merchant_type'], 'mer_name' => $admin['mer_name'], diff --git a/config/admin.php b/config/admin.php index 370b968..31947d4 100644 --- a/config/admin.php +++ b/config/admin.php @@ -5,7 +5,7 @@ return [ //token 有效期 - 'token_exp' => 6, //6小时 + 'token_exp' => 24, //6小时 //token超时多久可自动续期(后台) 'token_valid_exp' => 30, //30分钟 //token超时多久可自动续期(用户)