Files
fitness-coach/miniprogram/pages/search/search.js
wm 06bd96a473 fix: 修复动作卡片点击双触发导致跳 404
- 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 为空时直接显示空态,不再发起错误请求
2026-07-22 15:30:28 +08:00

72 lines
1.7 KiB
JavaScript

const app = getApp();
const svc = require('../../services/exercise');
const planSvc = require('../../services/plan');
Page({
data: {
statusBarHeight: 20,
q: '',
results: [],
loading: false,
searched: false,
selectMode: false,
planId: '',
navTitle: '搜索'
},
onLoad(query) {
this.setData({ statusBarHeight: app.globalData.statusBarHeight || 20 });
const selectMode = query.mode === 'select';
const planId = query.planId || '';
this.setData({
selectMode,
planId,
navTitle: selectMode ? '选择动作' : '搜索'
});
if (selectMode) {
wx.setNavigationBarTitle({ title: '选择动作' });
}
},
onInput(e) {
const q = e.detail.value;
this.setData({ q });
this.debouncedSearch(q);
},
debouncedSearch(q) {
if (this._timer) clearTimeout(this._timer);
this._timer = setTimeout(() => this.doSearch(q), 350);
},
doSearch(q) {
if (!q || !q.trim()) {
this.setData({ results: [], searched: false, loading: false });
return;
}
this.setData({ loading: true });
svc.search(q.trim()).then((res) => {
this.setData({
results: (res && res.items) || [],
searched: true,
loading: false
});
}).catch(() => {
this.setData({ searched: true, loading: false });
});
},
onExercise(e) {
const id = e.detail && e.detail.id;
if (!id) return;
if (this.data.selectMode && this.data.planId) {
planSvc.addExercise(this.data.planId, id).then(() => {
wx.showToast({ title: '已添加', icon: 'success' });
setTimeout(() => wx.navigateBack(), 500);
}).catch(() => {});
return;
}
wx.navigateTo({ url: '/pages/detail/detail?id=' + encodeURIComponent(id) });
}
});