From 845dff1ef760fdcb1161147c19e241791dad7455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=85=E5=BF=B5?= Date: Fri, 8 May 2026 13:28:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=20token=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 14 ++++++++++++-- api/apiT.js | 2 +- pages/index/index.vue | 28 ++++++++++++++-------------- pages/login/index.vue | 4 ++-- utils/auth.js | 2 +- 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/App.vue b/App.vue index 43212e3..eeef267 100644 --- a/App.vue +++ b/App.vue @@ -5,15 +5,25 @@ import { version } from './package.json' + import { + silentLogin + } from '@/utils/auth.js' // #ifdef APP import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update'; // #endif export default { onLaunch: function() { - // 线上示例使用 - // console.log('%c uni-app官方团队诚邀优秀前 端工程师加盟,一起打造更卓越的uni-app & uniCloud,欢迎投递简历到 hr2013@dcloud.io', 'color: red'); console.log('App Launch'); + const token = uni.getStorageSync('token') + if (token) { + this.globalData.tokenRefreshPromise = silentLogin().then(result => { + if (!result.success) { + uni.removeStorageSync('token') + } + return result + }) + } }, onShow: function() { console.log('App Show') diff --git a/api/apiT.js b/api/apiT.js index 63b788f..a0121c5 100644 --- a/api/apiT.js +++ b/api/apiT.js @@ -31,7 +31,7 @@ async function handle401Error(originalOptions) { // 清空队列 retryQueue.length = 0; } else { - // 登录失败,拒绝所有队列中的请求 + // 登录失败,拒绝所有队列中的请求并跳转登录页 retryQueue.forEach(({ reject }) => { reject({ data: { message: '登录失败,请重新登录' } }); }); diff --git a/pages/index/index.vue b/pages/index/index.vue index 9289f11..d0546a3 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -88,21 +88,21 @@ export default { homemapurl:'' }; }, - onLoad(options) { - // 数据加载逻辑 - if (!uni.getStorageSync('token')) { - this.mini_login().then(() => { - this.bannerListfn() - this.configListfn() - this.barragelist() - this.$refs.paging && this.$refs.paging.reload() - }) - }else{ - this.bannerListfn() - this.configListfn() - this.barragelist() - this.$refs.paging && this.$refs.paging.reload() + async onLoad(options) { + const token = uni.getStorageSync('token') + if (!token) { + await this.mini_login() + } else { + const refreshPromise = getApp().globalData.tokenRefreshPromise + if (refreshPromise) { + const result = await refreshPromise + if (!result.success) return + } } + this.bannerListfn() + this.configListfn() + this.barragelist() + this.$refs.paging && this.$refs.paging.reload() }, onShow() { this.getFuzzy().then(res => { diff --git a/pages/login/index.vue b/pages/login/index.vue index 074da0a..27f27c8 100644 --- a/pages/login/index.vue +++ b/pages/login/index.vue @@ -81,7 +81,7 @@ icon: 'none' }); - } else if (res.data.code == 0) { + } else if (res.data.code == 200) { //返回数据接收成功 this.mini_login(); } @@ -111,7 +111,7 @@ }).then(res => { console.log(res.data); uni.hideLoading() - if (res.data.code == 0) { + if (res.data.code == 200) { //存取session uni.setStorageSync('openid', res.data.data.openid); uni.setStorageSync('token', res.data.data.token); diff --git a/utils/auth.js b/utils/auth.js index 07ff985..1ae2696 100644 --- a/utils/auth.js +++ b/utils/auth.js @@ -23,7 +23,7 @@ export function silentLogin() { 'Fdesk-System-Code': 'wechat' }, success: (res) => { - if (res.data.code == 0) { + if (res.data.code == 200) { const { token, openid } = res.data.data; // 更新本地存储 uni.setStorageSync('token', token);