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); },
|
||||
});
|
||||
@@ -1 +1 @@
|
||||
<view class="page"><brand-loading wx:if="{{loading}}" /><view wx:elif="{{error}}" class="inline-error"><text>{{error}}</text><button size="mini" bindtap="retry">重新加载</button></view><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>
|
||||
<view class="page"><view class="head"><text class="eyebrow">PRIVATE CONSULTATION</text><text class="title">私享测量预约</text><text class="sub">{{offering ? offering.name : '主理人亲自沟通,72 小时内反馈空间构想'}}</text></view><view class="form"><view wx:for="{{fields}}" wx:key="key" class="field"><text>{{item.label}}</text><input data-key="{{item.key}}" placeholder="{{item.placeholder}}" value="{{values[item.key]}}" bindinput="onInput"/></view><button class="submit" loading="{{submitting}}" bindtap="submit">提交定制需求</button><text class="privacy">信息仅用于本次方案沟通,我们不会向第三方披露。</text></view></view>
|
||||
@@ -1,62 +1 @@
|
||||
.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;
|
||||
}
|
||||
.inline-error {
|
||||
padding: 72rpx 32rpx;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
color: var(--brand-color-muted);
|
||||
}
|
||||
.inline-error button {
|
||||
margin-top: 24rpx;
|
||||
color: var(--brand-color-primary);
|
||||
background: var(--brand-color-subtle);
|
||||
}
|
||||
.page{min-height:100vh;background:#f7f5f1;color:#252525}.head{background:#252827;color:#fff;padding:68rpx 40rpx 58rpx}.eyebrow{display:block;color:#d9c8b2;font-size:20rpx;letter-spacing:3rpx;margin-bottom:18rpx}.title{display:block;font-family:serif;font-size:50rpx}.sub{display:block;color:#c6c2ba;font-size:25rpx;line-height:1.6;margin-top:18rpx}.form{padding:40rpx}.field{margin-bottom:30rpx}.field text{display:block;font-size:24rpx;color:#77736b;margin-bottom:12rpx}.field input{box-sizing:border-box;width:100%;height:84rpx;padding:0 22rpx;background:#fff;border-bottom:1px solid #d7d0c6;font-size:27rpx}.submit{margin-top:22rpx;width:100%;height:88rpx;line-height:88rpx;border-radius:0;background:#8a6846;color:#fff;font-size:28rpx}.privacy{display:block;text-align:center;color:#aaa39a;font-size:20rpx;margin-top:26rpx}
|
||||
Reference in new issue
Block a user