302 lines
8.6 KiB
Python
302 lines
8.6 KiB
Python
|
|
#!/usr/bin/env python
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
"""
|
|||
|
|
清空用户微信绑定脚本
|
|||
|
|
用于将ai_users表中指定用户的wechat_openid字段清空
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import sys
|
|||
|
|
from database_config import db_manager
|
|||
|
|
|
|||
|
|
def clear_wechat_binding(user_identifier):
|
|||
|
|
"""
|
|||
|
|
清空用户的微信绑定
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
user_identifier: 用户标识,可以是用户ID、用户名或手机号
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
bool: 操作是否成功
|
|||
|
|
"""
|
|||
|
|
conn = None
|
|||
|
|
cursor = None
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
conn = db_manager.get_connection()
|
|||
|
|
cursor = conn.cursor()
|
|||
|
|
|
|||
|
|
# 首先查询用户信息
|
|||
|
|
query_sql = """
|
|||
|
|
SELECT id, username, real_name, phone, wechat_openid
|
|||
|
|
FROM ai_users
|
|||
|
|
WHERE id = %s OR username = %s OR phone = %s
|
|||
|
|
"""
|
|||
|
|
cursor.execute(query_sql, (user_identifier, user_identifier, user_identifier))
|
|||
|
|
user = cursor.fetchone()
|
|||
|
|
|
|||
|
|
if not user:
|
|||
|
|
print(f"❌ 未找到用户: {user_identifier}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
# 从字典中获取值
|
|||
|
|
user_id = user['id']
|
|||
|
|
username = user['username']
|
|||
|
|
real_name = user['real_name']
|
|||
|
|
phone = user['phone']
|
|||
|
|
wechat_openid = user['wechat_openid']
|
|||
|
|
|
|||
|
|
# 显示用户信息
|
|||
|
|
print("\n" + "="*60)
|
|||
|
|
print("📋 找到用户信息:")
|
|||
|
|
print("="*60)
|
|||
|
|
print(f" 用户ID: {user_id}")
|
|||
|
|
print(f" 用户名: {username}")
|
|||
|
|
print(f" 真实姓名: {real_name or '未设置'}")
|
|||
|
|
print(f" 手机号: {phone or '未设置'}")
|
|||
|
|
print(f" 微信OpenID: {wechat_openid or '未绑定'}")
|
|||
|
|
print("="*60)
|
|||
|
|
|
|||
|
|
# 如果已经没有绑定,直接返回
|
|||
|
|
if not wechat_openid:
|
|||
|
|
print("\n✅ 该用户未绑定微信,无需清空")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
# 确认操作
|
|||
|
|
confirm = input(f"\n⚠️ 确认要清空用户 [{username}] 的微信绑定吗?(yes/no): ")
|
|||
|
|
if confirm.lower() not in ['yes', 'y', '是']:
|
|||
|
|
print("\n❌ 操作已取消")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
# 清空微信绑定
|
|||
|
|
update_sql = """
|
|||
|
|
UPDATE ai_users
|
|||
|
|
SET wechat_openid = NULL,
|
|||
|
|
updated_at = NOW()
|
|||
|
|
WHERE id = %s
|
|||
|
|
"""
|
|||
|
|
cursor.execute(update_sql, (user_id,))
|
|||
|
|
conn.commit()
|
|||
|
|
|
|||
|
|
print("\n" + "="*60)
|
|||
|
|
print("✅ 微信绑定已成功清空!")
|
|||
|
|
print("="*60)
|
|||
|
|
print(f" 用户ID: {user_id}")
|
|||
|
|
print(f" 用户名: {username}")
|
|||
|
|
print(f" 操作: wechat_openid 已设置为 NULL")
|
|||
|
|
print("="*60)
|
|||
|
|
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
if conn:
|
|||
|
|
conn.rollback()
|
|||
|
|
print(f"\n❌ 操作失败: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
finally:
|
|||
|
|
if cursor:
|
|||
|
|
cursor.close()
|
|||
|
|
if conn:
|
|||
|
|
conn.close()
|
|||
|
|
|
|||
|
|
|
|||
|
|
def clear_wechat_binding_batch(user_identifiers):
|
|||
|
|
"""
|
|||
|
|
批量清空多个用户的微信绑定
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
user_identifiers: 用户标识列表
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
tuple: (成功数量, 失败数量)
|
|||
|
|
"""
|
|||
|
|
success_count = 0
|
|||
|
|
fail_count = 0
|
|||
|
|
|
|||
|
|
print("\n" + "█"*60)
|
|||
|
|
print(f"🔄 开始批量清空微信绑定 - 共 {len(user_identifiers)} 个用户")
|
|||
|
|
print("█"*60)
|
|||
|
|
|
|||
|
|
for identifier in user_identifiers:
|
|||
|
|
print(f"\n处理用户: {identifier}")
|
|||
|
|
if clear_wechat_binding_silent(identifier):
|
|||
|
|
success_count += 1
|
|||
|
|
else:
|
|||
|
|
fail_count += 1
|
|||
|
|
|
|||
|
|
print("\n" + "█"*60)
|
|||
|
|
print("📊 批量操作完成")
|
|||
|
|
print("█"*60)
|
|||
|
|
print(f" ✅ 成功: {success_count} 个")
|
|||
|
|
print(f" ❌ 失败: {fail_count} 个")
|
|||
|
|
print("█"*60)
|
|||
|
|
|
|||
|
|
return success_count, fail_count
|
|||
|
|
|
|||
|
|
|
|||
|
|
def clear_wechat_binding_silent(user_identifier):
|
|||
|
|
"""
|
|||
|
|
静默模式清空用户的微信绑定(无需确认)
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
user_identifier: 用户标识
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
bool: 操作是否成功
|
|||
|
|
"""
|
|||
|
|
conn = None
|
|||
|
|
cursor = None
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
conn = db_manager.get_connection()
|
|||
|
|
cursor = conn.cursor()
|
|||
|
|
|
|||
|
|
# 查询用户
|
|||
|
|
query_sql = """
|
|||
|
|
SELECT id, username, wechat_openid
|
|||
|
|
FROM ai_users
|
|||
|
|
WHERE id = %s OR username = %s OR phone = %s
|
|||
|
|
"""
|
|||
|
|
cursor.execute(query_sql, (user_identifier, user_identifier, user_identifier))
|
|||
|
|
user = cursor.fetchone()
|
|||
|
|
|
|||
|
|
if not user:
|
|||
|
|
print(f" ❌ 未找到用户")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
# 从字典中获取值
|
|||
|
|
user_id = user['id']
|
|||
|
|
username = user['username']
|
|||
|
|
wechat_openid = user['wechat_openid']
|
|||
|
|
|
|||
|
|
if not wechat_openid:
|
|||
|
|
print(f" ℹ️ 用户 [{username}] 未绑定微信")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
# 清空微信绑定
|
|||
|
|
update_sql = """
|
|||
|
|
UPDATE ai_users
|
|||
|
|
SET wechat_openid = NULL,
|
|||
|
|
updated_at = NOW()
|
|||
|
|
WHERE id = %s
|
|||
|
|
"""
|
|||
|
|
cursor.execute(update_sql, (user_id,))
|
|||
|
|
conn.commit()
|
|||
|
|
|
|||
|
|
print(f" ✅ 用户 [{username}] 微信绑定已清空")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
if conn:
|
|||
|
|
conn.rollback()
|
|||
|
|
print(f" ❌ 操作失败: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
finally:
|
|||
|
|
if cursor:
|
|||
|
|
cursor.close()
|
|||
|
|
if conn:
|
|||
|
|
conn.close()
|
|||
|
|
|
|||
|
|
|
|||
|
|
def list_users_with_wechat():
|
|||
|
|
"""
|
|||
|
|
列出所有已绑定微信的用户
|
|||
|
|
"""
|
|||
|
|
conn = None
|
|||
|
|
cursor = None
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
conn = db_manager.get_connection()
|
|||
|
|
cursor = conn.cursor()
|
|||
|
|
|
|||
|
|
query_sql = """
|
|||
|
|
SELECT id, username, real_name, phone, enterprise_name, wechat_openid
|
|||
|
|
FROM ai_users
|
|||
|
|
WHERE wechat_openid IS NOT NULL AND wechat_openid != ''
|
|||
|
|
ORDER BY id
|
|||
|
|
"""
|
|||
|
|
cursor.execute(query_sql)
|
|||
|
|
users = cursor.fetchall()
|
|||
|
|
|
|||
|
|
if not users:
|
|||
|
|
print("\n📋 没有已绑定微信的用户")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
print("\n" + "="*80)
|
|||
|
|
print(f"📋 已绑定微信的用户列表 - 共 {len(users)} 个")
|
|||
|
|
print("="*80)
|
|||
|
|
print(f"{'ID':<6} {'用户名':<15} {'真实姓名':<12} {'手机号':<15} {'企业':<20} OpenID")
|
|||
|
|
print("-"*80)
|
|||
|
|
|
|||
|
|
for user in users:
|
|||
|
|
user_id = user['id']
|
|||
|
|
username = user['username']
|
|||
|
|
real_name = user['real_name']
|
|||
|
|
phone = user['phone']
|
|||
|
|
enterprise_name = user['enterprise_name']
|
|||
|
|
openid = user['wechat_openid']
|
|||
|
|
openid_display = openid[:20] + '...' if openid and len(openid) > 20 else (openid or '')
|
|||
|
|
print(f"{user_id:<6} {username:<15} {real_name or '未设置':<12} {phone or '未设置':<15} {(enterprise_name[:18] if enterprise_name else ''):<20} {openid_display}")
|
|||
|
|
|
|||
|
|
print("="*80)
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"\n❌ 查询失败: {e}")
|
|||
|
|
|
|||
|
|
finally:
|
|||
|
|
if cursor:
|
|||
|
|
cursor.close()
|
|||
|
|
if conn:
|
|||
|
|
conn.close()
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
"""主函数"""
|
|||
|
|
if len(sys.argv) < 2:
|
|||
|
|
print("\n" + "="*60)
|
|||
|
|
print("📖 使用说明:")
|
|||
|
|
print("="*60)
|
|||
|
|
print("1. 清空单个用户的微信绑定:")
|
|||
|
|
print(" python 清空用户微信绑定.py <用户ID|用户名|手机号>")
|
|||
|
|
print("\n2. 批量清空多个用户的微信绑定:")
|
|||
|
|
print(" python 清空用户微信绑定.py <用户1> <用户2> <用户3> ...")
|
|||
|
|
print("\n3. 列出所有已绑定微信的用户:")
|
|||
|
|
print(" python 清空用户微信绑定.py --list")
|
|||
|
|
print("="*60)
|
|||
|
|
print("\n示例:")
|
|||
|
|
print(" python 清空用户微信绑定.py 1")
|
|||
|
|
print(" python 清空用户微信绑定.py user001")
|
|||
|
|
print(" python 清空用户微信绑定.py 13800000001")
|
|||
|
|
print(" python 清空用户微信绑定.py 1 2 3")
|
|||
|
|
print(" python 清空用户微信绑定.py --list")
|
|||
|
|
print("="*60)
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 列出已绑定用户
|
|||
|
|
if sys.argv[1] == '--list':
|
|||
|
|
list_users_with_wechat()
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 单个用户
|
|||
|
|
if len(sys.argv) == 2:
|
|||
|
|
clear_wechat_binding(sys.argv[1])
|
|||
|
|
# 批量用户
|
|||
|
|
else:
|
|||
|
|
clear_wechat_binding_batch(sys.argv[1:])
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
try:
|
|||
|
|
main()
|
|||
|
|
except KeyboardInterrupt:
|
|||
|
|
print("\n\n❌ 操作被用户中断")
|
|||
|
|
sys.exit(1)
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"\n❌ 程序异常: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
sys.exit(1)
|