226 lines
7.1 KiB
Python
226 lines
7.1 KiB
Python
|
|
from flask import request, jsonify, current_app
|
||
|
|
from datetime import datetime
|
||
|
|
from app import db
|
||
|
|
from app.models import Product
|
||
|
|
from . import api_bp
|
||
|
|
from .license import require_admin
|
||
|
|
import traceback
|
||
|
|
import sys
|
||
|
|
|
||
|
|
@api_bp.route('/products', methods=['GET'])
|
||
|
|
@require_admin
|
||
|
|
def get_products():
|
||
|
|
"""获取产品列表"""
|
||
|
|
try:
|
||
|
|
current_app.logger.info("开始获取产品列表")
|
||
|
|
current_app.logger.info(f"Python版本: {sys.version}")
|
||
|
|
current_app.logger.info(f"当前用户: {getattr(current_app, 'current_user', 'Unknown')}")
|
||
|
|
|
||
|
|
# 检查current_user是否存在
|
||
|
|
try:
|
||
|
|
from flask_login import current_user
|
||
|
|
current_app.logger.info(f"Flask-Login current_user: {current_user}")
|
||
|
|
current_app.logger.info(f"用户认证状态: {current_user.is_authenticated if hasattr(current_user, 'is_authenticated') else 'Unknown'}")
|
||
|
|
except Exception as e:
|
||
|
|
current_app.logger.error(f"获取current_user失败: {e}")
|
||
|
|
|
||
|
|
page = request.args.get('page', 1, type=int)
|
||
|
|
per_page = min(request.args.get('per_page', 20, type=int), 100)
|
||
|
|
keyword = request.args.get('keyword', '').strip()
|
||
|
|
current_app.logger.info(f"查询参数: page={page}, per_page={per_page}, keyword={keyword}")
|
||
|
|
|
||
|
|
query = Product.query
|
||
|
|
|
||
|
|
if keyword:
|
||
|
|
query = query.filter(
|
||
|
|
Product.product_name.like(f'%{keyword}%') |
|
||
|
|
Product.description.like(f'%{keyword}%')
|
||
|
|
)
|
||
|
|
|
||
|
|
query = query.order_by(Product.create_time.desc())
|
||
|
|
pagination = query.paginate(page=page, per_page=per_page, error_out=False)
|
||
|
|
|
||
|
|
products = [product.to_dict(include_stats=True) for product in pagination.items]
|
||
|
|
|
||
|
|
return jsonify({
|
||
|
|
'success': True,
|
||
|
|
'data': {
|
||
|
|
'products': products,
|
||
|
|
'pagination': {
|
||
|
|
'page': page,
|
||
|
|
'per_page': per_page,
|
||
|
|
'total': pagination.total,
|
||
|
|
'pages': pagination.pages,
|
||
|
|
'has_prev': pagination.has_prev,
|
||
|
|
'has_next': pagination.has_next
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
current_app.logger.error(f"获取产品列表失败: {str(e)}")
|
||
|
|
current_app.logger.error(f"错误类型: {type(e)}")
|
||
|
|
import traceback
|
||
|
|
current_app.logger.error(f"错误堆栈: {traceback.format_exc()}")
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': '服务器内部错误'
|
||
|
|
}), 500
|
||
|
|
|
||
|
|
@api_bp.route('/products', methods=['POST'])
|
||
|
|
@require_admin
|
||
|
|
def create_product():
|
||
|
|
"""创建产品"""
|
||
|
|
try:
|
||
|
|
data = request.get_json()
|
||
|
|
if not data:
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': '请求数据为空'
|
||
|
|
}), 400
|
||
|
|
|
||
|
|
product_name = data.get('product_name', '').strip()
|
||
|
|
description = data.get('description', '').strip()
|
||
|
|
custom_id = data.get('product_id', '').strip()
|
||
|
|
|
||
|
|
if not product_name:
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': '产品名称不能为空'
|
||
|
|
}), 400
|
||
|
|
|
||
|
|
# 检查自定义ID是否重复
|
||
|
|
if custom_id:
|
||
|
|
existing = Product.query.filter_by(product_id=custom_id).first()
|
||
|
|
if existing:
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': '产品ID已存在'
|
||
|
|
}), 400
|
||
|
|
|
||
|
|
# 创建产品
|
||
|
|
product = Product(
|
||
|
|
product_id=custom_id if custom_id else None,
|
||
|
|
product_name=product_name,
|
||
|
|
description=description,
|
||
|
|
status=1
|
||
|
|
)
|
||
|
|
|
||
|
|
db.session.add(product)
|
||
|
|
db.session.commit()
|
||
|
|
|
||
|
|
return jsonify({
|
||
|
|
'success': True,
|
||
|
|
'message': '产品创建成功',
|
||
|
|
'data': product.to_dict()
|
||
|
|
})
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
db.session.rollback()
|
||
|
|
current_app.logger.error(f"创建产品失败: {str(e)}")
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': '服务器内部错误'
|
||
|
|
}), 500
|
||
|
|
|
||
|
|
@api_bp.route('/products/<product_id>', methods=['GET'])
|
||
|
|
@require_admin
|
||
|
|
def get_product(product_id):
|
||
|
|
"""获取产品详情"""
|
||
|
|
try:
|
||
|
|
product = Product.query.filter_by(product_id=product_id).first()
|
||
|
|
if not product:
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': '产品不存在'
|
||
|
|
}), 404
|
||
|
|
|
||
|
|
return jsonify({
|
||
|
|
'success': True,
|
||
|
|
'data': product.to_dict(include_stats=True)
|
||
|
|
})
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
current_app.logger.error(f"获取产品详情失败: {str(e)}")
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': '服务器内部错误'
|
||
|
|
}), 500
|
||
|
|
|
||
|
|
@api_bp.route('/products/<product_id>', methods=['PUT'])
|
||
|
|
@require_admin
|
||
|
|
def update_product(product_id):
|
||
|
|
"""更新产品"""
|
||
|
|
try:
|
||
|
|
product = Product.query.filter_by(product_id=product_id).first()
|
||
|
|
if not product:
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': '产品不存在'
|
||
|
|
}), 404
|
||
|
|
|
||
|
|
data = request.get_json()
|
||
|
|
if not data:
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': '请求数据为空'
|
||
|
|
}), 400
|
||
|
|
|
||
|
|
if 'product_name' in data:
|
||
|
|
product.product_name = data['product_name'].strip()
|
||
|
|
if 'description' in data:
|
||
|
|
product.description = data['description'].strip()
|
||
|
|
if 'status' in data:
|
||
|
|
product.status = data['status']
|
||
|
|
|
||
|
|
db.session.commit()
|
||
|
|
|
||
|
|
return jsonify({
|
||
|
|
'success': True,
|
||
|
|
'message': '产品更新成功',
|
||
|
|
'data': product.to_dict()
|
||
|
|
})
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
db.session.rollback()
|
||
|
|
current_app.logger.error(f"更新产品失败: {str(e)}")
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': '服务器内部错误'
|
||
|
|
}), 500
|
||
|
|
|
||
|
|
@api_bp.route('/products/<product_id>', methods=['DELETE'])
|
||
|
|
@require_admin
|
||
|
|
def delete_product(product_id):
|
||
|
|
"""删除产品"""
|
||
|
|
try:
|
||
|
|
product = Product.query.filter_by(product_id=product_id).first()
|
||
|
|
if not product:
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': '产品不存在'
|
||
|
|
}), 404
|
||
|
|
|
||
|
|
# 检查是否有关联的卡密
|
||
|
|
license_count = product.licenses.count()
|
||
|
|
if license_count > 0:
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': f'产品下还有 {license_count} 个卡密,无法删除'
|
||
|
|
}), 400
|
||
|
|
|
||
|
|
db.session.delete(product)
|
||
|
|
db.session.commit()
|
||
|
|
|
||
|
|
return jsonify({
|
||
|
|
'success': True,
|
||
|
|
'message': '产品删除成功'
|
||
|
|
})
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
db.session.rollback()
|
||
|
|
current_app.logger.error(f"删除产品失败: {str(e)}")
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': '服务器内部错误'
|
||
|
|
}), 500
|