- 8 个页面 exercise-card 由 bind:tap 改为 catch:tap,拦截原生 tap 冒泡 避免一次点击被『自定义事件 + 原生冒泡』触发两次, 第二次 e.detail 无 id → navigateTo detail?id=undefined → 404 - 所有 onExercise 顶部加 id 守卫,无 id 直接 return - exercise-card 点击前校验 exercise.id,缺失不触发 - detail onLoad 的 id 为空时直接显示空态,不再发起错误请求
76 lines
2.0 KiB
JavaScript
76 lines
2.0 KiB
JavaScript
const app = getApp();
|
|
const planSvc = require('../../services/plan');
|
|
|
|
Page({
|
|
data: {
|
|
statusBarHeight: 20,
|
|
id: '',
|
|
plan: null,
|
|
exercises: [],
|
|
loading: true
|
|
},
|
|
|
|
onLoad(query) {
|
|
const id = query.id || '';
|
|
this.setData({ statusBarHeight: app.globalData.statusBarHeight || 20, id });
|
|
this.load();
|
|
},
|
|
|
|
load() {
|
|
this.setData({ loading: true });
|
|
planSvc.get(this.data.id).then((plan) => {
|
|
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 },
|
|
exercises: (plan && plan.exercises) || [],
|
|
loading: false
|
|
});
|
|
}).catch(() => {
|
|
this.setData({ loading: false });
|
|
});
|
|
},
|
|
|
|
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: '#E5544B',
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
planSvc.remove(this.data.id).then(() => {
|
|
wx.showToast({ title: '已删除', icon: 'success' });
|
|
setTimeout(() => wx.navigateBack(), 500);
|
|
}).catch(() => {});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|