v1.8.0: 模块化重构 + 后台登录认证
This commit is contained in:
72
modules/routes/api_categories.py
Normal file
72
modules/routes/api_categories.py
Normal file
@@ -0,0 +1,72 @@
|
||||
"""
|
||||
分类管理 API
|
||||
"""
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from flask import Blueprint, request, jsonify
|
||||
from config import CATEGORIES_FILE
|
||||
from utils import load_data, save_data
|
||||
|
||||
cat_bp = Blueprint('api_categories', __name__)
|
||||
|
||||
|
||||
@cat_bp.route('/api/categories')
|
||||
def api_categories():
|
||||
categories = load_data(CATEGORIES_FILE)
|
||||
hide_hidden = request.args.get('all', '0') == '0'
|
||||
if hide_hidden:
|
||||
categories = [c for c in categories if c.get('visible', True)]
|
||||
return jsonify(sorted(categories, key=lambda x: x.get('order', 0)))
|
||||
|
||||
|
||||
@cat_bp.route('/api/categories/<category_id>')
|
||||
def api_category_detail(category_id):
|
||||
categories = load_data(CATEGORIES_FILE)
|
||||
category = next((c for c in categories if c['id'] == category_id), None)
|
||||
if not category:
|
||||
return jsonify({'error': 'Category not found'}), 404
|
||||
return jsonify(category)
|
||||
|
||||
|
||||
@cat_bp.route('/api/categories', methods=['POST'])
|
||||
def api_create_category():
|
||||
data = request.get_json()
|
||||
categories = load_data(CATEGORIES_FILE)
|
||||
data['id'] = uuid.uuid4().hex[:12]
|
||||
data['created_at'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||
data['visible'] = data.get('visible', True)
|
||||
categories.append(data)
|
||||
save_data(CATEGORIES_FILE, categories)
|
||||
return jsonify(data)
|
||||
|
||||
|
||||
@cat_bp.route('/api/categories/<category_id>', methods=['PUT'])
|
||||
def api_update_category(category_id):
|
||||
data = request.get_json()
|
||||
categories = load_data(CATEGORIES_FILE)
|
||||
category = next((c for c in categories if c['id'] == category_id), None)
|
||||
if not category:
|
||||
return jsonify({'error': 'Category not found'}), 404
|
||||
category.update(data)
|
||||
category['updated_at'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||
save_data(CATEGORIES_FILE, categories)
|
||||
return jsonify(category)
|
||||
|
||||
|
||||
@cat_bp.route('/api/categories/<category_id>', methods=['DELETE'])
|
||||
def api_delete_category(category_id):
|
||||
categories = load_data(CATEGORIES_FILE)
|
||||
categories = [c for c in categories if c['id'] != category_id]
|
||||
save_data(CATEGORIES_FILE, categories)
|
||||
return jsonify({'success': True})
|
||||
|
||||
|
||||
@cat_bp.route('/api/categories/<category_id>/visible', methods=['POST'])
|
||||
def api_toggle_category_visible(category_id):
|
||||
categories = load_data(CATEGORIES_FILE)
|
||||
category = next((c for c in categories if c['id'] == category_id), None)
|
||||
if not category:
|
||||
return jsonify({'error': 'Category not found'}), 404
|
||||
category['visible'] = not category.get('visible', True)
|
||||
save_data(CATEGORIES_FILE, categories)
|
||||
return jsonify({'success': True, 'visible': category['visible']})
|
||||
Reference in New Issue
Block a user