微信小程序云开发实战:扫码点餐系统的高并发订单处理与外卖接单架构

承恒信息科技在服务泉州餐饮连锁品牌进行小程序开发时发现,午晚高峰期间扫码点餐系统的并发量可达平峰期的10倍以上,订单处理延迟和外卖接单遗漏是两大核心技术痛点。本文将分享基于微信小程序云开发构建高可用餐饮点餐系统的实战经验,涵盖云函数订单处理架构、实时数据推送和外卖平台聚合对接方案。

一、系统架构与云开发技术选型

系统采用微信小程序云开发(CloudBase)作为后端基础设施,前端使用小程序原生框架开发。云开发提供云函数、云数据库、云存储和云调用四大能力,免去了服务器运维成本。核心模块包括:扫码点餐、排队叫号、外卖接单聚合、订单管理后台。高峰期单门店QPS可达300+,需要云函数弹性扩容支持。

云数据库采用MongoDB文档型存储,适合菜品、订单等非结构化数据。外卖接单聚合层通过云函数定时触发器轮询美团、饿了么开放API,将订单统一归集到内部系统。AI智能推荐基于用户历史点餐数据,通过协同过滤算法推荐菜品。

二、高并发订单处理云函数

订单创建是点餐系统的核心链路,需要处理并发下单、库存扣减、排队等位的复杂逻辑。以下是订单创建云函数的实现,使用云数据库事务保障数据一致性:

// cloudfunctions/createOrder/index.js

const cloud = require('wx-server-sdk');

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });

const db = cloud.database();

const _ = db.command;

exports.main = async (event, context) => {

const { storeId, tableNo, items, userId, orderType } = event;

const orderId = 'ORD' + Date.now() + Math.floor(Math.random() * 1000);

// 开启数据库事务

const transaction = await db.startTransaction();

try {

// 1. 检查桌台状态(防止重复下单)

const tableRes = await transaction.collection('tables')

.doc(`{storeId}_{tableNo}`)

.get();

const table = tableRes.data;

if (table.status === 'occupied' && table.currentOrderId) {

return { code: 4001, msg: '该桌已有进行中订单,请勿重复下单' };

}

// 2. 批量检查菜品库存并扣减

let totalAmount = 0;

for (const item of items) {

const dishRes = await transaction.collection('dishes')

.doc(item.dishId)

.get();

const dish = dishRes.data;

if (dish.dailyStock !== -1 && dish.dailyStock < item.quantity) {

throw new Error(`菜品库存不足: ${dish.name}`);

}

// 扣减日库存(-1表示无限量)

if (dish.dailyStock !== -1) {

await transaction.collection('dishes')

.doc(item.dishId)

.update({ data: { dailyStock: _.inc(-item.quantity) } });

}

totalAmount += dish.price * item.quantity;

}

// 3. 创建订单记录

const orderData = {

_id: orderId,

storeId,

tableNo,

userId,

orderType, // 'dine_in' | 'takeout'

items: items.map(i => ({

dishId: i.dishId,

name: i.name,

price: i.price,

quantity: i.quantity,

subtotal: i.price * i.quantity

})),

totalAmount,

status: 'pending', // pending -> confirmed -> cooking -> served

createdAt: db.serverDate(),

expireAt: db.serverDate({ offset: 900000 }) // 15分钟未支付自动取消

};

await transaction.collection('orders').add({ data: orderData });

// 4. 更新桌台状态

await transaction.collection('tables')

.doc(`{storeId}_{tableNo}`)

.update({ data: {

status: 'occupied',

currentOrderId: orderId

}});

// 5. 提交事务

await transaction.commit();

// 6. 推送新订单通知到厨房端(WebSocket)

await cloud.callFunction({

name: 'notifyKitchen',

data: { storeId, orderId, tableNo, items }

});

return { code: 0, msg: '下单成功', data: { orderId, totalAmount } };

} catch (error) {

await transaction.rollback();

console.error('订单创建失败', error);

return { code: 5000, msg: error.message || '下单失败,请重试' };

}

};

