121 lines
3.9 KiB
JavaScript
121 lines
3.9 KiB
JavaScript
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);
|
|
}
|
|
|
|
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" });
|
|
},
|
|
});
|
|
},
|
|
});
|