refactor: 重构模板叠加式编译产物

This commit is contained in:
que01 committed 2026-09-26 11:21:29 +08:00
1 parent c46297b13b
commit 25d1fe983c
93 files changed
+870 -2467

No files matched your search

+9
View File
@@ -0,0 +1,9 @@
# booking 能力包
为需要"选择数量 + 自取/配送"下单流程的模板提供小程序页面。模板在 `template.json` 的
`requiredCapabilities` 中声明 `booking` 后,`template:dev` 会把 `miniapp/` 叠加进产物。
- `miniapp/pages/booking`:下单页,提交到 `POST /customer/bookings`。
- `miniapp/utils/booking-templates.js`:下单页文案。
当前后端仍使用核心 `Booking` 模型;把它迁移为独立 capability 是后续任务。
@@ -0,0 +1,7 @@
{
"key": "booking",
"version": "1.0.0",
"runtimeContract": "1.0",
"description": "带数量、自取/配送的下单能力,提交到 /customer/bookings。",
"pages": ["pages/booking/index"]
}
@@ -0,0 +1,92 @@
const { request, customerRequest, customerTokenKey } = require("../../utils/request");
const { getTemplate } = require("../../utils/booking-templates");
Page({
data: {
service: {},
form: {},
offeringId: "",
quantity: 1,
fulfillment: "pickup",
submitting: false,
template: getTemplate("personal"),
},
onLoad(query) {
Promise.all([request("/public/offerings"), request("/public/brand")])
.then(([services, brand]) => {
const list = Array.isArray(services) ? services : [];
const service =
list.find((item) => item.id === query.offeringId || item.id === query.serviceId) ||
list.find((item) => item.priceAmount !== null) ||
{};
this.setData({
service,
offeringId: service.id || "",
template: getTemplate(brand.templateKey),
});
})
.catch(() => wx.showToast({ title: "服务加载失败", icon: "none" }));
},
input(event) {
this.setData({ ["form." + event.currentTarget.dataset.key]: event.detail.value });
},
chooseFulfillment(event) {
this.setData({ fulfillment: event.currentTarget.dataset.value });
},
increase() {
if (this.data.quantity < 50) this.setData({ quantity: this.data.quantity + 1 });
},
decrease() {
if (this.data.quantity > 1) this.setData({ quantity: this.data.quantity - 1 });
},
submit() {
const form = this.data.form;
if (!this.data.offeringId) return wx.showToast({ title: "请先选择服务", icon: "none" });
if (!form.customerName || !form.wechatId)
return wx.showToast({ title: "请填写称呼和联系方式", icon: "none" });
if (this.data.fulfillment === "delivery" && !form.deliveryAddress)
return wx.showToast({ title: "请填写" + this.data.template.locationLabel, icon: "none" });
this.setData({ submitting: true });
const send = () =>
customerRequest("/customer/bookings", {
method: "POST",
data: {
requestType: this.data.template.requestType,
customerName: form.customerName,
wechatId: form.wechatId,
contactPreference: "wechat",
fulfillmentType: this.data.fulfillment,
deliveryAddress: form.deliveryAddress,
requestedTime: form.requestedTime,
note: form.note,
items: [{ offeringId: this.data.offeringId, quantity: this.data.quantity }],
},
})
.then(() => {
this.setData({ submitting: false });
wx.showToast({ title: "预约已提交" });
setTimeout(() => wx.navigateTo({ url: "/pages/bookings/index" }), 500);
})
.catch((error) => {
this.setData({ submitting: false });
wx.showToast({ title: error.message || "提交失败", icon: "none" });
});
const token = wx.getStorageSync(customerTokenKey());
if (token) return send();
wx.login({
success: (login) =>
request("/customer/session/wechat", { method: "POST", data: { code: login.code } })
.then((result) => {
wx.setStorageSync(customerTokenKey(), result.token);
send();
})
.catch((error) => {
this.setData({ submitting: false });
wx.showToast({ title: error.message || "微信登录失败", icon: "none" });
}),
fail: (error) => {
this.setData({ submitting: false });
wx.showToast({ title: error.errMsg || "微信登录失败", icon: "none" });
},
});
},
});
@@ -0,0 +1,10 @@
{
"navigationBarTitleText": "提交需求",
"navigationBarBackgroundColor": "#FBF7F1",
"navigationBarTextStyle": "black",
"usingComponents": {
"brand-button": "/components/ui/brand-button/brand-button",
"brand-field": "/components/ui/brand-field/brand-field",
"brand-card": "/components/ui/brand-card/brand-card"
}
}
@@ -0,0 +1,7 @@
<view class="page">
<view class="product"><view class="product-image"><image wx:if="{{service.coverUrl}}" src="{{service.coverUrl}}" mode="aspectFill"/><view wx:else class="dish-placeholder"></view></view><view class="product-copy"><text class="name">{{service.name || '选择服务'}}</text><text class="price">{{service.priceText || '沟通报价'}}</text></view><text class="available">{{template.available}}</text></view>
<view class="quantity"><text>数量</text><view class="stepper"><button bindtap="decrease">−</button><text>{{quantity}} 份</text><button bindtap="increase">+</button></view></view>
<view class="fulfillment"><text class="field-title">{{template.modeLabel}}</text><view class="choices"><button class="choice {{fulfillment === 'pickup' ? 'active' : ''}}" data-value="pickup" bindtap="chooseFulfillment">{{template.modeA}}</button><button class="choice {{fulfillment === 'delivery' ? 'active' : ''}}" data-value="delivery" bindtap="chooseFulfillment">{{template.modeB}}</button></view></view>
<brand-card><view class="form"><brand-field label="怎么称呼" placeholder="请输入你的称呼" required value="{{form.customerName}}" bind:change="input" data-key="customerName"/><brand-field label="联系方式" placeholder="手机号或微信号" required value="{{form.wechatId}}" bind:change="input" data-key="wechatId"/><brand-field label="期望时间" placeholder="例如:下周三 14:00" value="{{form.requestedTime}}" bind:change="input" data-key="requestedTime"/><brand-field wx:if="{{fulfillment === 'delivery'}}" label="{{template.locationLabel}}" placeholder="{{template.locationPlaceholder}}" value="{{form.deliveryAddress}}" bind:change="input" data-key="deliveryAddress"/><brand-field label="{{template.requestType === 'package' ? '备注' : '需求说明'}}" placeholder="{{template.notePlaceholder}}" type="textarea" value="{{form.note}}" bind:change="input" data-key="note"/></view></brand-card>
<view class="notice">● {{template.submitNotice}}</view><brand-button loading="{{submitting}}" disabled="{{submitting}}" bind:tap="submit">{{submitting ? '提交中...' : template.submitLabel}}</brand-button>
</view>
@@ -0,0 +1,138 @@
.page {
min-height: 100vh;
padding: 24rpx 32rpx 60rpx;
background: #fbf7f1;
color: #2b2b2b;
}
.product {
display: flex;
align-items: center;
gap: 20rpx;
padding: 20rpx;
background: #f4ebd9;
border-radius: 16rpx;
}
.product-image {
width: 220rpx;
height: 180rpx;
overflow: hidden;
border-radius: 12rpx;
flex: none;
}
.product-image image,
.dish-placeholder {
width: 100%;
height: 100%;
display: block;
background: #c79555;
}
.product-copy {
flex: 1;
}
.name {
display: block;
font-size: 34rpx;
font-weight: 700;
}
.price {
display: block;
margin-top: 18rpx;
color: #b54a2c;
font-size: 38rpx;
}
.available {
color: #7a4a2b;
font-size: 26rpx;
white-space: nowrap;
align-self: flex-start;
margin-top: 14rpx;
}
.quantity,
.fulfillment {
margin-top: 20rpx;
padding: 28rpx;
background: #f4ebd9;
border-radius: 16rpx;
font-size: 32rpx;
}
.stepper {
float: right;
display: flex;
align-items: center;
gap: 18rpx;
}
.stepper button {
width: 56rpx;
height: 56rpx;
line-height: 52rpx;
padding: 0;
border-radius: 50%;
background: #fff;
color: #b54a2c;
}
.field-title {
font-weight: 700;
}
.choices {
display: flex;
gap: 16rpx;
margin-top: 18rpx;
}
.choice {
flex: 1;
border-radius: 12rpx;
background: #7a4a2b;
color: #fff;
}
.choice.active {
background: #b54a2c;
}
.form {
margin-top: 20rpx;
background: #fff;
border-radius: 16rpx;
padding: 8rpx 28rpx;
}
.form label {
display: flex;
justify-content: space-between;
gap: 20rpx;
padding: 28rpx 0;
border-bottom: 1rpx solid #eee;
font-size: 30rpx;
}
.form input,
.form textarea {
flex: 1;
text-align: right;
}
.form textarea {
min-height: 100rpx;
}
.notice {
margin: 20rpx 0;
padding: 24rpx;
background: #f4ebd9;
border-radius: 16rpx;
color: #7a4a2b;
}
.submit {
background: #b54a2c;
color: #fff;
border-radius: 12rpx;
font-size: 34rpx;
}
.stepper button {
display: flex;
width: 64rpx;
height: 64rpx;
align-items: center;
justify-content: center;
padding: 0;
border: 0;
border-radius: 16rpx;
background: #fff;
color: #b54a2c;
font-size: 40rpx;
line-height: 1;
}
@@ -0,0 +1,78 @@
const TEMPLATES = {
food: {
modeLabels: { pickup: "自取", delivery: "配送" },
contactLabel: "联系摊主",
cancelLabel: "取消预约",
cancelNote: "取消后请及时告知摊主,避免食材浪费。",
hero: "今天吃什么",
heroSub: "现做家常饭",
about: "品牌简介",
description: "个人经营的家常小灶,套餐现做,也可预约小型聚餐和私厨定制。",
primary: "预约套餐",
secondary: "咨询私厨",
timeLabel: "营业时间",
time: "11:00—20:00",
addressLabel: "摊位地址",
address: "校园路社区便民点 12 号",
noteLabel: "配送说明",
note: "满 30 元周边配送,也可自取",
serviceSub: "今日可订",
first: "普通套餐",
firstSub: "日常家常饭",
second: "私厨服务",
secondSub: "聚餐定制",
available: "今日可订",
unavailable: "暂不可订",
empty: "暂无可订服务",
bookingTitle: "预约套餐",
requestType: "package",
modeLabel: "取餐方式",
modeA: "自取",
modeB: "配送",
locationLabel: "配送地址",
locationPlaceholder: "请输入配送地址",
notePlaceholder: "口味、餐具或其他说明",
submitNotice: "提交后由摊主人工确认,确认后再安排制作。",
submitLabel: "提交预约",
},
personal: {
modeLabels: { pickup: "线上沟通", delivery: "线下沟通" },
contactLabel: "联系品牌方",
cancelLabel: "取消申请",
cancelNote: "如需调整,请尽早联系品牌方。",
hero: "个人品牌",
heroSub: "把专业能力,变成值得信赖的体验",
about: "关于我",
description: "这里展示你的专业背景、工作方式与品牌理念。",
primary: "查看服务",
secondary: "发起咨询",
timeLabel: "服务时间",
time: "工作日 10:00—18:00",
addressLabel: "所在位置",
address: "线上 / 线下均可沟通",
noteLabel: "合作说明",
note: "提交需求后,我会尽快与你联系",
serviceSub: "可咨询",
first: "标准服务",
firstSub: "清晰的服务方案",
second: "定制合作",
secondSub: "按需求沟通",
available: "可咨询",
unavailable: "暂不可用",
empty: "暂无可咨询服务",
bookingTitle: "合作咨询",
requestType: "inquiry",
modeLabel: "沟通方式",
modeA: "线上沟通",
modeB: "线下沟通",
locationLabel: "沟通方式或地点",
locationPlaceholder: "请输入会议方式、地点或链接",
notePlaceholder: "请介绍你的需求、预算或期待结果",
submitNotice: "提交后品牌方会与你确认需求、时间和合作方式。",
submitLabel: "提交需求",
},
};
function getTemplate(key) {
return TEMPLATES[key] || TEMPLATES.personal;
}
module.exports = { getTemplate, TEMPLATES };
+6
View File
@@ -0,0 +1,6 @@
{
"name": "@personal-brand/capability-booking",
"version": "1.0.0",
"private": true,
"description": "下单能力的小程序页面,由声明 booking 能力的模板在编译时引入。"
}