该云函数使用云数据库事务(startTransaction)保障库存扣减和订单创建的原子性。事务隔离级别为快照隔离,有效防止超卖。订单设置15分钟过期时间,配合定时触发器自动清理超时未支付订单。在压测中,单云函数实例可处理QPS 50,云开发自动扩容至10实例后可承载QPS 500。

三、排队叫号与实时推送

排队叫号需要实时将叫号信息推送到用户小程序,传统轮询方式延迟高且浪费资源。方案采用云开发的实时数据推送(watch)能力,客户端监听队列变化,服务端更新数据后自动推送到所有在线客户端。

// cloudfunctions/callNumber/index.js --- 叫号云函数

const cloud = require('wx-server-sdk');

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });

const db = cloud.database();

const _ = db.command;

exports.main = async (event, context) => {

const { storeId, queueId } = event;

// 获取当前排队队列

const queueRes = await db.collection('queue')

.where({ storeId, status: 'waiting' })

.orderBy('queueNumber', 'asc')

.limit(1)

.get();

if (queueRes.data.length === 0) {

return { code: 4001, msg: '没有等待的顾客' };

}

const nextCustomer = queueRes.data0;

// 更新排队状态为已叫号

await db.collection('queue')

.doc(nextCustomer._id)

.update({ data: {

status: 'called',

calledAt: db.serverDate(),

expireAt: db.serverDate({ offset: 600000 }) // 10分钟未到店自动过期

}});

// 写入叫号通知记录(客户端通过watch实时监听)

await db.collection('notifications').add({ data: {

storeId,

type: 'call_number',

queueNumber: nextCustomer.queueNumber,

phone: nextCustomer.phone,

tableType: nextCustomer.tableType,

message: `请${nextCustomer.queueNumber}号顾客到店入座`,

createdAt: db.serverDate(),

read: false

}});

// 发送订阅消息通知用户

await cloud.openapi.subscribeMessage.send({

touser: nextCustomer.openid,

templateId: 'call_number_template_id',

page: `pages/queue/detail?storeId=${storeId}`,

data: {

thing1: { value: `${nextCustomer.queueNumber}号` },

thing2: { value: nextCustomer.tableType },

time4: { value: new Date().toLocaleTimeString('zh-CN') }

}

});

return { code: 0, data: { queueNumber: nextCustomer.queueNumber } };

};

// 小程序端实时监听叫号(客户端代码)

// pages/queue/waiting.js

Page({

data: { currentNumber: 0, myNumber: 0 },

onLoad() {

const db = wx.cloud.database();

// watch 实时监听叫号通知

this.watcher = db.collection('notifications')

.where({ storeId: this.storeId, type: 'call_number' })

.orderBy('createdAt', 'desc')

.limit(1)

.watch({

onChange: (snapshot) => {

if (snapshot.docs.length > 0) {

const latest = snapshot.docs0;

this.setData({ currentNumber: latest.queueNumber });

if (latest.queueNumber === this.data.myNumber) {

wx.vibrateLong(); // 震动提醒

wx.showToast({ title: '到您了!', icon: 'success' });

}

}

},

onError: (err) => console.error('监听失败', err)

});

},

onUnload() {

if (this.watcher) this.watcher.close();

}

});

实时推送方案利用云数据库的watch能力,客户端建立长连接监听数据变更,服务端写入通知记录后自动推送。推送延迟控制在200ms以内,比轮询方案节省90%以上的网络请求。配合微信订阅消息,即使小程序在后台也能收到叫号通知。

四、外卖平台聚合接单与部署

餐饮门店通常同时接入美团、饿了么等多个外卖平台,各自有独立的接单后台,管理成本高。聚合接单方案通过云函数定时轮询各平台开放API,将订单统一拉取到内部系统,实现一个后台管理所有外卖订单。

