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

80 lines
2.8 KiB
TypeScript
Raw Normal View History

export {}
import { request } from '../../utils/requestPromise'
2026-09-09 22:05:00 +08:00
const app = getApp<{ globalData: { apiBase: string; token: string } }>();
Page({
data: {
serviceType: '',
serviceTypeLabel: '',
modes: ['online', 'offline', 'phone'],
modeLabels: ['线上视频', '线下面谈', '电话沟通'],
2026-09-09 22:05:00 +08:00
pModeIndex: 0,
preferredTime: '',
summary: '',
submitting: false,
errorMsg: ''
2026-09-09 22:05:00 +08:00
},
onLoad(options: any) {
if (options.type) {
const typeMap: Record<string, string> = {
relationship_guidance: '恋爱困惑指导',
emotional_support: '情感支持',
premarital_guidance: '婚前辅导',
anti_fraud_guidance: '婚恋反诈指导'
};
this.setData({ serviceType: options.type, serviceTypeLabel: typeMap[options.type] || options.type });
}
},
onModeChange(e: any) {
this.setData({ pModeIndex: e.detail.value });
},
onTimeInput(e: any) { this.setData({ preferredTime: e.detail.value }); },
onSummaryInput(e: any) { this.setData({ summary: e.detail.value }); },
async onSubmit() {
const { serviceType, pModeIndex, preferredTime, summary } = this.data;
if (!serviceType) {
wx.showToast({ title: '请选择服务类型', icon: 'none' });
return;
}
if (!summary || !summary.trim()) {
wx.showToast({ title: '请简要描述您的困扰', icon: 'none' });
return;
}
this.setData({ submitting: true, errorMsg: '' });
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`,
method: 'POST',
header: { 'Authorization': `Bearer ${app.globalData.token}`, 'Content-Type': 'application/json' },
data: {
serviceType,
// 字段名与后端 ApplyRequest 对齐(此前用 preferredServiceMode / userRequestSummary 导致整条链路拿不到值)
preferredMode: this.data.modes[pModeIndex],
preferredTime,
summary,
requestSource: 'user_self',
// 幂等键:同一次填写重复提交不会产生两条申请
idempotencyKey: `counseling-${serviceType}-${Date.now()}`
}
2026-09-09 22:05:00 +08:00
});
if (res.statusCode === 200 && res.data && res.data.success) {
wx.showToast({ title: '申请已提交', icon: 'success' });
setTimeout(() => wx.redirectTo({ url: '/pages/my-counseling/my-counseling' }), 1500);
} else {
this.setData({ errorMsg: (res.data && res.data.message) || '提交失败,请重试' });
wx.showToast({ title: (res.data && res.data.message) || '提交失败', icon: 'none' });
}
2026-09-09 22:05:00 +08:00
} catch (e) {
this.setData({ errorMsg: '网络异常,请稍后重试' });
wx.showToast({ title: '网络异常,请稍后重试', icon: 'none' });
2026-09-09 22:05:00 +08:00
} finally {
this.setData({ submitting: false });
}
}
});