Files
persionalBrand/packages/template-acheng-campus-food/miniapp/pages/inquiry/index.js
T
que01 55989a5dfe fix: 修复模板页面跳转并移除无入口的下单页
新增运行时 openPage/takePageQuery,tabBar 页自动使用 switchTab 并传递参数
- 编译器写入 tabPages,并拒绝 navigateTo/redirectTo 到 tabBar 页或 switchTab 带参数
- 阿成首页、套餐、出摊、主理人、详情、记录、成功页跳转统一改用 openPage
- 套餐筛选改用后台业务提供项分类;详情跳咨询时携带业务提供项
- 移除 capability-booking:小程序无入口且依赖已删除的 /public/brand,需求由咨询表单覆盖"
2026-09-27 10:10:23 +08:00

132 lines
4.6 KiB
JavaScript

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: "",
},
onLoad(options) {
this.offeringId = options.offeringId || "";
loadWorkspaceWithFallback()
.then((workspace) => {
this.forms = workspace.inquiryForms || [];
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,
});
},
});