200 lines
5.6 KiB
Python
200 lines
5.6 KiB
Python
"""
|
|
页面路由:静态页面 + 图标
|
|
"""
|
|
import os
|
|
import io
|
|
from flask import Blueprint, request, redirect, send_from_directory, send_file
|
|
from database import get_form, get_session, get_user
|
|
from config import TEMPLATES_DIR, IMG_DIR, PIL_AVAILABLE
|
|
from utils.expiry import get_expiry_info
|
|
from routes.auth import require_login
|
|
|
|
bp = Blueprint('pages', __name__)
|
|
|
|
|
|
@bp.route('/')
|
|
def index():
|
|
"""根路由重定向到管理后台"""
|
|
return redirect('/admin')
|
|
|
|
|
|
@bp.route('/admin')
|
|
@require_login
|
|
def admin_page():
|
|
return send_from_directory(TEMPLATES_DIR, 'admin.html')
|
|
|
|
|
|
@bp.route('/create')
|
|
@require_login
|
|
def create_form():
|
|
return send_from_directory(TEMPLATES_DIR, 'create.html')
|
|
|
|
|
|
@bp.route('/status')
|
|
@require_login
|
|
def status_page():
|
|
return send_from_directory(TEMPLATES_DIR, 'status.html')
|
|
|
|
|
|
@bp.route('/user_manage')
|
|
@require_login
|
|
def user_manage_page():
|
|
return send_from_directory(TEMPLATES_DIR, 'user_manage.html')
|
|
|
|
|
|
@bp.route('/preset_manage')
|
|
@require_login
|
|
def preset_manage_page():
|
|
return send_from_directory(TEMPLATES_DIR, 'preset_manage.html')
|
|
|
|
|
|
@bp.route('/edit/<form_id>')
|
|
@require_login
|
|
def edit_form_page(form_id):
|
|
"""编辑表单页面"""
|
|
form = get_form(form_id)
|
|
if not form:
|
|
return redirect('/error?code=404&message=表单不存在')
|
|
|
|
session_id = request.cookies.get('session_id')
|
|
session_data = get_session(session_id)
|
|
if not session_data:
|
|
return redirect('/login')
|
|
username = session_data['username']
|
|
user = get_user(username)
|
|
managed_forms = user.get('managed_forms', []) if user else []
|
|
|
|
if user['role'] != 'admin' and form_id not in managed_forms:
|
|
return redirect('/error?code=403&message=无权编辑此表单')
|
|
|
|
return send_from_directory(TEMPLATES_DIR, 'edit.html')
|
|
|
|
|
|
@bp.route('/motion-demo')
|
|
def motion_demo_page():
|
|
"""交互式文字动画库示例页"""
|
|
return send_from_directory(TEMPLATES_DIR, 'motion-demo.html')
|
|
|
|
|
|
@bp.route('/<form_id>')
|
|
def dynamic_form(form_id):
|
|
"""动态表单页面"""
|
|
form = get_form(form_id)
|
|
if not form:
|
|
return redirect('/error?code=404&message=表单不存在或已过期')
|
|
# 检查是否过期
|
|
expiry_info = get_expiry_info(form.get('createdAt', ''), form.get('expiresAt'))
|
|
if expiry_info and expiry_info.get('is_expired'):
|
|
return redirect('/error?code=410&message=表单已过期')
|
|
return send_from_directory(TEMPLATES_DIR, 'index.html')
|
|
|
|
|
|
@bp.route('/error')
|
|
def error_page():
|
|
return send_from_directory(TEMPLATES_DIR, 'error.html')
|
|
|
|
|
|
@bp.route('/favicon.ico')
|
|
def favicon():
|
|
"""提供网站图标"""
|
|
ico_path = os.path.join(IMG_DIR, 'favicon.ico')
|
|
if os.path.exists(ico_path):
|
|
return send_file(ico_path, mimetype='image/x-icon')
|
|
|
|
png_path = os.path.join(IMG_DIR, 'favicon-32x32.png')
|
|
if os.path.exists(png_path):
|
|
return send_file(png_path, mimetype='image/png')
|
|
|
|
source_path = os.path.join(IMG_DIR, 'favicon.png')
|
|
if os.path.exists(source_path):
|
|
return send_file(source_path, mimetype='image/png')
|
|
|
|
return '', 404
|
|
|
|
|
|
@bp.route('/favicon-<int:w>x<int:h>.png')
|
|
def favicon_size(w, h):
|
|
size = w
|
|
valid_sizes = [16, 32]
|
|
if size not in valid_sizes:
|
|
return '', 404
|
|
|
|
path = os.path.join(IMG_DIR, f'favicon-{size}x{size}.png')
|
|
if os.path.exists(path):
|
|
return send_file(path, mimetype='image/png')
|
|
|
|
source_path = os.path.join(IMG_DIR, 'favicon.png')
|
|
if os.path.exists(source_path) and PIL_AVAILABLE:
|
|
try:
|
|
from PIL import Image
|
|
img = Image.open(source_path)
|
|
img = img.resize((size, size), Image.Resampling.LANCZOS)
|
|
img_io = io.BytesIO()
|
|
img.save(img_io, 'PNG', optimize=True)
|
|
img_io.seek(0)
|
|
return send_file(img_io, mimetype='image/png')
|
|
except:
|
|
pass
|
|
|
|
return '', 404
|
|
|
|
|
|
@bp.route('/apple-touch-icon.png')
|
|
def apple_touch_icon():
|
|
icon_path = os.path.join(IMG_DIR, 'apple-touch-icon.png')
|
|
if os.path.exists(icon_path):
|
|
return send_file(icon_path, mimetype='image/png')
|
|
|
|
source_path = os.path.join(IMG_DIR, 'favicon.png')
|
|
if os.path.exists(source_path) and PIL_AVAILABLE:
|
|
try:
|
|
from PIL import Image, ImageDraw
|
|
img = Image.open(source_path)
|
|
img = img.convert('RGBA')
|
|
img = img.resize((180, 180), Image.Resampling.LANCZOS)
|
|
|
|
mask = Image.new('L', (180, 180), 0)
|
|
draw = ImageDraw.Draw(mask)
|
|
draw.rounded_rectangle([0, 0, 180, 180], radius=36, fill=255)
|
|
|
|
output = Image.new('RGBA', (180, 180), (255, 255, 255, 0))
|
|
output.paste(img, (0, 0))
|
|
output.putalpha(mask)
|
|
|
|
img_io = io.BytesIO()
|
|
output.save(img_io, 'PNG', optimize=True)
|
|
img_io.seek(0)
|
|
return send_file(img_io, mimetype='image/png')
|
|
except:
|
|
pass
|
|
|
|
return '', 404
|
|
|
|
|
|
@bp.route('/android-chrome-<int:w>x<int:h>.png')
|
|
def android_chrome(w, h):
|
|
size = w
|
|
valid_sizes = [192, 512]
|
|
if size not in valid_sizes:
|
|
return '', 404
|
|
|
|
path = os.path.join(IMG_DIR, f'android-chrome-{size}x{size}.png')
|
|
if os.path.exists(path):
|
|
return send_file(path, mimetype='image/png')
|
|
|
|
source_path = os.path.join(IMG_DIR, 'favicon.png')
|
|
if os.path.exists(source_path) and PIL_AVAILABLE:
|
|
try:
|
|
from PIL import Image
|
|
img = Image.open(source_path)
|
|
img = img.resize((size, size), Image.Resampling.LANCZOS)
|
|
|
|
img_io = io.BytesIO()
|
|
img.save(img_io, 'PNG', optimize=True)
|
|
img_io.seek(0)
|
|
return send_file(img_io, mimetype='image/png')
|
|
except:
|
|
pass
|
|
|
|
return '', 404
|