去年帮朋友改造他的快餐店时,发现传统纸质菜单点餐存在几个痛点:高峰期服务员根本忙不过来、手写订单容易出错、后厨经常看错字迹潦草的订单。当时就萌生了开发微信点餐小程序的想法——毕竟现在谁吃饭不带着手机呢?
微信小程序点餐系统最直接的价值在于:
选择微信小程序原生开发而非uniapp等跨平台方案,主要考虑:
关键组件:
scroll-view实现菜品分类横向导航tabbar替换原生tabbarcanvas生成分享海报websocket实现订单状态实时更新采用云开发(TCB)方案而非自建服务器:
数据库结构示例:
javascript复制// 菜品集合
dishes: {
_id: string
name: string
price: number
category: string
sales: number // 月销量
stock: number // 库存
images: string[] // 云存储ID
}
// 订单集合
orders: {
_id: string
table: number // 桌号
items: {
dishId: string
quantity: number
remark: string
}[]
status: 'pending' | 'cooking' | 'completed'
createTime: Date
}
购物车需要解决的关键问题:
实现方案:
javascript复制// 购物车数据结构
let cart = {
items: [
{
dishId: '123',
name: '宫保鸡丁',
price: 38,
quantity: 2,
specs: { spiciness: '中辣' }
}
],
total: 76
}
// 添加菜品逻辑
function addToCart(dish, specs) {
const existing = cart.items.find(item =>
item.dishId === dish._id &&
JSON.stringify(item.specs) === JSON.stringify(specs)
);
if (existing) {
existing.quantity++;
} else {
cart.items.push({
dishId: dish._id,
name: dish.name,
price: dish.price,
quantity: 1,
specs
});
}
updateTotal();
}
订单状态机设计要点:
状态流转图:
code复制待支付 → 已取消(超时未支付)
待支付 → 待制作(支付成功)
待制作 → 制作中(商家接单)
制作中 → 已完成(出餐)
制作中 → 已取消(商家拒单)
实测发现:菜品图片占首屏请求量的80%,采取以下措施:
<image>的lazy-load属性优化后首屏加载时间从2.1s降至0.8s。
高频查询场景:
建立复合索引:
javascript复制db.collection('dishes').createIndex({
category: 1,
sales: -1
})
查询示例:
javascript复制db.collection('dishes')
.where({ category: '热菜' })
.orderBy('sales', 'desc')
.limit(20)
.get()
核心指标:
使用云数据库的聚合能力:
javascript复制const res = await db.collection('orders')
.aggregate()
.match({
createTime: _.gt(todayStart)
})
.group({
_id: null,
totalOrders: $.sum(1),
totalAmount: $.sum('$amount')
})
.end()
对比了三种方案后选择云打印:
打印内容模板示例:
code复制[订单号] #20231125001
[时间] 2023-11-25 12:30
----------------------
1. 宫保鸡丁 x1 中辣
2. 米饭 x2
----------------------
备注:不要葱花
问题现象:突然无法发起支付,错误码"CA_ERROR"
排查过程:
解决方案:
当菜品超过100项时,快速滚动会出现明显卡顿。通过以下方法优化:
<recycle-view>替代普通<view><image>的lazy-loadworker线程优化后FPS从15提升到55+。
设计要点:
javascript复制// 积分变更记录
points: {
userId: string,
value: number, // 正为增加,负为消耗
reason: '消费' | '兑换',
balance: number, // 当前余额
expireAt: Date
}
基于用户历史订单推荐相似菜品:
实现代码:
javascript复制function recommendDishes(userId) {
const history = await getOrderHistory(userId);
const favoriteCategory = getMostFrequentCategory(history);
return db.collection('dishes')
.where({
category: favoriteCategory,
price: _.lte(history.avgPrice * 1.2)
})
.orderBy('sales', 'desc')
.limit(5)
.get();
}
上线3个月后的关键指标:
最受欢迎的五个功能:
近期正在测试的新功能:
技术储备:
wx.chooseMedia实现拍照上传关键提示:小程序提交审核时,支付功能需要提供《增值电信业务经营许可证》,个体户可以用《个体工商户营业执照》+《电子商务登记声明》替代