Files
danyang-match/miniapp/pages/my-counseling/my-counseling.ts
T

134 lines
5.0 KiB
TypeScript
Raw Normal View History

import { request } from '../../utils/requestPromise'
2026-09-09 22:05:00 +08:00
const app = getApp<{ globalData: { apiBase: string; token: string } }>();
Page({
data: {
list: [] as any[],
loading: false,
errorMsg: '',
/** 权限不足 / 未登录时给出明确提示,而不是静默空白 */
forbidden: false
},
2026-09-09 22:05:00 +08:00
onLoad() { this.loadList(); },
onShow() { this.loadList(); },
async loadList() {
this.setData({ loading: true, errorMsg: '', forbidden: false });
2026-09-09 22:05:00 +08:00
try {
const res = await request<any>({
2026-09-09 22:05:00 +08:00
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) {
2026-09-09 22:05:00 +08:00
const typeMap: Record<string, string> = {
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'
}));
2026-09-09 22:05:00 +08:00
this.setData({ list });
} else {
this.setData({ errorMsg: (res.data && res.data.message) || '加载失败,请下拉重试' });
2026-09-09 22:05:00 +08:00
}
} catch (e) {
this.setData({ errorMsg: '网络异常,请下拉重试' });
} finally {
this.setData({ loading: false });
}
},
onPullDownRefresh() {
this.loadList().then(() => wx.stopPullDownRefresh());
2026-09-09 22:05:00 +08:00
},
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<boolean>((resolve) => {
wx.showModal({
title: '取消申请',
content: '确认取消本次咨询申请?',
success: (r) => resolve(!!r.confirm),
fail: () => resolve(false)
});
});
if (!confirm) return;
try {
const res = await request<any>({
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<boolean>((resolve) => {
wx.showModal({
title: '确认参与婚前辅导',
content: '确认作为伴侣参与本次婚前辅导?',
success: (r) => resolve(!!r.confirm),
fail: () => resolve(false)
});
});
if (!confirm) return;
try {
const res = await request<any>({
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' });
}
},
2026-09-09 22:05:00 +08:00
statusLabel(s: string) {
const m: Record<string, string> = {
pending: '待处理', assigned: '已分配', scheduled: '已排期',
in_progress: '服务中', completed: '已完成', cancelled: '已取消', closed: '已关闭'
};
2026-09-09 22:05:00 +08:00
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') : ''; }
});