feat: 接入配置驱动的动态咨询表单
This commit is contained in:
1 parent
8872316ad3
commit
02966cf65c
9 files changed
+232
-3
No files matched your search
@@ -4,7 +4,8 @@
|
||||
"pages/services/index",
|
||||
"pages/booking/index",
|
||||
"pages/bookings/index",
|
||||
"pages/offering/detail"
|
||||
"pages/offering/detail",
|
||||
"pages/inquiry/index"
|
||||
],
|
||||
"window": {
|
||||
"navigationBarTitleText": "个人品牌",
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
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" });
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"navigationBarTitleText": "提交咨询",
|
||||
"navigationBarBackgroundColor": "#FBF7F1",
|
||||
"navigationBarTextStyle": "black",
|
||||
"usingComponents": {
|
||||
"brand-button": "/components/ui/brand-button/brand-button",
|
||||
"brand-field": "/components/ui/brand-field/brand-field",
|
||||
"brand-loading": "/components/ui/brand-loading/brand-loading",
|
||||
"brand-empty": "/components/ui/brand-empty/brand-empty",
|
||||
"brand-error": "/components/ui/brand-error/brand-error"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<view class="page"><brand-loading wx:if="{{loading}}" /><brand-error wx:elif="{{error}}" message="{{error}}" bind:retry="retry" /><brand-empty wx:elif="{{!form}}" description="暂未开放咨询表单" /><view wx:else><view class="intro"><text class="title">{{form.name}}</text><text class="subtitle">告诉我你的想法,我会尽快与你联系</text></view><view class="form-card"><block wx:for="{{renderFields}}" wx:for-item="field" wx:for-index="index" wx:key="key"><brand-field wx:if="{{field.type === 'text' || field.type === 'textarea' || field.type === 'number' || field.type === 'url' || field.type === 'phone'}}" label="{{field.label}}" required="{{field.required}}" type="{{field.type === 'textarea' ? 'textarea' : field.type === 'number' ? 'number' : 'text'}}" placeholder="{{field.placeholder || '请输入' + field.label}}" value="{{values[field.key]}}" data-key="{{field.key}}" bind:change="onFieldChange" /><picker wx:elif="{{field.type === 'select'}}" mode="selector" range="{{field.options}}" value="0" bindchange="onPickerChange" data-index="{{index}}"><view class="picker-field"><text class="label">{{field.label}}<text wx:if="{{field.required}}" class="required"> *</text></text><text class="picker-value">{{values[field.key] || '请选择'}}</text></view></picker><picker wx:elif="{{field.type === 'date'}}" mode="date" value="{{values[field.key]}}" bindchange="onDateChange" data-index="{{index}}"><view class="picker-field"><text class="label">{{field.label}}<text wx:if="{{field.required}}" class="required"> *</text></text><text class="picker-value">{{values[field.key] || '请选择日期'}}</text></view></picker></block></view><brand-button loading="{{submitting}}" disabled="{{submitting}}" bind:tap="submit">{{submitting ? '提交中...' : '提交咨询'}}</brand-button></view></view>
|
||||
@@ -0,0 +1,50 @@
|
||||
.page {
|
||||
min-height: 100vh;
|
||||
padding: 40rpx 32rpx 64rpx;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
.intro {
|
||||
margin-bottom: 32rpx;
|
||||
}
|
||||
.title {
|
||||
display: block;
|
||||
color: var(--brand-color-text);
|
||||
font-size: 44rpx;
|
||||
font-weight: 700;
|
||||
}
|
||||
.subtitle {
|
||||
display: block;
|
||||
margin-top: 12rpx;
|
||||
color: var(--brand-color-muted);
|
||||
font-size: 28rpx;
|
||||
line-height: 1.6;
|
||||
}
|
||||
.form-card {
|
||||
margin-bottom: 32rpx;
|
||||
padding: 32rpx 28rpx 8rpx;
|
||||
background: var(--brand-color-surface);
|
||||
border-radius: var(--brand-radius-card);
|
||||
box-shadow: 0 8rpx 24rpx rgba(122, 74, 43, 0.06);
|
||||
}
|
||||
.picker-field {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 24rpx;
|
||||
padding: 24rpx 20rpx;
|
||||
border: 1rpx solid #eadfce;
|
||||
border-radius: var(--brand-radius-control);
|
||||
background: var(--brand-color-surface);
|
||||
}
|
||||
.label {
|
||||
color: var(--brand-color-text);
|
||||
font-size: 28rpx;
|
||||
font-weight: 600;
|
||||
}
|
||||
.required {
|
||||
color: var(--brand-color-primary);
|
||||
}
|
||||
.picker-value {
|
||||
color: var(--brand-color-muted);
|
||||
font-size: 28rpx;
|
||||
}
|
||||
@@ -7,6 +7,19 @@ Page({
|
||||
.catch(() => this.setData({ loading: false, error: "详情加载失败" }));
|
||||
},
|
||||
submit() {
|
||||
const cta = this.data.item?.cta || {};
|
||||
if (cta.type === "inquiry") {
|
||||
const formId = cta.config?.formId;
|
||||
return wx.navigateTo({
|
||||
url: "/pages/inquiry/index" + (formId ? "?formId=" + encodeURIComponent(formId) : ""),
|
||||
});
|
||||
}
|
||||
if (cta.type === "external-link") {
|
||||
return wx.showToast({ title: "请在支持外链的环境打开", icon: "none" });
|
||||
}
|
||||
if (cta.type === "contact") {
|
||||
return wx.showToast({ title: "请从品牌主页查看联系方式", icon: "none" });
|
||||
}
|
||||
wx.navigateTo({
|
||||
url: "/pages/booking/index?serviceId=" + encodeURIComponent(this.data.item?.id || ""),
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user