// cloudfunctions/pollTakeoutOrders/index.js --- 定时触发轮询外卖订单

const cloud = require('wx-server-sdk');

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });

const db = cloud.database();

// 定时触发器配置(每30秒执行一次)

// config.json: { "triggers": { "type": "timer", "config": "0/30 \* \* \* \* \* \*" } }

exports.main = async (event, context) => {

const stores = await db.collection('stores')

.where({ takeoutEnabled: true })

.get();

for (const store of stores.data) {

// 并行拉取美团和饿了么订单

const meituanOrders, elemeOrders = await Promise.allSettled([

fetchMeituanOrders(store),

fetchElemeOrders(store)

]);

const allOrders = \[\];

if (meituanOrders.status === 'fulfilled') allOrders.push(...meituanOrders.value);

if (elemeOrders.status === 'fulfilled') allOrders.push(...elemeOrders.value);

// 去重并写入内部订单系统

for (const order of allOrders) {

const exists = await db.collection('takeout_orders')

.where({ platformOrderId: order.platformOrderId })

.count();

if (exists.total === 0) {

await db.collection('takeout_orders').add({ data: {

...order,

storeId: store._id,

internalStatus: 'new',

receivedAt: db.serverDate(),

autoAccept: store.autoAcceptEnabled || false

}});

// 推送新外卖订单通知到门店

await cloud.callFunction({

name: 'notifyStore',

data: { storeId: store._id, order, type: 'new_takeout' }

});

}

}

}

return { code: 0, msg: '轮询完成' };

};

async function fetchMeituanOrders(store) {

const token = await getMeituanToken(store.meituanAppId, store.meituanAppSecret);

const res = await cloud.callFunction({

name: 'httpRequest',

data: {

url: 'https://open.waimai.meituan.com/api/order/new',

method: 'GET',

headers: { Authorization: `Bearer ${token}` }

}

});

return (res.result.data || \[\]).map(o => ({

platform: 'meituan',

platformOrderId: o.orderId,

customerName: o.userName,

customerPhone: o.phone,

address: o.address,

items: o.detail,

totalAmount: o.total,

deliveryFee: o.shippingFee,

remark: o.remark,

orderTime: o.utime

}));

}

外卖聚合方案通过定时触发器每30秒轮询各平台API,使用Promise.allSettled并行请求提升效率。订单去重通过platformOrderId唯一标识保证幂等性。系统支持自动接单模式,高峰期减少人工操作。生产环境运行数据:单门店日均处理外卖订单200+,订单从平台到门店通知的平均延迟<35秒,接单遗漏率降至0。整体云开发方案月度成本约200。


相关推荐
阿标在干嘛2 小时前
从HTTP到WebSocket:招投标信息实时推送系统的协议选型与架构设计
大数据·微服务·架构
CCPC不拿奖不改名12 小时前
大模型推理架构与开源生态知识整理
数据库·windows·python·架构·langchain·开源·github
heimeiyingwang16 小时前
【架构实战】可观测性三支柱:日志、指标、链路的融合
elasticsearch·架构·kubernetes
万点科技码农17 小时前
MCP+A2A协议与AI原生架构:软件定制开发新标准下的西安服务商实力解析
架构·ai-native
dogstarhuang18 小时前
从 0 到 1 搭建可收费的 API 开放平台(实战)
java·架构·api
GIoT801018 小时前
自动化请求的智能重试策略:指数退避 + 熔断 + IP 轮换
架构
葬送的代码人生18 小时前
从 Vue 到 React:Tailwind CSS 布局 + BFF 代理实战
前端·react.js·架构
hunterandroid20 小时前
[鸿蒙从零到一] ArkUI 组件化实战:构建可复用、可组合的自定义组件
前端·华为·架构
这是个栗子20 小时前
uni-app 微信小程序开发:常用函数总结(一)
微信小程序·小程序·uni-app·getcurrentpages