188 lines
5.2 KiB
Python
188 lines
5.2 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
管理员账号创建工具
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
import getpass
|
|
import re
|
|
from datetime import datetime
|
|
|
|
# 添加项目路径
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
from app import create_app
|
|
from app.models.admin import AdminUser
|
|
from config.database import db
|
|
|
|
|
|
def validate_email(email):
|
|
"""验证邮箱格式"""
|
|
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
|
|
return re.match(pattern, email) is not None
|
|
|
|
|
|
def validate_phone(phone):
|
|
"""验证手机号格式"""
|
|
pattern = r'^1[3-9]\d{9}$'
|
|
return re.match(pattern, phone) is not None
|
|
|
|
|
|
def validate_password(password):
|
|
"""验证密码强度"""
|
|
if len(password) < 6:
|
|
return False, "密码长度至少6位"
|
|
|
|
if not re.search(r'[a-zA-Z]', password):
|
|
return False, "密码必须包含字母"
|
|
|
|
if not re.search(r'\d', password):
|
|
return False, "密码必须包含数字"
|
|
|
|
return True, "密码符合要求"
|
|
|
|
|
|
def create_admin():
|
|
"""创建管理员账号"""
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
print("=" * 50)
|
|
print("🛠️ 太白购物商城 - 管理员账号创建工具")
|
|
print("=" * 50)
|
|
print()
|
|
|
|
# 检查是否已有管理员
|
|
existing_count = AdminUser.query.count()
|
|
if existing_count > 0:
|
|
print(f"⚠️ 当前已有 {existing_count} 个管理员账号")
|
|
confirm = input("是否继续创建新的管理员账号?(y/N): ").strip().lower()
|
|
if confirm != 'y':
|
|
print("❌ 取消创建")
|
|
return
|
|
print()
|
|
|
|
# 输入用户名
|
|
while True:
|
|
username = input("请输入管理员用户名: ").strip()
|
|
if not username:
|
|
print("❌ 用户名不能为空")
|
|
continue
|
|
|
|
if len(username) < 3:
|
|
print("❌ 用户名长度至少3位")
|
|
continue
|
|
|
|
# 检查用户名是否已存在
|
|
if AdminUser.query.filter_by(username=username).first():
|
|
print("❌ 用户名已存在")
|
|
continue
|
|
|
|
break
|
|
|
|
# 输入真实姓名
|
|
real_name = input("请输入真实姓名: ").strip()
|
|
|
|
# 输入邮箱
|
|
while True:
|
|
email = input("请输入邮箱地址: ").strip()
|
|
if not email:
|
|
break
|
|
|
|
if not validate_email(email):
|
|
print("❌ 邮箱格式不正确")
|
|
continue
|
|
|
|
# 检查邮箱是否已存在
|
|
if AdminUser.query.filter_by(email=email).first():
|
|
print("❌ 邮箱已被使用")
|
|
continue
|
|
|
|
break
|
|
|
|
# 输入手机号
|
|
while True:
|
|
phone = input("请输入手机号: ").strip()
|
|
if not phone:
|
|
break
|
|
|
|
if not validate_phone(phone):
|
|
print("❌ 手机号格式不正确")
|
|
continue
|
|
|
|
# 检查手机号是否已存在
|
|
if AdminUser.query.filter_by(phone=phone).first():
|
|
print("❌ 手机号已被使用")
|
|
continue
|
|
|
|
break
|
|
|
|
# 输入密码
|
|
while True:
|
|
password = getpass.getpass("请输入密码: ")
|
|
|
|
is_valid, message = validate_password(password)
|
|
if not is_valid:
|
|
print(f"❌ {message}")
|
|
continue
|
|
|
|
confirm_password = getpass.getpass("请确认密码: ")
|
|
|
|
if password != confirm_password:
|
|
print("❌ 密码不一致,请重新输入")
|
|
continue
|
|
|
|
break
|
|
|
|
print()
|
|
print("=" * 30)
|
|
print("📋 管理员信息确认")
|
|
print("=" * 30)
|
|
print(f"用户名: {username}")
|
|
print(f"真实姓名: {real_name if real_name else '未填写'}")
|
|
print(f"邮箱: {email if email else '未填写'}")
|
|
print(f"手机号: {phone if phone else '未填写'}")
|
|
print(f"创建时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
|
print()
|
|
|
|
confirm = input("确认创建?(y/N): ").strip().lower()
|
|
|
|
if confirm != 'y':
|
|
print("❌ 取消创建")
|
|
return
|
|
|
|
try:
|
|
# 创建管理员
|
|
admin = AdminUser(
|
|
username=username,
|
|
real_name=real_name if real_name else None,
|
|
email=email if email else None,
|
|
phone=phone if phone else None,
|
|
status=1
|
|
)
|
|
admin.set_password(password)
|
|
|
|
db.session.add(admin)
|
|
db.session.commit()
|
|
|
|
print()
|
|
print("✅ 管理员账号创建成功!")
|
|
print("=" * 30)
|
|
print("📌 登录信息")
|
|
print("=" * 30)
|
|
print(f"登录地址: http://localhost:5000/admin/login")
|
|
print(f"用户名: {username}")
|
|
print(f"密码: [已设置]")
|
|
print()
|
|
print("🔐 请妥善保管登录信息")
|
|
|
|
except Exception as e:
|
|
print(f"❌ 创建失败: {str(e)}")
|
|
db.session.rollback()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
create_admin()
|