perf(fe): 修复 token 过期白屏、请求翻倍与失败伪装成空态
token 过期后小程序静默白屏(影响所有用户) - JWT 有效期 7 天,过期后 40100 只 clearToken 不重新登录,而 store.ready 的 promise 已缓存不会重跑,加上 23 处静默 catch, 用户看到的是「什么都没有、也没有任何提示」,只能重启小程序 - request 层加 token 失效钩子:自动重新登录并重试一次(只重试一次, 防止登录接口本身故障时死循环);store 注册钩子重置引导缓存 - 补 15s 请求超时、60s 上传超时,超时给明确文案(原先默认 60s 卡住无反馈) 请求翻倍 - 每个 tab 页同时用 store.subscribe 和 onShow→ready().then() 加载, 而 ready() 内部 loadPets 会 notify 触发 subscriber,首次进入请求翻倍 - subscriber 改为只处理「切换宠物 / 数据变更」,首次加载交给 onShow - 首页 bind:save 与组件实际触发的 saved 事件名不一致,onSheetSave 从未生效;一并修正,并去掉关闭弹层时的全量重载 失败伪装成空态 - 社区请求失败时显示「还没有帖子,来发第一条吧」,把故障说成没数据。 改为区分「加载失败(可点击重试)」「关注 tab 为空」「确实没有帖子」 - 新增 utils/ui.js 统一错误提示(同文案 3 秒内去重,避免并发失败刷屏), 首页与记录页主数据加载失败不再静默 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const store = require('../../utils/store.js');
|
||||
const api = require('../../utils/api.js');
|
||||
const { toastErr } = require('../../utils/ui.js');
|
||||
|
||||
const TAG_CLASS = { 求助: 'warn', 经验: 'blue', 精选: 'purple', 避坑: 'red', 晒宠: '' };
|
||||
|
||||
@@ -33,6 +34,8 @@ Page({
|
||||
sheetShow: false,
|
||||
sheetType: '',
|
||||
sheetPostId: '',
|
||||
loaded: false,
|
||||
loadErr: '',
|
||||
},
|
||||
onLoad() {
|
||||
this._unsub = store.subscribe((pet) => this.setData({ pet }));
|
||||
@@ -54,10 +57,15 @@ Page({
|
||||
},
|
||||
loadPosts() {
|
||||
const tab = this.data.feedTabs[this.data.feedIdx];
|
||||
this.setData({ loadErr: '' });
|
||||
api
|
||||
.listPosts(tab, 1)
|
||||
.then((page) => this.setData({ posts: (page.list || []).map(mapPost) }))
|
||||
.catch(() => {});
|
||||
.then((page) => this.setData({ posts: (page.list || []).map(mapPost), loaded: true }))
|
||||
.catch((e) => {
|
||||
// 不能让「加载失败」显示成「还没有帖子」,那是在骗用户
|
||||
this.setData({ loadErr: e.message || '加载失败', loaded: true });
|
||||
toastErr(e);
|
||||
});
|
||||
},
|
||||
switchFeedTab(e) {
|
||||
this.setData({ feedIdx: e.currentTarget.dataset.index });
|
||||
|
||||
@@ -41,7 +41,13 @@ module.exports.isUrl = function (s) { return s && s.indexOf('http') === 0; }
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view wx:if="{{posts.length === 0}}" class="empty-hint">还没有帖子,来发第一条吧 🐾</view>
|
||||
<view wx:if="{{loadErr}}" class="empty-hint">
|
||||
{{loadErr}}
|
||||
<view class="retry-btn" bindtap="loadPosts">点击重试</view>
|
||||
</view>
|
||||
<view wx:elif="{{loaded && posts.length === 0}}" class="empty-hint">
|
||||
{{feedTabs[feedIdx] === '关注' ? '还没有关注的人,去「推荐」里关注几个宠友吧 🐾' : '还没有帖子,来发第一条吧 🐾'}}
|
||||
</view>
|
||||
</view>
|
||||
</scroll-view>
|
||||
|
||||
|
||||
@@ -53,3 +53,4 @@ page{height:100vh;overflow:hidden;display:flex;flex-direction:column}
|
||||
color:var(--primary-dark);background:#FFF2DC;border:1rpx solid #FFD39A;
|
||||
}
|
||||
.follow-btn.on{color:var(--muted);background:#F4F1EC;border-color:var(--line)}
|
||||
.retry-btn{margin-top:20rpx;display:inline-block;padding:12rpx 36rpx;border-radius:999rpx;background:#FFF2DC;color:var(--primary-dark);font-weight:700;font-size:26rpx}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const store = require('../../utils/store.js');
|
||||
const api = require('../../utils/api.js');
|
||||
const { toastErr } = require('../../utils/ui.js');
|
||||
|
||||
function todayStr() {
|
||||
const d = new Date();
|
||||
@@ -34,6 +35,8 @@ Page({
|
||||
this.setData({ todayDate: todayStr() });
|
||||
this._unsub = store.subscribe((pet) => {
|
||||
this.setData({ pet });
|
||||
// 首次由 onShow 统一加载,这里只处理之后的「切换宠物 / 数据变更」,避免重复请求
|
||||
if (!this._inited) return;
|
||||
this.loadAll();
|
||||
});
|
||||
},
|
||||
@@ -47,6 +50,7 @@ Page({
|
||||
store
|
||||
.ready()
|
||||
.then(() => {
|
||||
this._inited = true;
|
||||
this.setData({ pet: store.getPet() });
|
||||
this.loadAll();
|
||||
})
|
||||
@@ -72,7 +76,7 @@ Page({
|
||||
api
|
||||
.homeSummary(id)
|
||||
.then((summary) => this.setData({ summary }))
|
||||
.catch(() => {});
|
||||
.catch((e) => toastErr(e));
|
||||
},
|
||||
loadTasks() {
|
||||
const id = store.currentPetId();
|
||||
@@ -81,7 +85,7 @@ Page({
|
||||
api
|
||||
.getTasks(id, date)
|
||||
.then((tasks) => this.setData({ tasks: (tasks || []).map(mapTask) }))
|
||||
.catch(() => {});
|
||||
.catch((e) => toastErr(e));
|
||||
},
|
||||
onTapDay(e) {
|
||||
const { date, active } = e.currentTarget.dataset;
|
||||
@@ -130,8 +134,9 @@ Page({
|
||||
this.setData({ sheetType: type, sheetShow: true });
|
||||
},
|
||||
closeSheet() {
|
||||
// 只关闭。数据变更由 bind:saved 或 store 的宠物变更通知触发刷新,
|
||||
// 不必每次关弹层(哪怕只是看了眼 AI)都全量重拉一遍
|
||||
this.setData({ sheetShow: false });
|
||||
this.loadAll();
|
||||
},
|
||||
onSheetSave() {
|
||||
this.loadTasks();
|
||||
|
||||
@@ -97,4 +97,4 @@
|
||||
</scroll-view>
|
||||
|
||||
<fab bind:tap="onFab"></fab>
|
||||
<bottom-sheet show="{{sheetShow}}" type="{{sheetType}}" bind:close="closeSheet" bind:save="onSheetSave"></bottom-sheet>
|
||||
<bottom-sheet show="{{sheetShow}}" type="{{sheetType}}" bind:close="closeSheet" bind:saved="onSheetSave"></bottom-sheet>
|
||||
|
||||
@@ -46,6 +46,7 @@ Page({
|
||||
onLoad() {
|
||||
this._unsub = store.subscribe((pet) => {
|
||||
this.setData({ pet, calLoaded: false });
|
||||
if (!this._inited) return; // 首次由 onShow 加载
|
||||
this.loadTimeline();
|
||||
if (this.data.planView === 'calendar') this.loadCalendar();
|
||||
});
|
||||
@@ -60,6 +61,7 @@ Page({
|
||||
store
|
||||
.ready()
|
||||
.then(() => {
|
||||
this._inited = true;
|
||||
this.setData({ pet: store.getPet() });
|
||||
this.loadTimeline();
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const store = require('../../utils/store.js');
|
||||
const api = require('../../utils/api.js');
|
||||
const { toastErr } = require('../../utils/ui.js');
|
||||
|
||||
function pad(n) {
|
||||
return n < 10 ? '0' + n : '' + n;
|
||||
@@ -90,6 +91,7 @@ Page({
|
||||
onLoad() {
|
||||
this._unsub = store.subscribe((pet) => {
|
||||
this.setData({ pet });
|
||||
if (!this._inited) return; // 首次由 onShow 加载
|
||||
this.loadRecords();
|
||||
});
|
||||
},
|
||||
@@ -103,6 +105,7 @@ Page({
|
||||
store
|
||||
.ready()
|
||||
.then(() => {
|
||||
this._inited = true;
|
||||
this.setData({ pet: store.getPet() });
|
||||
this.loadRecords();
|
||||
})
|
||||
@@ -145,7 +148,7 @@ Page({
|
||||
const list = (res.list || []).map(mapRecord);
|
||||
this.setData({ timeline: list, recPage: 1, recTotal: res.total || 0, recHasMore: list.length < (res.total || 0) });
|
||||
})
|
||||
.catch(() => {});
|
||||
.catch((e) => toastErr(e));
|
||||
this.loadTrend();
|
||||
},
|
||||
loadMoreRecords() {
|
||||
|
||||
@@ -14,6 +14,7 @@ Page({
|
||||
onLoad() {
|
||||
this._unsub = store.subscribe((pet) => {
|
||||
this.setData({ pet });
|
||||
if (!this._inited) return; // 首次由 onShow 加载
|
||||
this.loadData();
|
||||
});
|
||||
},
|
||||
@@ -27,6 +28,7 @@ Page({
|
||||
store
|
||||
.ready()
|
||||
.then(() => {
|
||||
this._inited = true;
|
||||
this.setData({ pet: store.getPet() });
|
||||
this.loadData();
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user