""" 客户端工具函数 """ import json import os from datetime import datetime, timezone, timedelta def beijing_now_str(fmt='%Y-%m-%d %H:%M:%S'): """返回北京时间字符串""" return datetime.now(timezone(timedelta(hours=8))).strftime(fmt) def validate_username(username): """验证用户名""" if not username or len(username) < 4 or len(username) > 20: return False, "用户名长度必须为4-20个字符" if not username.replace('_', '').isalnum(): return False, "用户名只能包含字母、数字和下划线" return True, "" def validate_password(password): """验证密码""" if not password or len(password) < 6 or len(password) > 20: return False, "密码长度必须为6-20个字符" return True, "" def create_default_config(): """创建默认配置文件(仅在不存在时创建)""" if os.path.exists('config.json'): return default_config = { 'server_host': '127.0.0.1', 'server_port': 8888, 'auto_login': False, 'username': '', 'save_password': False, 'theme': 'light', 'font_size': 12, 'notify_new_message': True, 'notify_sound': True } with open('config.json', 'w', encoding='utf-8') as f: json.dump(default_config, f, ensure_ascii=False, indent=2)