76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
"""创建超级管理员账号。
|
||
|
||
使用方式:
|
||
cd D:/work/code/python/coding/baodanagent
|
||
python scripts/create_admin.py
|
||
|
||
默认账号:admin / Admin@123456
|
||
"""
|
||
import sys
|
||
import os
|
||
|
||
# 添加 api 目录到 Python 路径
|
||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'api'))
|
||
|
||
# 加载环境变量
|
||
from dotenv import load_dotenv
|
||
load_dotenv(os.path.join(os.path.dirname(__file__), '..', '.env'))
|
||
|
||
|
||
def create_admin():
|
||
"""创建超级管理员账号。"""
|
||
try:
|
||
from insurance.app import create_app
|
||
from insurance.db.compat import db
|
||
from insurance.models.wecom_user import WeComUserMapping
|
||
import bcrypt
|
||
import uuid
|
||
|
||
app = create_app()
|
||
|
||
with app.app_context():
|
||
username = "admin"
|
||
password = "Admin@123456"
|
||
|
||
# 检查是否已存在
|
||
existing = db.session.query(WeComUserMapping).filter_by(username=username).first()
|
||
if existing:
|
||
print(f"用户 {username} 已存在,跳过创建")
|
||
print(f" 角色: {existing.role}")
|
||
print(f" 状态: {existing.status}")
|
||
return
|
||
|
||
# 创建用户
|
||
password_hash = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
|
||
mapping = WeComUserMapping(
|
||
wecom_userid=f"local_{uuid.uuid4().hex[:8]}",
|
||
internal_user_id=f"user_{uuid.uuid4().hex[:8]}",
|
||
username=username,
|
||
password_hash=password_hash,
|
||
role="super_admin",
|
||
department="管理组",
|
||
status="active",
|
||
)
|
||
db.session.add(mapping)
|
||
db.session.commit()
|
||
|
||
print("=" * 50)
|
||
print("超级管理员创建成功!")
|
||
print("=" * 50)
|
||
print(f" 用户名: {username}")
|
||
print(f" 密码: {password}")
|
||
print(f" 角色: super_admin")
|
||
print("=" * 50)
|
||
print("登录后即可看到管理后台菜单。")
|
||
|
||
except Exception as e:
|
||
print(f"创建失败: {e}")
|
||
print(f"\n请确保:")
|
||
print(f"1. 数据库已启动并创建")
|
||
print(f"2. .env 文件中 DATABASE_URL 已配置")
|
||
print(f"3. 已安装所需依赖 (pip install -r requirements.txt)")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
create_admin()
|