feat: 接入配置驱动的动态咨询表单
This commit is contained in:
1 parent
8872316ad3
commit
02966cf65c
9 files changed
+232
-3
No files matched your search
@@ -134,7 +134,7 @@ export class PublicWorkspaceController {
|
|||||||
@Get()
|
@Get()
|
||||||
async get(): Promise<unknown> {
|
async get(): Promise<unknown> {
|
||||||
const brandId = process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001";
|
const brandId = process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001";
|
||||||
const [brand, modules, fields, offerings] = await Promise.all([
|
const [brand, modules, fields, offerings, inquiryForm] = await Promise.all([
|
||||||
this.prisma.brandProfile.findUnique({ where: { id: brandId } }),
|
this.prisma.brandProfile.findUnique({ where: { id: brandId } }),
|
||||||
this.prisma.brandModule.findMany({
|
this.prisma.brandModule.findMany({
|
||||||
where: { brandId, isEnabled: true },
|
where: { brandId, isEnabled: true },
|
||||||
@@ -148,8 +148,12 @@ export class PublicWorkspaceController {
|
|||||||
where: { brandId, isEnabled: true },
|
where: { brandId, isEnabled: true },
|
||||||
orderBy: { sortOrder: "asc" },
|
orderBy: { sortOrder: "asc" },
|
||||||
}),
|
}),
|
||||||
|
this.prisma.inquiryForm.findFirst({
|
||||||
|
where: { brandId, isEnabled: true },
|
||||||
|
orderBy: { updatedAt: "desc" },
|
||||||
|
}),
|
||||||
]);
|
]);
|
||||||
return { brand, modules, fields, offerings };
|
return { brand, modules, fields, offerings, inquiryForm };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,6 +47,8 @@ test("health and public content are available without authentication", async ()
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("admin can install a template and public workspace exposes enabled configuration", async () => {
|
test("admin can install a template and public workspace exposes enabled configuration", async () => {
|
||||||
|
await prisma.inquirySubmission.deleteMany({ where: { brandId } });
|
||||||
|
await prisma.inquiryForm.deleteMany({ where: { brandId } });
|
||||||
await prisma.brandFieldDefinition.deleteMany({ where: { brandId } });
|
await prisma.brandFieldDefinition.deleteMany({ where: { brandId } });
|
||||||
await prisma.brandModule.deleteMany({ where: { brandId } });
|
await prisma.brandModule.deleteMany({ where: { brandId } });
|
||||||
await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } });
|
await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } });
|
||||||
@@ -74,6 +76,7 @@ test("admin can install a template and public workspace exposes enabled configur
|
|||||||
const publicWorkspace = await request("/public/workspace");
|
const publicWorkspace = await request("/public/workspace");
|
||||||
assert.equal(publicWorkspace.statusCode, 200);
|
assert.equal(publicWorkspace.statusCode, 200);
|
||||||
assert.equal(JSON.parse(publicWorkspace.body).modules.length, 4);
|
assert.equal(JSON.parse(publicWorkspace.body).modules.length, 4);
|
||||||
|
assert.equal(JSON.parse(publicWorkspace.body).inquiryForm, null);
|
||||||
const conflict = await request("/admin/workspace/install", {
|
const conflict = await request("/admin/workspace/install", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { authorization: `Bearer ${token}` },
|
headers: { authorization: `Bearer ${token}` },
|
||||||
@@ -176,6 +179,9 @@ test("admin can create and version an inquiry form", async () => {
|
|||||||
assert.equal(JSON.parse(updated.body).version, 2);
|
assert.equal(JSON.parse(updated.body).version, 2);
|
||||||
const publicForm = await request(`/public/inquiry-forms/${id}`);
|
const publicForm = await request(`/public/inquiry-forms/${id}`);
|
||||||
assert.equal(publicForm.statusCode, 200);
|
assert.equal(publicForm.statusCode, 200);
|
||||||
|
const publicWorkspace = await request("/public/workspace");
|
||||||
|
assert.equal(publicWorkspace.statusCode, 200);
|
||||||
|
assert.equal(JSON.parse(publicWorkspace.body).inquiryForm.id, id);
|
||||||
const submission = await request("/customer/inquiry-submissions", {
|
const submission = await request("/customer/inquiry-submissions", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { authorization: `Bearer ${customerToken}` },
|
headers: { authorization: `Bearer ${customerToken}` },
|
||||||
|
|||||||
@@ -4,7 +4,8 @@
|
|||||||
"pages/services/index",
|
"pages/services/index",
|
||||||
"pages/booking/index",
|
"pages/booking/index",
|
||||||
"pages/bookings/index",
|
"pages/bookings/index",
|
||||||
"pages/offering/detail"
|
"pages/offering/detail",
|
||||||
|
"pages/inquiry/index"
|
||||||
],
|
],
|
||||||
"window": {
|
"window": {
|
||||||
"navigationBarTitleText": "个人品牌",
|
"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: "详情加载失败" }));
|
.catch(() => this.setData({ loading: false, error: "详情加载失败" }));
|
||||||
},
|
},
|
||||||
submit() {
|
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({
|
wx.navigateTo({
|
||||||
url: "/pages/booking/index?serviceId=" + encodeURIComponent(this.data.item?.id || ""),
|
url: "/pages/booking/index?serviceId=" + encodeURIComponent(this.data.item?.id || ""),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -152,6 +152,13 @@ export interface InquiryForm {
|
|||||||
isEnabled: boolean;
|
isEnabled: boolean;
|
||||||
fieldsSnapshot: unknown[];
|
fieldsSnapshot: unknown[];
|
||||||
}
|
}
|
||||||
|
export interface PublicWorkspace {
|
||||||
|
brand: BrandProfile | null;
|
||||||
|
modules: BrandModule[];
|
||||||
|
fields: BrandFieldDefinition[];
|
||||||
|
offerings: Offering[];
|
||||||
|
inquiryForm: InquiryForm | null;
|
||||||
|
}
|
||||||
export interface Offering {
|
export interface Offering {
|
||||||
id: string;
|
id: string;
|
||||||
brandId?: string;
|
brandId?: string;
|
||||||
@@ -220,6 +227,21 @@ export class ApiClient {
|
|||||||
listServices(): Promise<PublicService[]> {
|
listServices(): Promise<PublicService[]> {
|
||||||
return this.request("/public/services");
|
return this.request("/public/services");
|
||||||
}
|
}
|
||||||
|
getPublicWorkspace(): Promise<PublicWorkspace> {
|
||||||
|
return this.request("/public/workspace");
|
||||||
|
}
|
||||||
|
getPublicInquiryForm(id: string): Promise<InquiryForm> {
|
||||||
|
return this.request(`/public/inquiry-forms/${encodeURIComponent(id)}`);
|
||||||
|
}
|
||||||
|
createCustomerInquirySubmission(
|
||||||
|
formId: string,
|
||||||
|
values: Record<string, unknown>,
|
||||||
|
): Promise<unknown> {
|
||||||
|
return this.request("/customer/inquiry-submissions", {
|
||||||
|
method: "POST",
|
||||||
|
body: { formId, values },
|
||||||
|
});
|
||||||
|
}
|
||||||
createCustomerSession(code: string): Promise<{ token: string }> {
|
createCustomerSession(code: string): Promise<{ token: string }> {
|
||||||
return this.request("/customer/session/wechat", { method: "POST", body: { code } });
|
return this.request("/customer/session/wechat", { method: "POST", body: { code } });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user