user.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from data import MySQLTool
  2. import hashlib
  3. import uuid
  4. import TokenUtils
  5. from returnTemp import get_template, get_error_message
  6. class User:
  7. def __init__(self, action, data, logger):
  8. self.action = action
  9. self.data = data
  10. self.logger = logger
  11. def user_router(self):
  12. if self.action == "register":
  13. return self.register()
  14. elif self.action == "login":
  15. return self.login()
  16. else:
  17. return self.test()
  18. def register(self):
  19. print("---register---")
  20. return register(self.data)
  21. def login(self):
  22. print("---login----")
  23. return login(self.data)
  24. def test(self):
  25. print("---test----")
  26. return test(self.data)
  27. def test(data):
  28. token = data["token"]
  29. uid = data["uid"]
  30. newtoken = TokenUtils.generate_jwt_token(uid)
  31. newt = TokenUtils.verify_jwt_token(uid, newtoken)
  32. return {"result": newt}
  33. def register(data):
  34. login_type = data["login_type"]
  35. print(f"登录类型:{login_type}")
  36. if login_type == "1":
  37. return accountRegister(data)
  38. elif login_type == "2":
  39. print("222")
  40. else:
  41. print("3333")
  42. def accountRegister(data):
  43. print("account login")
  44. keys_to_check = ['login_type', 'account', 'password']
  45. if all(key in data for key in keys_to_check):
  46. # account
  47. login_type = data["login_type"]
  48. account = data["account"]
  49. password = data["password"]
  50. if login_type == "" or account == "" or password == "":
  51. # 三个值必须存在
  52. template = get_template(2000, get_error_message(2000), {})
  53. return template
  54. hashed_password = hashlib.md5(password.encode()).hexdigest()
  55. print(hashed_password)
  56. # 优先查询是否已经注册过
  57. mysql_tool.connect()
  58. result = mysql_tool.select(table='user', columns='*', conditions={'type': login_type, 'username': account})
  59. if len(result):
  60. mysql_tool.disconnect()
  61. return get_template(1003, get_error_message(1003), {})
  62. else:
  63. # 随机生成uid
  64. unique_id = uuid.uuid4()
  65. # 将 UUID 转换为字符串
  66. unique_id_str = str(unique_id)
  67. columns = ['type', 'username', 'uid', 'token', 'phone', 'credential']
  68. values = [1, account, unique_id_str, "", "", str(hashed_password)]
  69. insert_result = mysql_tool.insert(table='user', columns=columns, values=values)
  70. print(insert_result)
  71. mysql_tool.disconnect()
  72. return get_template(0, "注册成功", {})
  73. else:
  74. template = get_template(2000, get_error_message(2000), {})
  75. return template
  76. def accountLogin(data):
  77. keys_to_check = ['login_type', 'account', 'password']
  78. if all(key in data for key in keys_to_check):
  79. login_type = data["login_type"]
  80. account = data["account"]
  81. password = data["password"]
  82. if login_type == "" or account == "" or password == "":
  83. # 三个值必须存在
  84. template = get_template(2000, get_error_message(2000), {})
  85. return template
  86. else:
  87. # 开始登录
  88. print("开始登录")
  89. mysql_tool.connect()
  90. result = mysql_tool.select(table='user', columns='*', conditions={'type': login_type, 'username': account})
  91. if len(result):
  92. first_row = result[0]
  93. # 获取第一行数据中的'username'值
  94. login_type = first_row[1]
  95. username = first_row[2]
  96. uid = first_row[3]
  97. token = TokenUtils.generate_jwt_token(uid)
  98. credential = first_row[6]
  99. # 计算原始密码是否=mysql密码
  100. if credential == hashlib.md5(password.encode()).hexdigest():
  101. # 更新数据库
  102. set_values = {'token': token}
  103. conditions = {'uid': uid}
  104. affected_rows = mysql_tool.update(table='user', set_values=set_values, conditions=conditions)
  105. print(f"Updated {affected_rows} rows")
  106. # 生成token
  107. return get_template(0, get_error_message(0), {
  108. "login_type": login_type,
  109. "username": username,
  110. "uid": uid,
  111. "token": token,
  112. "credential": credential
  113. })
  114. else:
  115. template = get_template(1005, get_error_message(1005), {})
  116. return template
  117. else:
  118. mysql_tool.disconnect()
  119. return get_template(1004, get_error_message(1004), {})
  120. else:
  121. template = get_template(2004, get_error_message(2004), {})
  122. return template
  123. mysql_tool = MySQLTool(host='localhost', user='root', password='wyt615115@', database='apiserver')
  124. def login(data):
  125. logintype = data["login_type"]
  126. if logintype == "1":
  127. return accountLogin(data)
  128. elif logintype == "2":
  129. print("222")
  130. else:
  131. print("3333")