const app = getApp(); const planSvc = require('../../services/plan'); const EMOJIS = ['📋', '💪', '🔥', '🏋️', '🏃', '🧘', '🤸', '🥊', '🚴', '⚡', '🌟', '🎯']; const COLORS = ['#D9B36C', '#58C9B9', '#9B8CFF', '#F08A5D', '#3FBF7F', '#5B9DF9']; Page({ data: { statusBarHeight: 20, mode: 'new', id: '', name: '', description: '', emoji: '📋', color: '#D9B36C', emojis: EMOJIS, colors: COLORS, loading: false, dirty: false // 是否有未保存的修改 }, onLoad(query) { const mode = query.mode === 'edit' ? 'edit' : 'new'; const id = query.id || ''; this.setData({ statusBarHeight: app.globalData.statusBarHeight || 20, mode, id }); if (mode === 'edit' && id) { this.setData({ loading: true }); planSvc.get(id).then((p) => { this.setData({ name: p.name || '', description: p.description || '', emoji: p.emoji || '📋', color: p.color || '#D9B36C', loading: false }); }).catch(() => { this.setData({ loading: false }); }); } }, onName(e) { this.setData({ name: e.detail.value, dirty: true }); }, onDesc(e) { this.setData({ description: e.detail.value, dirty: true }); }, onPickEmoji(e) { this.setData({ emoji: e.currentTarget.dataset.emoji, dirty: true }); }, onPickColor(e) { this.setData({ color: e.currentTarget.dataset.color, dirty: true }); }, // 点返回键(navbar 拦截模式)触发:有未保存改动时先确认 onBackRequest() { if (!this.data.dirty) { wx.navigateBack(); return; } wx.showModal({ title: '放弃编辑?', content: '当前修改尚未保存,确定要退出吗?', confirmText: '退出', cancelText: '继续编辑', success: (res) => { if (res.confirm) wx.navigateBack(); } }); }, onSave() { const name = (this.data.name || '').trim(); if (!name) { wx.showToast({ title: '请填写计划名称', icon: 'none' }); return; } const dto = { name, description: (this.data.description || '').trim(), emoji: this.data.emoji, color: this.data.color }; wx.showLoading({ title: '保存中…', mask: true }); const op = this.data.mode === 'edit' ? planSvc.update(this.data.id, dto) : planSvc.create(dto); op.then(() => { wx.hideLoading(); wx.showToast({ title: '已保存', icon: 'success' }); setTimeout(() => wx.navigateBack(), 500); }).catch(() => { wx.hideLoading(); }); } });