feat: 按林境门作原型完善静态小程序
This commit is contained in:
1 parent
4afb6bbbe8
commit
8489c8ca79
26 files changed
+92
-964
No files matched your search
@@ -1,120 +1,7 @@
|
||||
const { request, customerRequest } = require("../../utils/request");
|
||||
|
||||
function isVisible(field, values) {
|
||||
const condition = field.visibleWhen;
|
||||
if (!condition || typeof condition !== "object") return true;
|
||||
return Object.entries(condition).every(([key, expected]) => values[key] === expected);
|
||||
}
|
||||
|
||||
const { offerings } = require("../../utils/linjing-data");
|
||||
Page({
|
||||
data: {
|
||||
loading: true,
|
||||
error: "",
|
||||
submitting: false,
|
||||
form: null,
|
||||
fields: [],
|
||||
renderFields: [],
|
||||
values: {},
|
||||
formId: "",
|
||||
},
|
||||
onLoad(options) {
|
||||
this.load(options.formId);
|
||||
},
|
||||
load(formId) {
|
||||
this.setData({ loading: true, error: "" });
|
||||
const formRequest = formId
|
||||
? request("/public/inquiry-forms/" + encodeURIComponent(formId))
|
||||
: request("/public/workspace").then((workspace) => workspace.inquiryForm);
|
||||
formRequest
|
||||
.then((form) => {
|
||||
if (!form)
|
||||
return this.setData({ loading: false, form: null, fields: [], renderFields: [] });
|
||||
const fields = Array.isArray(form.fieldsSnapshot) ? form.fieldsSnapshot : [];
|
||||
const values = Object.fromEntries(
|
||||
fields.map((field) => [field.key, field.defaultValue ?? ""]),
|
||||
);
|
||||
this.setData({
|
||||
form,
|
||||
formId: form.id,
|
||||
fields,
|
||||
renderFields: fields,
|
||||
values,
|
||||
loading: false,
|
||||
});
|
||||
})
|
||||
.catch((error) =>
|
||||
this.setData({ loading: false, error: error.message || "咨询表单加载失败" }),
|
||||
);
|
||||
},
|
||||
retry() {
|
||||
this.load();
|
||||
},
|
||||
visibleFields() {
|
||||
return this.data.fields.filter((field) => isVisible(field, this.data.values));
|
||||
},
|
||||
updateValue(key, value) {
|
||||
const values = { ...this.data.values, [key]: value };
|
||||
this.setData({
|
||||
values,
|
||||
renderFields: this.data.fields.filter((field) => isVisible(field, values)),
|
||||
});
|
||||
},
|
||||
onFieldChange(event) {
|
||||
const key = event.currentTarget.dataset.key;
|
||||
this.updateValue(key, event.detail?.value ?? event.detail);
|
||||
},
|
||||
onPickerChange(event) {
|
||||
const field = this.data.renderFields[event.currentTarget.dataset.index];
|
||||
this.updateValue(field.key, field.options[event.detail.value]);
|
||||
},
|
||||
onDateChange(event) {
|
||||
const field = this.data.renderFields[event.currentTarget.dataset.index];
|
||||
this.updateValue(field.key, event.detail.value);
|
||||
},
|
||||
validate() {
|
||||
const missing = this.visibleFields().find(
|
||||
(field) => field.required && !this.data.values[field.key],
|
||||
);
|
||||
if (missing) {
|
||||
wx.showToast({ title: "请填写" + missing.label, icon: "none" });
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
submit() {
|
||||
if (!this.data.formId || !this.validate() || this.data.submitting) return;
|
||||
const send = () =>
|
||||
customerRequest("/customer/inquiry-submissions", {
|
||||
method: "POST",
|
||||
data: { formId: this.data.formId, values: this.data.values },
|
||||
})
|
||||
.then(() => {
|
||||
this.setData({ submitting: false });
|
||||
wx.showToast({ title: "咨询已提交", icon: "success" });
|
||||
setTimeout(() => wx.switchTab({ url: "/pages/bookings/index" }), 500);
|
||||
})
|
||||
.catch((error) => {
|
||||
this.setData({ submitting: false });
|
||||
wx.showToast({ title: error.message || "提交失败", icon: "none" });
|
||||
});
|
||||
this.setData({ submitting: true });
|
||||
const token = wx.getStorageSync("customerToken");
|
||||
if (token) return send();
|
||||
wx.login({
|
||||
success: (login) =>
|
||||
request("/customer/session/wechat", { method: "POST", data: { code: login.code } })
|
||||
.then((result) => {
|
||||
wx.setStorageSync("customerToken", result.token);
|
||||
send();
|
||||
})
|
||||
.catch((error) => {
|
||||
this.setData({ submitting: false });
|
||||
wx.showToast({ title: error.message || "微信登录失败", icon: "none" });
|
||||
}),
|
||||
fail: () => {
|
||||
this.setData({ submitting: false });
|
||||
wx.showToast({ title: "微信登录失败", icon: "none" });
|
||||
},
|
||||
});
|
||||
},
|
||||
data: { offering: null, submitting: false, values: { name: "", phone: "", city: "", doorType: "", note: "" }, fields: [{ key: "name", label: "联系人", placeholder: "请输入称呼" }, { key: "phone", label: "手机号码", placeholder: "请输入手机号" }, { key: "city", label: "项目所在城市", placeholder: "例如:上海 · 佘山" }, { key: "doorType", label: "希望定制的门类型", placeholder: "入户门 / 庭院门 / 室内门" }, { key: "note", label: "补充说明", placeholder: "告诉我们你的空间和需求" }] },
|
||||
onLoad(options) { this.setData({ offering: offerings.find((item) => item.id === options.offeringId) || null }); },
|
||||
onInput(e) { this.setData({ ["values." + e.currentTarget.dataset.key]: e.detail.value }); },
|
||||
submit() { const values = this.data.values; if (!values.name || !values.phone) return wx.showToast({ title: "请填写联系人和手机号", icon: "none" }); this.setData({ submitting: true }); setTimeout(() => { const id = "LJ" + Date.now().toString().slice(-8); const record = { id, offeringName: this.data.offering ? this.data.offering.name : "门系统定制咨询", type: values.doorType || "门系统定制", project: values.city || "待确认项目", status: "待主理人复核图纸", createdAt: new Date().toLocaleDateString("zh-CN"), values }; const records = wx.getStorageSync("linjingInquiryRecords") || []; wx.setStorageSync("linjingInquiryRecords", [record].concat(records)); this.setData({ submitting: false }); wx.redirectTo({ url: "/pages/inquiry-success/index?id=" + id }); }, 500); },
|
||||
});
|
||||
Reference in new issue
Block a user