154 lines
4.7 KiB
Python
154 lines
4.7 KiB
Python
"""
|
|
浏览历史管理视图
|
|
"""
|
|
from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify, session
|
|
from app.models.browse_history import BrowseHistory
|
|
from app.models.product import Product
|
|
from app.utils.decorators import login_required
|
|
from config.database import db
|
|
|
|
history_bp = Blueprint('history', __name__, url_prefix='/history')
|
|
|
|
|
|
@history_bp.route('/')
|
|
@login_required
|
|
def index():
|
|
"""浏览历史页面"""
|
|
page = request.args.get('page', 1, type=int)
|
|
per_page = 20
|
|
|
|
# 获取用户浏览历史
|
|
history = BrowseHistory.get_user_history(session['user_id'], page, per_page)
|
|
|
|
# 获取浏览历史总数
|
|
total_count = BrowseHistory.get_user_history_count(session['user_id'])
|
|
|
|
return render_template('user/history.html',
|
|
history=history,
|
|
total_count=total_count)
|
|
|
|
|
|
@history_bp.route('/add', methods=['POST'])
|
|
@login_required
|
|
def add():
|
|
"""添加浏览记录"""
|
|
try:
|
|
data = request.get_json()
|
|
product_id = data.get('product_id')
|
|
|
|
if not product_id:
|
|
return jsonify({'success': False, 'message': '商品ID不能为空'})
|
|
|
|
success, message = BrowseHistory.add_history(session['user_id'], product_id)
|
|
|
|
if success:
|
|
# 获取更新后的浏览历史数量
|
|
history_count = BrowseHistory.get_user_history_count(session['user_id'])
|
|
return jsonify({
|
|
'success': True,
|
|
'message': message,
|
|
'history_count': history_count
|
|
})
|
|
else:
|
|
return jsonify({'success': False, 'message': message})
|
|
|
|
except Exception as e:
|
|
return jsonify({'success': False, 'message': f'添加浏览记录失败: {str(e)}'})
|
|
|
|
|
|
@history_bp.route('/remove', methods=['POST'])
|
|
@login_required
|
|
def remove():
|
|
"""删除单个浏览记录"""
|
|
try:
|
|
data = request.get_json()
|
|
product_id = data.get('product_id')
|
|
|
|
if not product_id:
|
|
return jsonify({'success': False, 'message': '商品ID不能为空'})
|
|
|
|
success, message = BrowseHistory.remove_history_item(session['user_id'], product_id)
|
|
|
|
if success:
|
|
# 获取更新后的浏览历史数量
|
|
history_count = BrowseHistory.get_user_history_count(session['user_id'])
|
|
return jsonify({
|
|
'success': True,
|
|
'message': message,
|
|
'history_count': history_count
|
|
})
|
|
else:
|
|
return jsonify({'success': False, 'message': message})
|
|
|
|
except Exception as e:
|
|
return jsonify({'success': False, 'message': f'删除浏览记录失败: {str(e)}'})
|
|
|
|
|
|
@history_bp.route('/clear', methods=['POST'])
|
|
@login_required
|
|
def clear():
|
|
"""清空浏览历史"""
|
|
try:
|
|
success, message = BrowseHistory.clear_user_history(session['user_id'])
|
|
|
|
return jsonify({
|
|
'success': success,
|
|
'message': message,
|
|
'history_count': 0 if success else BrowseHistory.get_user_history_count(session['user_id'])
|
|
})
|
|
|
|
except Exception as e:
|
|
return jsonify({'success': False, 'message': f'清空浏览历史失败: {str(e)}'})
|
|
|
|
|
|
@history_bp.route('/batch-remove', methods=['POST'])
|
|
@login_required
|
|
def batch_remove():
|
|
"""批量删除浏览记录"""
|
|
try:
|
|
data = request.get_json()
|
|
product_ids = data.get('product_ids', [])
|
|
|
|
if not product_ids:
|
|
return jsonify({'success': False, 'message': '请选择要删除的商品'})
|
|
|
|
success_count = 0
|
|
fail_count = 0
|
|
|
|
for product_id in product_ids:
|
|
success, _ = BrowseHistory.remove_history_item(session['user_id'], product_id)
|
|
if success:
|
|
success_count += 1
|
|
else:
|
|
fail_count += 1
|
|
|
|
message = f'成功删除 {success_count} 个浏览记录'
|
|
if fail_count > 0:
|
|
message += f',失败 {fail_count} 个'
|
|
|
|
# 获取更新后的浏览历史数量
|
|
history_count = BrowseHistory.get_user_history_count(session['user_id'])
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'message': message,
|
|
'history_count': history_count
|
|
})
|
|
|
|
except Exception as e:
|
|
return jsonify({'success': False, 'message': f'批量操作失败: {str(e)}'})
|
|
|
|
|
|
@history_bp.route('/count')
|
|
@login_required
|
|
def count():
|
|
"""获取浏览历史数量"""
|
|
try:
|
|
history_count = BrowseHistory.get_user_history_count(session['user_id'])
|
|
return jsonify({
|
|
'success': True,
|
|
'history_count': history_count
|
|
})
|
|
except Exception as e:
|
|
return jsonify({'success': False, 'message': str(e)})
|