fix:修复会员登录态过期后不会触发重新登录的问题

This commit is contained in:
妙码生花 2023-08-11 01:16:02 +08:00
parent 2dde95d78a
commit 0304961db1
3 changed files with 24 additions and 7 deletions

View File

@ -5,6 +5,7 @@ namespace app\api\controller;
use ba\Tree;
use Throwable;
use think\facade\Db;
use think\facade\Cookie;
use think\facade\Config;
use app\common\controller\Frontend;
@ -18,7 +19,7 @@ class Index extends Frontend
}
/**
* 前台初始化请求
* 前台和会员中心的初始化请求
* @throws Throwable
*/
public function index()
@ -38,6 +39,20 @@ class Index extends Frontend
}
$rules = array_values($rules);
} else {
// 若是从前台会员中心内发出的请求,要求必须登录,否则会员中心异常
$requiredLogin = $this->request->get('requiredLogin/b', false);
if ($requiredLogin) {
$token = $this->request->server('HTTP_BA_USER_TOKEN', $this->request->request('ba-user-token', Cookie::get('ba-user-token') ?: false));
// 若过期可触发过期判定
$this->auth->init($token);
// 未过期或无token直接要求登录
$this->error(__('Please login first'), [
'routePath' => '/user/login'
], 302);
}
$rules = Db::name('user_rule')
->where('status', '1')
->where('no_login_valid', 1)

View File

@ -14,7 +14,7 @@ export const indexUrl = '/api/index/'
* 1. token时
* 2. token
*/
export function initialize(callback?: (res: ApiResponse) => void) {
export function initialize(callback?: (res: ApiResponse) => void, requiredLogin?: boolean) {
debounce(() => {
if (router.currentRoute.value.meta.initialize === false) return
@ -28,6 +28,9 @@ export function initialize(callback?: (res: ApiResponse) => void) {
createAxios({
url: indexUrl + 'index',
method: 'get',
params: {
requiredLogin: requiredLogin ? 1 : 0,
},
}).then((res) => {
setTitle(res.data.site.siteName)
handleFrontendRoute(res.data.rules, res.data.menus)
@ -37,16 +40,15 @@ export function initialize(callback?: (res: ApiResponse) => void) {
if (!isEmpty(res.data.userInfo)) {
res.data.userInfo.refresh_token = userInfo.getToken('refresh')
userInfo.dataFill(res.data.userInfo)
// 请求到会员信息才设置会员中心初始化是成功的
siteConfig.setUserInitialize(true)
}
if (!res.data.openMemberCenter) memberCenter.setLayoutMode('Disable')
siteConfig.setInitialize(true)
if (userInfo.isLogin()) {
siteConfig.setUserInitialize(true)
}
typeof callback == 'function' && callback(res)
})
}, 200)()

View File

@ -74,7 +74,7 @@ onMounted(async () => {
if (siteConfig.userInitialize) {
callback()
} else {
initialize(callback)
initialize(callback, true)
}
if (document.body.clientWidth < 1024) {