2026-03-12 08:35:52 +08:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
"""
|
2026-03-12 20:42:12 +08:00
|
|
|
|
OpenClaw Mission Control - Flask 版本
|
|
|
|
|
|
支持登录、注册、控制中心功能
|
2026-03-12 08:35:52 +08:00
|
|
|
|
作者:小白 🐶
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
2026-03-12 10:54:10 +08:00
|
|
|
|
from flask import Flask, render_template, redirect, url_for, request, flash, make_response
|
2026-03-12 08:35:52 +08:00
|
|
|
|
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
|
|
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
# 初始化应用
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
app.config['SECRET_KEY'] = 'xiaobai-secret-key-2026'
|
|
|
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
|
|
|
|
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
|
|
|
2026-03-12 10:54:10 +08:00
|
|
|
|
# 禁用缓存 - 解决浏览器缓存问题
|
|
|
|
|
|
@app.after_request
|
|
|
|
|
|
def add_no_cache_headers(response):
|
|
|
|
|
|
"""为 HTML 页面添加禁用缓存的响应头"""
|
|
|
|
|
|
if 'text/html' in response.content_type:
|
|
|
|
|
|
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
|
|
|
|
|
|
response.headers['Pragma'] = 'no-cache'
|
|
|
|
|
|
response.headers['Expires'] = '0'
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
2026-03-12 08:35:52 +08:00
|
|
|
|
# 初始化数据库
|
|
|
|
|
|
db = SQLAlchemy(app)
|
|
|
|
|
|
|
|
|
|
|
|
# 初始化登录管理
|
|
|
|
|
|
login_manager = LoginManager()
|
|
|
|
|
|
login_manager.init_app(app)
|
|
|
|
|
|
login_manager.login_view = 'login'
|
|
|
|
|
|
|
2026-03-12 20:42:12 +08:00
|
|
|
|
# 注册 API Blueprint
|
|
|
|
|
|
from api import api
|
|
|
|
|
|
app.register_blueprint(api)
|
|
|
|
|
|
|
2026-03-12 08:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
# 用户模型
|
|
|
|
|
|
class User(UserMixin, db.Model):
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
username = db.Column(db.String(80), unique=True, nullable=False)
|
|
|
|
|
|
password_hash = db.Column(db.String(120), nullable=False)
|
|
|
|
|
|
|
|
|
|
|
|
def set_password(self, password):
|
|
|
|
|
|
self.password_hash = generate_password_hash(password)
|
|
|
|
|
|
|
|
|
|
|
|
def check_password(self, password):
|
|
|
|
|
|
return check_password_hash(self.password_hash, password)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_manager.user_loader
|
|
|
|
|
|
def load_user(user_id):
|
|
|
|
|
|
return User.query.get(int(user_id))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 路由 - 首页
|
|
|
|
|
|
@app.route('/')
|
|
|
|
|
|
def index():
|
|
|
|
|
|
if current_user.is_authenticated:
|
2026-03-12 20:42:12 +08:00
|
|
|
|
return redirect(url_for('dashboard'))
|
2026-03-12 08:35:52 +08:00
|
|
|
|
return render_template('index.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-03-12 20:42:12 +08:00
|
|
|
|
# 路由 - 控制中心仪表盘
|
|
|
|
|
|
@app.route('/dashboard')
|
|
|
|
|
|
@login_required
|
|
|
|
|
|
def dashboard():
|
|
|
|
|
|
return render_template('dashboard/index.html', username=current_user.username)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-03-12 08:35:52 +08:00
|
|
|
|
# 路由 - 登录
|
|
|
|
|
|
@app.route('/login', methods=['GET', 'POST'])
|
|
|
|
|
|
def login():
|
|
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
username = request.form.get('username')
|
|
|
|
|
|
password = request.form.get('password')
|
|
|
|
|
|
|
|
|
|
|
|
user = User.query.filter_by(username=username).first()
|
|
|
|
|
|
|
|
|
|
|
|
if user and user.check_password(password):
|
|
|
|
|
|
login_user(user)
|
|
|
|
|
|
flash('登录成功!欢迎回来~ 🐶', 'success')
|
|
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
else:
|
|
|
|
|
|
flash('用户名或密码错误!', 'error')
|
|
|
|
|
|
|
|
|
|
|
|
return render_template('login.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 路由 - 注册
|
|
|
|
|
|
@app.route('/register', methods=['GET', 'POST'])
|
|
|
|
|
|
def register():
|
|
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
username = request.form.get('username')
|
|
|
|
|
|
password = request.form.get('password')
|
|
|
|
|
|
confirm_password = request.form.get('confirm_password')
|
|
|
|
|
|
|
|
|
|
|
|
# 验证
|
|
|
|
|
|
if not username or not password:
|
|
|
|
|
|
flash('用户名和密码不能为空!', 'error')
|
|
|
|
|
|
return render_template('register.html')
|
|
|
|
|
|
|
|
|
|
|
|
if password != confirm_password:
|
|
|
|
|
|
flash('两次密码不一致!', 'error')
|
|
|
|
|
|
return render_template('register.html')
|
|
|
|
|
|
|
|
|
|
|
|
if len(username) < 3:
|
|
|
|
|
|
flash('用户名至少3个字符!', 'error')
|
|
|
|
|
|
return render_template('register.html')
|
|
|
|
|
|
|
|
|
|
|
|
if len(password) < 6:
|
|
|
|
|
|
flash('密码至少6个字符!', 'error')
|
|
|
|
|
|
return render_template('register.html')
|
|
|
|
|
|
|
|
|
|
|
|
# 检查用户是否存在
|
|
|
|
|
|
existing_user = User.query.filter_by(username=username).first()
|
|
|
|
|
|
if existing_user:
|
|
|
|
|
|
flash('用户名已存在!', 'error')
|
|
|
|
|
|
return render_template('register.html')
|
|
|
|
|
|
|
|
|
|
|
|
# 创建新用户
|
|
|
|
|
|
user = User(username=username)
|
|
|
|
|
|
user.set_password(password)
|
|
|
|
|
|
db.session.add(user)
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
flash('注册成功!请登录~ 🎉', 'success')
|
|
|
|
|
|
return redirect(url_for('login'))
|
|
|
|
|
|
|
|
|
|
|
|
return render_template('register.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 路由 - 登出
|
|
|
|
|
|
@app.route('/logout')
|
|
|
|
|
|
@login_required
|
|
|
|
|
|
def logout():
|
|
|
|
|
|
logout_user()
|
|
|
|
|
|
flash('已退出登录!', 'success')
|
|
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 创建数据库
|
|
|
|
|
|
with app.app_context():
|
|
|
|
|
|
db.create_all()
|
|
|
|
|
|
|
|
|
|
|
|
# 创建默认管理员账户
|
|
|
|
|
|
admin = User.query.filter_by(username='yunxiafei').first()
|
|
|
|
|
|
if not admin:
|
|
|
|
|
|
admin = User(username='yunxiafei')
|
|
|
|
|
|
admin.set_password('xu123654')
|
|
|
|
|
|
db.session.add(admin)
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
print("✅ 默认管理员账户已创建: yunxiafei / xu123654")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
app.run(host='0.0.0.0', port=5000, debug=False)
|