import { request } from '../../utils/requestPromise' const app = getApp<{ globalData: { apiBase: string; token: string } }>(); Page({ data: { list: [] as any[], loading: false, errorMsg: '', /** 权限不足 / 未登录时给出明确提示,而不是静默空白 */ forbidden: false }, onLoad() { this.loadList(); }, onShow() { this.loadList(); }, async loadList() { this.setData({ loading: true, errorMsg: '', forbidden: false }); try { const res = await request({ url: `${app.globalData.apiBase}/counseling/services`, header: { 'Authorization': `Bearer ${app.globalData.token}` } }); if (res.statusCode === 401) { this.setData({ forbidden: true, errorMsg: '登录状态已失效,请重新登录' }); return; } if (res.statusCode === 200 && res.data && res.data.success) { const typeMap: Record = { relationship_guidance: '恋爱困惑指导', emotional_support: '情感支持', premarital_guidance: '婚前辅导', anti_fraud_guidance: '婚恋反诈指导' }; const list = (res.data.data || []).map((item: any) => ({ ...item, serviceTypeName: typeMap[item.serviceType] || item.serviceType, statusName: this.statusLabel(item.status), createdText: this.formatTime(item.createdAt), scheduledText: item.scheduledStartAt ? this.formatTime(item.scheduledStartAt) : '', canCancel: item.status === 'pending' || item.status === 'assigned' || item.status === 'scheduled' })); this.setData({ list }); } else { this.setData({ errorMsg: (res.data && res.data.message) || '加载失败,请下拉重试' }); } } catch (e) { this.setData({ errorMsg: '网络异常,请下拉重试' }); } finally { this.setData({ loading: false }); } }, onPullDownRefresh() { this.loadList().then(() => wx.stopPullDownRefresh()); }, goToIntro() { wx.navigateTo({ url: '/pages/counseling-intro/counseling-intro' }); }, /** 详情:后端已提供 GET /counseling/services/{id},此处直接展开行内摘要,避免跳未实现页面 */ toggleExpand(e: any) { const id = e.currentTarget.dataset.id; const list = this.data.list.map((it: any) => it.id === id ? { ...it, expanded: !it.expanded } : it); this.setData({ list }); }, async onCancel(e: any) { const id = e.currentTarget.dataset.id; const confirm = await new Promise((resolve) => { wx.showModal({ title: '取消申请', content: '确认取消本次咨询申请?', success: (r) => resolve(!!r.confirm), fail: () => resolve(false) }); }); if (!confirm) return; try { const res = await request({ url: `${app.globalData.apiBase}/counseling/services/${id}/cancel`, method: 'POST', header: { 'Authorization': `Bearer ${app.globalData.token}`, 'Content-Type': 'application/json' }, data: { reason: '用户主动取消' } }); if (res.statusCode === 200 && res.data && res.data.success) { wx.showToast({ title: '已取消', icon: 'success' }); this.loadList(); } else { wx.showToast({ title: (res.data && res.data.message) || '取消失败', icon: 'none' }); } } catch (err) { wx.showToast({ title: '网络异常,请稍后重试', icon: 'none' }); } }, /** INV-005:婚前辅导伴侣**本人**确认 —— 只有受邀伴侣本人可操作 */ async onConfirmCompanion(e: any) { const id = e.currentTarget.dataset.id; const confirm = await new Promise((resolve) => { wx.showModal({ title: '确认参与婚前辅导', content: '确认作为伴侣参与本次婚前辅导?', success: (r) => resolve(!!r.confirm), fail: () => resolve(false) }); }); if (!confirm) return; try { const res = await request({ url: `${app.globalData.apiBase}/counseling/services/${id}/companion-confirm`, method: 'POST', header: { 'Authorization': `Bearer ${app.globalData.token}`, 'Content-Type': 'application/json' }, data: {} }); if (res.statusCode === 200 && res.data && res.data.success) { wx.showToast({ title: '已确认参与', icon: 'success' }); this.loadList(); } else { wx.showToast({ title: (res.data && res.data.message) || '确认失败', icon: 'none' }); } } catch (err) { wx.showToast({ title: '网络异常,请稍后重试', icon: 'none' }); } }, statusLabel(s: string) { const m: Record = { pending: '待处理', assigned: '已分配', scheduled: '已排期', in_progress: '服务中', completed: '已完成', cancelled: '已取消', closed: '已关闭' }; return m[s] || s; }, formatTime(t: string) { return t ? new Date(t).toLocaleString('zh-CN') : ''; }, formatDate(t: string) { return t ? new Date(t).toLocaleDateString('zh-CN') : ''; } });