- 新增 OpenClaw Gateway 连接器 (api/openclaw_connector.py)
- 新增聊天 WebSocket 路由 (api/chat.py)
- 新增聊天界面模板 (templates/chat/index.html)
- 新增聊天样式 (static/css/chat.css)
- 修改 app.py 支持 SocketIO
- 登录后默认跳转到聊天界面
作者:小白 🐶
193 lines
5.7 KiB
Python
193 lines
5.7 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
OpenClaw Mission Control - Flask 版本
|
||
支持登录、注册、控制中心、聊天功能
|
||
作者:小白 🐶
|
||
"""
|
||
|
||
from flask import Flask, render_template, redirect, url_for, request, flash, make_response
|
||
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
|
||
from flask_sqlalchemy import SQLAlchemy
|
||
from flask_socketio import SocketIO
|
||
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
|
||
|
||
# 初始化 SocketIO
|
||
socketio = SocketIO(app, cors_allowed_origins="*", async_mode='threading')
|
||
|
||
# 禁用缓存 - 解决浏览器缓存问题
|
||
@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
|
||
|
||
# 初始化数据库
|
||
db = SQLAlchemy(app)
|
||
|
||
# 初始化登录管理
|
||
login_manager = LoginManager()
|
||
login_manager.init_app(app)
|
||
login_manager.login_view = 'login'
|
||
|
||
# 注册 API Blueprint
|
||
from api import api
|
||
app.register_blueprint(api)
|
||
|
||
# 注册聊天 Socket Handlers
|
||
from api.chat import register_socket_handlers, init_gateways
|
||
|
||
|
||
# 用户模型
|
||
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:
|
||
return redirect(url_for('chat'))
|
||
return render_template('index.html')
|
||
|
||
|
||
# 路由 - 聊天界面
|
||
@app.route('/chat')
|
||
@login_required
|
||
def chat():
|
||
return render_template('chat/index.html', username=current_user.username)
|
||
|
||
|
||
# 路由 - 控制中心仪表盘
|
||
@app.route('/dashboard')
|
||
@login_required
|
||
def dashboard():
|
||
return render_template('dashboard/index.html', username=current_user.username)
|
||
|
||
|
||
# 路由 - 登录
|
||
@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__':
|
||
# 注册 Socket.IO 事件处理器
|
||
register_socket_handlers(socketio)
|
||
|
||
# 初始化 Gateway 连接
|
||
init_gateways()
|
||
|
||
# 启动应用(使用 SocketIO)
|
||
print("🚀 启动 OpenClaw Mission Control...")
|
||
print("📍 访问地址: http://0.0.0.0:5000")
|
||
socketio.run(app, host='0.0.0.0', port=5000, debug=False, allow_unsafe_werkzeug=True)
|