223 lines
6.0 KiB
Python
223 lines
6.0 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
"""
|
|||
|
|
登录问题诊断脚本
|
|||
|
|
"""
|
|||
|
|
import sqlite3
|
|||
|
|
import os
|
|||
|
|
|
|||
|
|
DB_PATH = 'server/data.db'
|
|||
|
|
|
|||
|
|
def check_database():
|
|||
|
|
"""检查数据库和用户"""
|
|||
|
|
print("=" * 60)
|
|||
|
|
print("🔍 检查数据库...")
|
|||
|
|
print("=" * 60)
|
|||
|
|
|
|||
|
|
if not os.path.exists(DB_PATH):
|
|||
|
|
print("❌ 数据库文件不存在:", DB_PATH)
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
print("✅ 数据库文件存在")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
conn = sqlite3.connect(DB_PATH)
|
|||
|
|
c = conn.cursor()
|
|||
|
|
|
|||
|
|
# 检查用户表
|
|||
|
|
users = c.execute('SELECT username, role FROM users').fetchall()
|
|||
|
|
|
|||
|
|
if not users:
|
|||
|
|
print("❌ 没有找到任何用户")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
print(f"\n✅ 找到 {len(users)} 个用户:")
|
|||
|
|
print("-" * 60)
|
|||
|
|
for username, role in users:
|
|||
|
|
print(f" 👤 用户名: {username:15s} | 角色: {role}")
|
|||
|
|
|
|||
|
|
conn.close()
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 数据库错误: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def check_server():
|
|||
|
|
"""检查服务器状态"""
|
|||
|
|
print("\n" + "=" * 60)
|
|||
|
|
print("🔍 检查服务器...")
|
|||
|
|
print("=" * 60)
|
|||
|
|
|
|||
|
|
import subprocess
|
|||
|
|
|
|||
|
|
# 检查进程
|
|||
|
|
try:
|
|||
|
|
result = subprocess.run(
|
|||
|
|
['ps', 'aux'],
|
|||
|
|
capture_output=True,
|
|||
|
|
text=True
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
if 'python' in result.stdout and 'app.py' in result.stdout:
|
|||
|
|
print("✅ 服务器正在运行")
|
|||
|
|
|
|||
|
|
# 提取进程信息
|
|||
|
|
for line in result.stdout.split('\n'):
|
|||
|
|
if 'app.py' in line:
|
|||
|
|
print(f" 📋 进程: {' '.join(line.split()[10:])}")
|
|||
|
|
return True
|
|||
|
|
else:
|
|||
|
|
print("❌ 服务器未运行")
|
|||
|
|
print("\n💡 启动服务器:")
|
|||
|
|
print(" cd server && python3 app.py")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"⚠️ 无法检查进程: {e}")
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
def check_files():
|
|||
|
|
"""检查关键文件"""
|
|||
|
|
print("\n" + "=" * 60)
|
|||
|
|
print("🔍 检查关键文件...")
|
|||
|
|
print("=" * 60)
|
|||
|
|
|
|||
|
|
files = {
|
|||
|
|
'frontend/login.html': '登录页面',
|
|||
|
|
'frontend/assets/login.css': '登录样式',
|
|||
|
|
'frontend/js/api.js': 'API 接口',
|
|||
|
|
'server/app.py': '后端服务',
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
all_exist = True
|
|||
|
|
for path, desc in files.items():
|
|||
|
|
if os.path.exists(path):
|
|||
|
|
size = os.path.getsize(path)
|
|||
|
|
print(f"✅ {desc:20s} - {path} ({size} bytes)")
|
|||
|
|
else:
|
|||
|
|
print(f"❌ {desc:20s} - {path} (不存在)")
|
|||
|
|
all_exist = False
|
|||
|
|
|
|||
|
|
return all_exist
|
|||
|
|
|
|||
|
|
def show_instructions():
|
|||
|
|
"""显示使用说明"""
|
|||
|
|
print("\n" + "=" * 60)
|
|||
|
|
print("📖 使用说明")
|
|||
|
|
print("=" * 60)
|
|||
|
|
|
|||
|
|
print("\n1️⃣ 启动后端服务:")
|
|||
|
|
print(" cd server")
|
|||
|
|
print(" python3 app.py")
|
|||
|
|
|
|||
|
|
print("\n2️⃣ 访问登录页面:")
|
|||
|
|
print(" http://localhost:5000/login.html")
|
|||
|
|
print(" ⚠️ 注意:不是 login-preview.html")
|
|||
|
|
|
|||
|
|
print("\n3️⃣ 使用以下账号登录:")
|
|||
|
|
print(" - tz (超级管理员)")
|
|||
|
|
print(" - 张正浩 (超级管理员)")
|
|||
|
|
print(" - admin (管理员)")
|
|||
|
|
print(" - 黄有想 (管理员)")
|
|||
|
|
|
|||
|
|
print("\n4️⃣ 如果忘记密码,重置密码:")
|
|||
|
|
print(" python3 reset_password.py <用户名> <新密码>")
|
|||
|
|
|
|||
|
|
print("\n5️⃣ 清除浏览器缓存:")
|
|||
|
|
print(" Chrome/Edge: Ctrl+Shift+Delete")
|
|||
|
|
print(" 或者使用无痕模式: Ctrl+Shift+N")
|
|||
|
|
|
|||
|
|
def create_reset_script():
|
|||
|
|
"""创建密码重置脚本"""
|
|||
|
|
script = '''#!/usr/bin/env python3
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
"""
|
|||
|
|
密码重置脚本
|
|||
|
|
用法: python3 reset_password.py <用户名> <新密码>
|
|||
|
|
"""
|
|||
|
|
import sys
|
|||
|
|
import sqlite3
|
|||
|
|
from werkzeug.security import generate_password_hash
|
|||
|
|
|
|||
|
|
if len(sys.argv) != 3:
|
|||
|
|
print("用法: python3 reset_password.py <用户名> <新密码>")
|
|||
|
|
sys.exit(1)
|
|||
|
|
|
|||
|
|
username = sys.argv[1]
|
|||
|
|
new_password = sys.argv[2]
|
|||
|
|
|
|||
|
|
DB_PATH = 'server/data.db'
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
conn = sqlite3.connect(DB_PATH)
|
|||
|
|
c = conn.cursor()
|
|||
|
|
|
|||
|
|
# 检查用户是否存在
|
|||
|
|
user = c.execute('SELECT id FROM users WHERE username = ?', (username,)).fetchone()
|
|||
|
|
|
|||
|
|
if not user:
|
|||
|
|
print(f"❌ 用户 '{username}' 不存在")
|
|||
|
|
print("\\n现有用户:")
|
|||
|
|
users = c.execute('SELECT username FROM users').fetchall()
|
|||
|
|
for u in users:
|
|||
|
|
print(f" - {u[0]}")
|
|||
|
|
sys.exit(1)
|
|||
|
|
|
|||
|
|
# 更新密码
|
|||
|
|
password_hash = generate_password_hash(new_password)
|
|||
|
|
c.execute('UPDATE users SET password_hash = ? WHERE username = ?', (password_hash, username))
|
|||
|
|
conn.commit()
|
|||
|
|
|
|||
|
|
print(f"✅ 用户 '{username}' 的密码已重置")
|
|||
|
|
print(f" 新密码: {new_password}")
|
|||
|
|
|
|||
|
|
conn.close()
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 错误: {e}")
|
|||
|
|
sys.exit(1)
|
|||
|
|
'''
|
|||
|
|
|
|||
|
|
with open('reset_password.py', 'w', encoding='utf-8') as f:
|
|||
|
|
f.write(script)
|
|||
|
|
|
|||
|
|
os.chmod('reset_password.py', 0o755)
|
|||
|
|
print("\n✅ 已创建密码重置脚本: reset_password.py")
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
print("\n" + "🔐 登录问题诊断工具".center(60, "="))
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
db_ok = check_database()
|
|||
|
|
files_ok = check_files()
|
|||
|
|
server_ok = check_server()
|
|||
|
|
|
|||
|
|
print("\n" + "=" * 60)
|
|||
|
|
print("📊 诊断结果")
|
|||
|
|
print("=" * 60)
|
|||
|
|
|
|||
|
|
if db_ok and files_ok:
|
|||
|
|
print("✅ 数据库和文件都正常")
|
|||
|
|
|
|||
|
|
if server_ok:
|
|||
|
|
print("✅ 服务器正在运行")
|
|||
|
|
print("\n💡 如果仍然无法登录,请尝试:")
|
|||
|
|
print(" 1. 清除浏览器缓存")
|
|||
|
|
print(" 2. 使用无痕模式")
|
|||
|
|
print(" 3. 检查浏览器控制台的错误信息")
|
|||
|
|
print(" 4. 确认访问的是 login.html 而不是 login-preview.html")
|
|||
|
|
else:
|
|||
|
|
print("❌ 服务器未运行,请先启动服务器")
|
|||
|
|
else:
|
|||
|
|
print("❌ 发现问题,请检查上述错误信息")
|
|||
|
|
|
|||
|
|
show_instructions()
|
|||
|
|
create_reset_script()
|
|||
|
|
|
|||
|
|
print("\n" + "=" * 60)
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
if __name__ == '__main__':
|
|||
|
|
main()
|