Skip to content

Commit

Permalink
创建一个在线商店,允许用户浏览和购买产品。
Browse files Browse the repository at this point in the history
实现购物车功能,允许用户添加和管理购物车中的商品。
集成支付处理,以接受信用卡、PayPal、USDT、微信、等付款方式。
提供用户账户管理,包括订单历史和地址管理。
剩下的你可以给我一些建议
  • Loading branch information
charging-kuafuai committed Jun 12, 2024
1 parent b146038 commit bbf833c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 65 deletions.
11 changes: 1 addition & 10 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
from app import db

class Demo(db.Model):
id = db.Column(db.Integer, primary_key=True)
info1 = db.Column(db.String(80), unique=True, nullable=False)
info2 = db.Column(db.String(120), unique=True, nullable=False)

def __init__(self, info1, info2):
self.info1 = info1
self.info2 = info2
`app/models.py`文件中创建以下类和方法
56 changes: 1 addition & 55 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -1,55 +1 @@
from flask import request, jsonify
from app import app, db
from app.models import Demo

# 添加用户
@app.route('/demos', methods=['POST'])
def add_demo():
data = request.get_json()
new_demo = Demo(info1=data['info1'], info2=data['info2'])
db.session.add(new_demo)
db.session.commit()
return jsonify({'message': 'Demo added successfully'})

# 获取所有用户
@app.route('/demos', methods=['GET'])
def get_demos():
demos = Demo.query.all()
demo_list = []
for demo in demos:
demo_data = {'id': demo.id, 'info1': demo.info1, 'info2': demo.info2}
demo_list.append(demo_data)
return jsonify({'demos': demo_list})

# 获取单个用户
@app.route('/demos/<int:demo_id>', methods=['GET'])
def get_demo(demo_id):
demo = Demo.query.get(demo_id)
if demo:
demo_data = {'id': demo.id, 'info1': demo.info1, 'info2': demo.info2}
return jsonify(demo_data)
return jsonify({'message': 'Demo not found'}), 404

# 更新用户信息
@app.route('/demos/<int:demo_id>', methods=['PUT'])
def update_demo(demo_id):
demo = Demo.query.get(demo_id)
if not demo:
return jsonify({'message': 'Demo not found'}), 404

data = request.get_json()
demo.info1 = data['info1']
demo.info2 = data['info2']
db.session.commit()
return jsonify({'message': 'Demo updated successfully'})

# 删除用户
@app.route('/demos/<int:demo_id>', methods=['DELETE'])
def delete_demo(demo_id):
demo = Demo.query.get(demo_id)
if not demo:
return jsonify({'message': 'Demo not found'}), 404

db.session.delete(demo)
db.session.commit()
return jsonify({'message': 'Demo deleted successfully'})
`app/routes.py`文件中创建以下方法
1 change: 1 addition & 0 deletions app/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`app/utils.py`文件中创建以下方法
92 changes: 92 additions & 0 deletions app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from flask import jsonify
from app.services import (
get_product_info,
add_product_to_cart,
remove_product_from_cart,
update_cart_product_quantity,
checkout_and_select_payment_method,
get_order_history,
get_address,
update_address,
register,
login
)

def get_products():
product_info = get_product_info()
return jsonify(product_info)

def add_to_cart():
# Get product details from request
product_id = request.json.get('product_id')
quantity = request.json.get('quantity')

# Add product to cart
add_product_to_cart(product_id, quantity)

return jsonify({'message': 'Product added to cart successfully'})

def remove_from_cart():
# Get product details from request
product_id = request.json.get('product_id')

# Remove product from cart
remove_product_from_cart(product_id)

return jsonify({'message': 'Product removed from cart successfully'})

def update_cart():
# Get product details from request
product_id = request.json.get('product_id')
quantity = request.json.get('quantity')

# Update product quantity in cart
update_cart_product_quantity(product_id, quantity)

return jsonify({'message': 'Cart updated successfully'})

def checkout():
# Get payment method from request
payment_method = request.json.get('payment_method')

# Checkout and select payment method
checkout_and_select_payment_method(payment_method)

return jsonify({'message': 'Checkout successful'})

def get_order_history():
order_history = get_order_history()
return jsonify(order_history)

def get_address():
address = get_address()
return jsonify(address)

def update_address():
# Get address details from request
address = request.json.get('address')

# Update address
update_address(address)

return jsonify({'message': 'Address updated successfully'})

def register():
# Get user details from request
username = request.json.get('username')
password = request.json.get('password')

# Register new user account
register(username, password)

return jsonify({'message': 'Registration successful'})

def login():
# Get user details from request
username = request.json.get('username')
password = request.json.get('password')

# Login user
login(username, password)

return jsonify({'message': 'Login successful'})

0 comments on commit bbf833c

Please sign in to comment.