from data import MySQLTool import hashlib import uuid import TokenUtils from returnTemp import get_template, get_error_message class User: def __init__(self, action, data, logger): self.action = action self.data = data self.logger = logger def user_router(self): if self.action == "register": return self.register() elif self.action == "login": return self.login() else: return self.test() def register(self): print("---register---") return register(self.data) def login(self): print("---login----") return login(self.data) def test(self): print("---test----") return test(self.data) def test(data): token = data["token"] uid = data["uid"] newtoken = TokenUtils.generate_jwt_token(uid) newt = TokenUtils.verify_jwt_token(uid, newtoken) return {"result": newt} def register(data): login_type = data["login_type"] print(f"登录类型:{login_type}") if login_type == "1": return accountRegister(data) elif login_type == "2": print("222") return wechatRegister(data) else: print("3333") def wechatRegister(data): return data def accountRegister(data): print("account login") keys_to_check = ['login_type', 'account', 'password'] if all(key in data for key in keys_to_check): # account login_type = data["login_type"] account = data["account"] password = data["password"] if login_type == "" or account == "" or password == "": # 三个值必须存在 template = get_template(2000, get_error_message(2000), {}) return template hashed_password = hashlib.md5(password.encode()).hexdigest() print(hashed_password) # 优先查询是否已经注册过 mysql_tool.connect() result = mysql_tool.select(table='user', columns='*', conditions={'type': login_type, 'username': account}) if len(result): mysql_tool.disconnect() return get_template(1003, get_error_message(1003), {}) else: # 随机生成uid unique_id = uuid.uuid4() # 将 UUID 转换为字符串 unique_id_str = str(unique_id) columns = ['type', 'username', 'uid', 'token', 'phone', 'credential'] values = [1, account, unique_id_str, "", "", str(hashed_password)] insert_result = mysql_tool.insert(table='user', columns=columns, values=values) print(insert_result) mysql_tool.disconnect() return get_template(0, "注册成功", {}) else: template = get_template(2000, get_error_message(2000), {}) return template def accountLogin(data): keys_to_check = ['login_type', 'account', 'password'] if all(key in data for key in keys_to_check): login_type = data["login_type"] account = data["account"] password = data["password"] if login_type == "" or account == "" or password == "": # 三个值必须存在 template = get_template(2000, get_error_message(2000), {}) return template else: # 开始登录 print("开始登录") mysql_tool.connect() result = mysql_tool.select(table='user', columns='*', conditions={'type': login_type, 'username': account}) if len(result): first_row = result[0] # 获取第一行数据中的'username'值 login_type = first_row[1] username = first_row[2] uid = first_row[3] token = TokenUtils.generate_jwt_token(uid) credential = first_row[6] # 计算原始密码是否=mysql密码 if credential == hashlib.md5(password.encode()).hexdigest(): # 更新数据库 set_values = {'token': token} conditions = {'uid': uid} affected_rows = mysql_tool.update(table='user', set_values=set_values, conditions=conditions) print(f"Updated {affected_rows} rows") # 生成token return get_template(0, get_error_message(0), { "login_type": login_type, "username": username, "uid": uid, "token": token, "credential": credential }) else: template = get_template(1005, get_error_message(1005), {}) return template else: mysql_tool.disconnect() return get_template(1004, get_error_message(1004), {}) else: template = get_template(2004, get_error_message(2004), {}) return template def wechatLogin(data): print("wechat data") return data mysql_tool = MySQLTool(host='localhost', user='root', password='wyt615115@', database='apiserver') def login(data): logintype = data["login_type"] if logintype == "1": return accountLogin(data) elif logintype == "2": return wechatLogin(data) else: print("3333")