修复首页登录监测
This commit is contained in:
parent
0eb6237e41
commit
bb183e89fc
1
App.vue
1
App.vue
|
|
@ -183,6 +183,7 @@ export default {
|
||||||
onShow: function () {
|
onShow: function () {
|
||||||
uni.hideTabBar();
|
uni.hideTabBar();
|
||||||
console.log('App Show')
|
console.log('App Show')
|
||||||
|
uni.$emit('appShowRefresh')
|
||||||
},
|
},
|
||||||
onHide: function () {
|
onHide: function () {
|
||||||
console.log('App Hide')
|
console.log('App Hide')
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,50 @@
|
||||||
|
# 改动记录
|
||||||
|
|
||||||
|
## 2026-06-12
|
||||||
|
|
||||||
|
### 后台长时间运行后首页登录态不同步
|
||||||
|
|
||||||
|
问题现象:
|
||||||
|
|
||||||
|
- APP 后台运行较长时间后再次进入。
|
||||||
|
- 商家首页显示未登录或无法及时获取商家信息。
|
||||||
|
- “我的”页面仍显示已登录。
|
||||||
|
- 从“我的”页面切回首页后,首页才能正常获取商家信息。
|
||||||
|
|
||||||
|
涉及文件:
|
||||||
|
|
||||||
|
- `App.vue`
|
||||||
|
- `pages/home/home.vue`
|
||||||
|
- `utils/request.js`
|
||||||
|
|
||||||
|
改动内容:
|
||||||
|
|
||||||
|
- `App.vue`
|
||||||
|
- 在全局 `onShow` 中新增 `uni.$emit('appShowRefresh')`。
|
||||||
|
- APP 从后台恢复前台时主动通知页面刷新状态,避免只依赖页面级 `onShow`。
|
||||||
|
|
||||||
|
- `pages/home/home.vue`
|
||||||
|
- 新增 `refreshPageState` 方法,统一处理首页登录态刷新。
|
||||||
|
- `onShow`、下拉刷新、APP 前台恢复事件都调用同一套刷新逻辑。
|
||||||
|
- `onLoad` 注册 `appShowRefresh` 事件。
|
||||||
|
- `onUnload` 解除 `appShowRefresh` 事件,避免重复监听。
|
||||||
|
- 有 token 时主动重新请求 `/sj/user/getUser`。
|
||||||
|
- 无 token 时清空 `userInfo`、`authDetails`、`sjId`,并重置 `isUserInfoLoaded`。
|
||||||
|
- `getUserInfo` 请求成功后显式设置 `isLogin = true` 和 `isUserInfoLoaded = true`。
|
||||||
|
- `getUserInfo` 请求失败时,如果本地 token 已被清理,则同步首页为未登录状态。
|
||||||
|
|
||||||
|
- `utils/request.js`
|
||||||
|
- `refreshToken` 兼容后端返回结构:
|
||||||
|
- 支持 token 在 `res.access_token`。
|
||||||
|
- 支持 token 在 `res.data.access_token`。
|
||||||
|
- 刷新 token 成功后同步更新:
|
||||||
|
- `accessToken`
|
||||||
|
- `refreshToken`
|
||||||
|
- `refresh_token_expries`
|
||||||
|
- 如果刷新接口成功但没有返回 `access_token`,主动抛错,避免写入 `undefined` token。
|
||||||
|
|
||||||
|
验证情况:
|
||||||
|
|
||||||
|
- 已检查相关 diff,未做全文件重构。
|
||||||
|
- `pages/home/home.vue` 原文件为 CRLF 行尾;为避免整文件行尾改造造成大面积 diff,本次保留原行尾。
|
||||||
|
- 项目 `package.json` 未配置 build/lint 脚本,未执行完整构建校验。
|
||||||
|
|
@ -401,24 +401,11 @@ export default {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onShow() {
|
onShow() {
|
||||||
// 登录判定逻辑
|
this.refreshPageState();
|
||||||
this.isLogin = uni.getStorageSync("accessToken") ? true : false;
|
|
||||||
if (this.isLogin) {
|
|
||||||
this.getUserInfo();
|
|
||||||
}
|
|
||||||
// 通知、品牌、技能 这三个接口不强依赖登录,无论登没登录都获取
|
|
||||||
this.fetchNoticeList();
|
|
||||||
this.fetchBrandList();
|
|
||||||
this.fetchSkillList();
|
|
||||||
},
|
},
|
||||||
onPullDownRefresh() {
|
onPullDownRefresh() {
|
||||||
// 1. 重新请求首页需要的所有数据
|
// 1. 重新请求首页需要的所有数据
|
||||||
if (this.isLogin) {
|
this.refreshPageState();
|
||||||
this.getUserInfo();
|
|
||||||
}
|
|
||||||
this.fetchNoticeList();
|
|
||||||
this.fetchBrandList();
|
|
||||||
this.fetchSkillList();
|
|
||||||
|
|
||||||
// 2. 停止下拉刷新的动画
|
// 2. 停止下拉刷新的动画
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
|
@ -427,6 +414,10 @@ export default {
|
||||||
},
|
},
|
||||||
onLoad() {
|
onLoad() {
|
||||||
uni.hideTabBar();
|
uni.hideTabBar();
|
||||||
|
uni.$on('appShowRefresh', this.refreshPageState);
|
||||||
|
},
|
||||||
|
onUnload() {
|
||||||
|
uni.$off('appShowRefresh', this.refreshPageState);
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
'orderDetail.score'() {
|
'orderDetail.score'() {
|
||||||
|
|
@ -434,6 +425,22 @@ export default {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
refreshPageState() {
|
||||||
|
const hasToken = !!uni.getStorageSync("accessToken");
|
||||||
|
this.isLogin = hasToken;
|
||||||
|
if (hasToken) {
|
||||||
|
this.getUserInfo();
|
||||||
|
} else {
|
||||||
|
this.userInfo = {};
|
||||||
|
this.authDetails = null;
|
||||||
|
this.isUserInfoLoaded = false;
|
||||||
|
this.sjId = null;
|
||||||
|
}
|
||||||
|
// 通知、品牌、技能 这三个接口不强依赖登录,无论登没登录都获取
|
||||||
|
this.fetchNoticeList();
|
||||||
|
this.fetchBrandList();
|
||||||
|
this.fetchSkillList();
|
||||||
|
},
|
||||||
// 金额转换万单位处理
|
// 金额转换万单位处理
|
||||||
formatMoney(amount) {
|
formatMoney(amount) {
|
||||||
let num = parseFloat(amount);
|
let num = parseFloat(amount);
|
||||||
|
|
@ -589,9 +596,11 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
getUserInfo() {
|
getUserInfo() {
|
||||||
request.post("/sj/user/getUser").then((result) => {
|
this.isUserInfoLoaded = false;
|
||||||
|
return request.post("/sj/user/getUser").then((result) => {
|
||||||
if (result.code == 200 && result.data) {
|
if (result.code == 200 && result.data) {
|
||||||
this.userInfo = result.data;
|
this.userInfo = result.data;
|
||||||
|
this.isLogin = true;
|
||||||
this.isUserInfoLoaded = true; // 数据回来了,允许相关 UI 进行渲染
|
this.isUserInfoLoaded = true; // 数据回来了,允许相关 UI 进行渲染
|
||||||
uni.setStorageSync("sjId", result.data.id);
|
uni.setStorageSync("sjId", result.data.id);
|
||||||
this.sjId = result.data.id;
|
this.sjId = result.data.id;
|
||||||
|
|
@ -607,6 +616,15 @@ export default {
|
||||||
this.fetchAuthDetails();
|
this.fetchAuthDetails();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}).catch((err) => {
|
||||||
|
if (!uni.getStorageSync("accessToken")) {
|
||||||
|
this.isLogin = false;
|
||||||
|
this.userInfo = {};
|
||||||
|
this.authDetails = null;
|
||||||
|
this.sjId = null;
|
||||||
|
}
|
||||||
|
this.isUserInfoLoaded = false;
|
||||||
|
console.error("首页获取商家信息失败:", err);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -279,9 +279,18 @@ async function refreshToken() {
|
||||||
})
|
})
|
||||||
|
|
||||||
if (res.state == 1 || res.code == 200) {
|
if (res.state == 1 || res.code == 200) {
|
||||||
uni.setStorageSync('accessToken', res.access_token)
|
const tokenData = res.data || res
|
||||||
uni.setStorageSync('refreshToken', res.refresh_token)
|
if (!tokenData.access_token) {
|
||||||
return res.access_token
|
throw new Error('Refresh token response missing access_token')
|
||||||
|
}
|
||||||
|
uni.setStorageSync('accessToken', tokenData.access_token)
|
||||||
|
if (tokenData.refresh_token) {
|
||||||
|
uni.setStorageSync('refreshToken', tokenData.refresh_token)
|
||||||
|
}
|
||||||
|
if (tokenData.refresh_token_expries) {
|
||||||
|
uni.setStorageSync('refresh_token_expries', tokenData.refresh_token_expries)
|
||||||
|
}
|
||||||
|
return tokenData.access_token
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error('Refresh token failed')
|
throw new Error('Refresh token failed')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue