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: {}, displayValues: {}, formId: "", submitting: false, formError: "", presentation: {}, }, onLoad(options) { this.offeringId = options?.offeringId || ""; return 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) { // 原型中的类型选项为按钮网格,人数/日期和预算/配送各占半行;数据与文案仍来自已发布表单。 const fields = (form?.fields || []).map((field) => ({ ...field, useChips: field.key === "type" && (field.type === "select" || field.type === "radio"), halfWidth: ["people", "date", "budget", "delivery"].includes(field.key), })); this.setData({ formId, fields, values: {}, displayValues: {} }); } }, 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" }); }, chooseChip(e) { const { key, value } = e.currentTarget.dataset; this.setData({ ["values." + key]: value }); }, 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, ["displayValues." + field.key]: option.label, }); }, checkboxChange(e) { this.setData({ ["values." + e.currentTarget.dataset.key]: e.detail.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] === "" || (Array.isArray(values[field.key]) && !values[field.key].length)), ); 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, }); }, });