92 lines
3.6 KiB
JavaScript
92 lines
3.6 KiB
JavaScript
const { loadWorkspaceWithFallback } = require("../../utils/runtime-workspace");
|
|
const { request, customerRequest, customerTokenKey } = require("../../utils/request");
|
|
Page({
|
|
data: {
|
|
offering: null,
|
|
submitting: false,
|
|
eyebrow: "CONSULTATION",
|
|
title: "需求咨询",
|
|
subtitle: "提交信息后由主理人与你联系",
|
|
submitLabel: "提交需求",
|
|
presentation: {},
|
|
values: {},
|
|
fields: [],
|
|
formId: "",
|
|
error: "",
|
|
},
|
|
onLoad(options) {
|
|
loadWorkspaceWithFallback()
|
|
.then((workspace) => {
|
|
const offering =
|
|
(workspace.offerings || []).find((item) => item.id === options.offeringId) || null;
|
|
const forms = workspace.inquiryForms || [];
|
|
const form =
|
|
forms.find((item) => item.targetId && item.targetId === options.offeringId) ||
|
|
forms.find((item) => item.target === "brand") ||
|
|
forms[0];
|
|
const isQuote =
|
|
form?.purpose === "quote-request" ||
|
|
(workspace.plugins || []).some((plugin) => plugin.key === "quote-request");
|
|
const presentation = workspace.brand?.visualPresentation?.inquiry || {};
|
|
this.setData({
|
|
offering,
|
|
fields: form?.fields || [],
|
|
formId: form?.key || form?.id || "",
|
|
title: isQuote ? "获取报价方案" : presentation.title || "需求咨询",
|
|
eyebrow: isQuote ? "QUOTE REQUEST" : presentation.eyebrow || "CONSULTATION",
|
|
subtitle: presentation.subtitle || "提交信息后由主理人与你联系",
|
|
submitLabel: isQuote ? "提交报价需求" : presentation.submitLabel || "提交需求",
|
|
presentation,
|
|
});
|
|
})
|
|
.catch((error) => this.setData({ error: error.message }));
|
|
},
|
|
onInput(e) {
|
|
this.setData({ ["values." + e.currentTarget.dataset.key]: e.detail.value });
|
|
},
|
|
onComponentInput(e) {
|
|
this.setData({ ["values." + e.detail.key]: e.detail.value });
|
|
},
|
|
submit() {
|
|
const values = this.data.values;
|
|
const missing = this.data.fields.find(
|
|
(field) => field.required && (values[field.key] === undefined || values[field.key] === ""),
|
|
);
|
|
if (missing) return wx.showToast({ title: "请填写" + missing.label, icon: "none" });
|
|
if (!this.data.formId) return wx.showToast({ title: "咨询表单暂不可用", icon: "none" });
|
|
this.setData({ submitting: true });
|
|
const send = () =>
|
|
customerRequest("/customer/inquiry-submissions", {
|
|
method: "POST",
|
|
data: { formId: this.data.formId, values },
|
|
}).then((submission) => {
|
|
const record = {
|
|
id: submission.id,
|
|
offeringName: this.data.offering ? this.data.offering.name : "品牌合作",
|
|
type: this.data.title,
|
|
project: "已提交需求",
|
|
status: submission.status === "new" ? "待处理" : submission.status,
|
|
createdAt: submission.createdAt,
|
|
};
|
|
wx.setStorageSync("latestInquirySubmission", record);
|
|
this.setData({ submitting: false });
|
|
wx.redirectTo({ url: "/pages/inquiry-success/index?id=" + submission.id });
|
|
});
|
|
const fail = (error) => {
|
|
this.setData({ submitting: false });
|
|
wx.showToast({ title: error.message || "提交失败,请重试", icon: "none" });
|
|
};
|
|
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,
|
|
});
|
|
},
|
|
});
|