Files
fitness-coach/miniprogram/pages/plan-detail/plan-detail.js
wm 7ff55a6257 fix(plan-detail): onShow 重载,修复从「添加动作」页返回后列表不刷新
- 根因:plan-detail 仅在 onLoad 加载数据;从 search(selectMode) 选完动作返回时
  onLoad 不再触发,导致新增的动作不显示,用户感知为『选择动作后返回页面没更新』。
- 修复:load() 从 onLoad 移至 onShow,首次进入与返回均会刷新;onLoad 仅做参数初始化。
2026-07-23 13:31:48 +08:00

102 lines
3.0 KiB
JavaScript

const app = getApp();
const planSvc = require('../../services/plan');
const { iconName } = require('../../utils/icon-name');
Page({
data: {
statusBarHeight: 20,
id: '',
plan: null,
exercises: [],
loading: true,
error: false,
errorMsg: ''
},
onLoad(query) {
const id = query.id || '';
const h = (app && app.globalData && app.globalData.statusBarHeight) || 20;
this.setData({ statusBarHeight: h, id });
},
onShow() {
// 首次进入与从「添加动作」/「编辑」页返回都会触发,保证选完动作后列表实时刷新
if (this.data.id) this.load();
},
load() {
const id = this.data.id;
if (!id) {
this.setData({ loading: false, error: true, errorMsg: '缺少计划标识,无法打开' });
return;
}
this.setData({ loading: true, error: false });
planSvc.get(id).then((plan) => {
if (!plan || !plan.id) {
this.setData({ loading: false, error: true, errorMsg: '未找到该计划' });
return;
}
const raw = plan && plan.updatedAt;
const updatedAtText = typeof raw === 'string'
? raw.slice(0, 10)
: (raw ? String(raw).slice(0, 10) : '');
this.setData({
plan: { ...plan, updatedAtText, icon: iconName(plan.emoji) },
exercises: (plan && plan.exercises) || [],
loading: false,
error: false
});
}).catch((err) => {
const status = err && err.statusCode;
const msg = status === 404
? '未找到该计划'
: (status === 401 ? '登录已失效,请重新登录' : '加载失败,请检查网络后重试');
this.setData({ loading: false, error: true, errorMsg: msg });
});
},
onRetry() {
this.load();
},
onRemoveExercise(e) {
const exerciseId = e.currentTarget.dataset.id;
planSvc.removeExercise(this.data.id, exerciseId).then(() => {
this.setData({ exercises: this.data.exercises.filter(i => i.id !== exerciseId) });
wx.showToast({ title: '已移除', icon: 'none' });
}).catch(() => {});
},
onExercise(e) {
if (!e || !e.detail || !e.detail.id) return;
wx.navigateTo({
url: '/pages/detail/detail?planId=' + encodeURIComponent(this.data.id) +
'&id=' + encodeURIComponent(e.detail.id)
});
},
onAdd() {
wx.navigateTo({ url: '/pages/search/search?mode=select&planId=' + encodeURIComponent(this.data.id) });
},
onEdit() {
wx.navigateTo({ url: '/pages/plan-edit/plan-edit?mode=edit&id=' + encodeURIComponent(this.data.id) });
},
onDelete() {
wx.showModal({
title: '删除计划',
content: '确定要删除该训练计划吗?此操作不可恢复。',
confirmColor: '#FF5C5C',
success: (res) => {
if (res.confirm) {
planSvc.remove(this.data.id).then(() => {
wx.showToast({ title: '已删除', icon: 'success' });
setTimeout(() => wx.navigateBack(), 500);
}).catch(() => {});
}
}
});
}
});