开源外卖点餐系统源码:提升餐饮行业数字化转型
**开源外卖点餐系统源码**
在现代的餐饮行业中,数字化转型已经成为一个必然趋势。外卖点餐系统作为一种重要的工具,可以帮助餐厅提高效率、减少成本,并且为顾客提供更好的服务体验。在本文中,我们将介绍一款开源的外卖点餐系统源码,旨在提升餐饮行业的数字化转型。
**系统概述**
我们的外卖点餐系统基于Web技术开发,使用Python语言和Flask框架。系统主要功能包括:
* 顾客端:提供在线点餐、支付和订单管理功能。
* 餐厅端:提供订单接收、制作和发货管理功能。
**系统设计**
### **数据库设计**
我们使用MySQL作为我们的数据库管理系统,设计了以下几张表:
#### **用户表(users)**
| 字段名 | 类型 | 描述 |
| --- | --- | --- |
| id | int | 用户ID |
| username | varchar(255) | 用户名 |
| password | varchar(255) | 密码 |
#### **餐厅表(restaurants)**
| 字段名 | 类型 | 描述 |
| --- | --- | --- |
| id | int | 餐厅ID |
| name | varchar(255) | 餐厅名称 |
| address | varchar(255) | 地址 |
#### **菜单表(menus)**
| 字段名 | 类型 | 描述 |
| --- | --- | --- |
| id | int | 菜单ID |
| restaurant_id | int | 餐厅ID |
| name | varchar(255) | 菜名 |
| price | decimal(10,2) | 价格 |
#### **订单表(orders)**
| 字段名 | 类型 | 描述 |
| --- | --- | --- |
| id | int | 订单ID |
| user_id | int | 用户ID |
| restaurant_id | int | 餐厅ID |
| status | varchar(255) | 订单状态 |
### **后端逻辑**
我们使用Flask框架开发了后端逻辑,主要功能包括:
#### **用户登录和注册**
from flask import request, jsonify@app.route('/login', methods=['POST']) def login(): username = request.json.get('username') password = request.json.get('password') user = User.query.filter_by(username=username).first() if user and user.password == password: return jsonify({'message': '登录成功'}),200 else: return jsonify({'message': '登录失败'}),401@app.route('/register', methods=['POST']) def register(): username = request.json.get('username') password = request.json.get('password') user = User.query.filter_by(username=username).first() if not user: new_user = User(username, password) db.session.add(new_user) db.session.commit() return jsonify({'message': '注册成功'}),201 else: return jsonify({'message': '用户名已存在'}),400
#### **点餐和支付**
from flask import request, jsonify@app.route('/order', methods=['POST']) def order(): user_id = request.json.get('user_id') restaurant_id = request.json.get('restaurant_id') menu_ids = request.json.get('menu_ids') order = Order.query.filter_by(user_id=user_id, restaurant_id=restaurant_id).first() if not order: new_order = Order(user_id, restaurant_id) db.session.add(new_order) db.session.commit() for menu_id in menu_ids: menu = Menu.query.get(menu_id) if menu: new_menu_item = MenuItem(order.id, menu.id) db.session.add(new_menu_item) db.session.commit() return jsonify({'message': '点餐成功'}),201 else: return jsonify({'message': '订单已存在'}),400@app.route('/pay', methods=['POST']) def pay(): order_id = request.json.get('order_id') payment_method = request.json.get('payment_method') order = Order.query.get(order_id) if order and order.status == 'pending': new_payment = Payment(order.id, payment_method) db.session.add(new_payment) db.session.commit() return jsonify({'message': '支付成功'}),200 else: return jsonify({'message': '订单状态不正确'}),400
#### **订单管理**
from flask import request, jsonify@app.route('/orders', methods=['GET']) def get_orders(): user_id = request.args.get('user_id') restaurant_id = request.args.get('restaurant_id') orders = Order.query.filter_by(user_id=user_id, restaurant_id=restaurant_id).all() return jsonify([order.to_dict() for order in orders]),200@app.route('/orders/<int:order_id>', methods=['GET']) def get_order(order_id): order = Order.query.get(order_id) if order: return jsonify(order.to_dict()),200 else: return jsonify({'message': '订单不存在'}),404
### **前端逻辑**
我们使用React框架开发了前端逻辑,主要功能包括:
#### **登录和注册**
jsximport React, { useState } from 'react'; function Login() { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const handleLogin = () => { fetch('/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }), }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error)); }; const handleRegister = () => { fetch('/register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }), }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error)); }; return ( <div> <input type="text" value={username} onChange={e => setUsername(e.target.value)} /> <input type="password" value={password} onChange={e => setPassword(e.target.value)} /> <button onClick={handleLogin}>登录</button> <button onClick={handleRegister}>注册</button> </div> ); }
#### **点餐和支付**
jsximport React, { useState } from 'react'; function Order() { const [user_id, setUser_id] = useState(''); const [restaurant_id, setRestaurant_id] = useState(''); const [menu_ids, setMenu_ids] = useState([]); const handleOrder = () => { fetch('/order', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ user_id, restaurant_id, menu_ids }), }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error)); }; const handlePay = () => { fetch('/pay', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ order_id, payment_method }), }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error)); }; return ( <div> <input type="text" value={user_id} onChange={e => setUser_id(e.target.value)} /> <input type="text" value={restaurant_id} onChange={e => setRestaurant_id(e.target.value)} /> <input type="text" value={menu_ids.join(',')} onChange={e => setMenu_ids(e.target.value.split(','))} /> <button onClick={handleOrder}>点餐</button> <button onClick={handlePay}>支付</button> </div> ); }
#### **订单管理**
jsximport React, { useState } from 'react'; function OrderList() { const [user_id, setUser_id] = useState(''); const [restaurant_id, setRestaurant_id] = useState(''); const handleGetOrders = () => { fetch(`/orders?user_id=${user_id}&restaurant_id=${restaurant_id}`, { method: 'GET', headers: { 'Content-Type': 'application/json' }, }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error)); }; const handleGetOrder = () => { fetch(`/orders/${order_id}`, { method: 'GET', headers: { 'Content-Type': 'application/json' }, }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error)); }; return ( <div> <input type="text" value={user_id} onChange={e => setUser_id(e.target.value)} /> <input type="text" value={restaurant_id} onChange={e => setRestaurant_id(e.target.value)} /> <button onClick={