const { loadWorkspaceWithFallback } = require("../../utils/runtime-workspace"); const { request, customerRequest, customerTokenKey } = require("../../utils/request"); const { takePageQuery } = require("../../utils/navigation"); const labels = { new: "待处理", reviewing: "沟通中", replied: "已回复", qualified: "已确认", converted: "已转化", closed: "已关闭", }; /** 按提交时的表单快照把记录还原成「标签:值」,后台以后改字段,历史记录仍然可读。 */ function summarize(item) { const fields = Array.isArray(item.fieldsSnapshot) ? item.fieldsSnapshot : []; const values = item.values || {}; return fields .filter((field) => values[field.key] !== undefined && values[field.key] !== "") .map((field) => ({ key: field.key, label: field.label || field.key, value: values[field.key], })); } Page({ data: { subView: "form", records: [], // 字段全部来自后台已发布的咨询表单,模板只负责呈现。 fields: [], values: {}, formId: "", submitting: false, formError: "", presentation: {}, }, onLoad(options) { this.offeringId = options.offeringId || ""; loadWorkspaceWithFallback() .then((workspace) => { this.forms = workspace.inquiryForms || []; this.setData({ presentation: workspace.brand?.visualPresentation?.inquiry || {} }); this.selectForm(); }) .catch((error) => this.setData({ formError: error.message })); }, /** 按目标业务提供项选择表单;没有专属表单时回退到品牌表单。 */ selectForm() { const forms = this.forms || []; const form = (this.offeringId && forms.find((item) => item.targetId === this.offeringId)) || forms.find((item) => item.target === "brand") || forms[0]; const formId = form?.key || form?.id || ""; if (formId !== this.data.formId) this.setData({ formId, fields: form?.fields || [], values: {} }); }, onShow() { // 本页是 tabBar 页,从详情页进入时参数经 openPage 暂存,而不是通过 onLoad 传入。 const query = takePageQuery("pages/inquiry/index"); if (query.offeringId) { this.offeringId = query.offeringId; this.setData({ subView: "form" }); if (this.forms) this.selectForm(); } this.loadRecords(); }, loadRecords() { if (!wx.getStorageSync(customerTokenKey())) return; customerRequest("/customer/inquiry-submissions") .then((items) => this.setData({ records: (items || []).map((item) => ({ ...item, title: item.inquiryForm?.name || "品牌咨询", summary: summarize(item), status: labels[item.status] || item.status, replyNote: item.replyNote, createdAt: new Date(item.createdAt).toLocaleDateString("zh-CN"), })), }), ) .catch(() => {}); }, switchView(e) { this.setData({ subView: e.currentTarget.dataset.view }); }, switchToForm() { this.setData({ subView: "form" }); }, choose(e) { const field = this.data.fields.find((item) => item.key === e.currentTarget.dataset.key); const option = field?.options?.[Number(e.detail.value)]; if (option) this.setData({ ["values." + field.key]: option.value }); }, input(e) { this.setData({ ["values." + e.currentTarget.dataset.key]: e.detail.value }); }, submit() { const { values, formId, fields } = this.data; const missing = fields.find( (field) => field.required && (values[field.key] === undefined || values[field.key] === ""), ); if (missing) return this.setData({ formError: "请填写" + missing.label }); if (!formId) return this.setData({ formError: "咨询表单暂不可用,请稍后重试" }); this.setData({ submitting: true, formError: "" }); const send = () => customerRequest("/customer/inquiry-submissions", { method: "POST", data: { formId, values }, }).then((result) => { wx.setStorageSync("latestInquirySubmission", result); this.setData({ submitting: false, subView: "history" }); this.loadRecords(); wx.showToast({ title: "咨询已提交", icon: "none" }); }); const fail = (error) => this.setData({ submitting: false, formError: error.message || "提交失败,请重试" }); if (wx.getStorageSync(customerTokenKey())) return send().catch(fail); wx.login({ success: (login) => request("/customer/session/wechat", { method: "POST", data: { code: login.code } }) .then((result) => { wx.setStorageSync(customerTokenKey(), result.token); return send(); }) .catch(fail), fail, }); }, });