-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
实现购物车功能,允许用户添加和管理购物车中的商品。 集成支付处理,以接受信用卡、PayPal、USDT、微信、等付款方式。 提供用户账户管理,包括订单历史和地址管理。 剩下的你可以给我一些建议
- Loading branch information
1 parent
b146038
commit bbf833c
Showing
4 changed files
with
95 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`文件中创建以下类和方法: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`文件中创建以下方法: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
在`app/utils.py`文件中创建以下方法: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}) |