user.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. return wechatRegister(data)
  41. else:
  42. print("3333")
  43. def wechatRegister(data):
  44. return data
  45. def accountRegister(data):
  46. print("account login")
  47. keys_to_check = ['login_type', 'account', 'password']
  48. if all(key in data for key in keys_to_check):
  49. # account
  50. login_type = data["login_type"]
  51. account = data["account"]
  52. password = data["password"]
  53. if login_type == "" or account == "" or password == "":
  54. # 三个值必须存在
  55. template = get_template(2000, get_error_message(2000), {})
  56. return template
  57. hashed_password = hashlib.md5(password.encode()).hexdigest()
  58. print(hashed_password)
  59. # 优先查询是否已经注册过
  60. mysql_tool.connect()
  61. result = mysql_tool.select(table='user', columns='*', conditions={'type': login_type, 'username': account})
  62. if len(result):
  63. mysql_tool.disconnect()
  64. return get_template(1003, get_error_message(1003), {})
  65. else:
  66. # 随机生成uid
  67. unique_id = uuid.uuid4()
  68. # 将 UUID 转换为字符串
  69. unique_id_str = str(unique_id)
  70. columns = ['type', 'username', 'uid', 'token', 'phone', 'credential']
  71. values = [1, account, unique_id_str, "", "", str(hashed_password)]
  72. insert_result = mysql_tool.insert(table='user', columns=columns, values=values)
  73. print(insert_result)
  74. mysql_tool.disconnect()
  75. return get_template(0, "注册成功", {})
  76. else:
  77. template = get_template(2000, get_error_message(2000), {})
  78. return template
  79. def accountLogin(data):
  80. keys_to_check = ['login_type', 'account', 'password']
  81. if all(key in data for key in keys_to_check):
  82. login_type = data["login_type"]
  83. account = data["account"]
  84. password = data["password"]
  85. if login_type == "" or account == "" or password == "":
  86. # 三个值必须存在
  87. template = get_template(2000, get_error_message(2000), {})
  88. return template
  89. else:
  90. # 开始登录
  91. print("开始登录")
  92. mysql_tool.connect()
  93. result = mysql_tool.select(table='user', columns='*', conditions={'type': login_type, 'username': account})
  94. if len(result):
  95. first_row = result[0]
  96. # 获取第一行数据中的'username'值
  97. login_type = first_row[1]
  98. username = first_row[2]
  99. uid = first_row[3]
  100. token = TokenUtils.generate_jwt_token(uid)
  101. credential = first_row[6]
  102. # 计算原始密码是否=mysql密码
  103. if credential == hashlib.md5(password.encode()).hexdigest():
  104. # 更新数据库
  105. set_values = {'token': token}
  106. conditions = {'uid': uid}
  107. affected_rows = mysql_tool.update(table='user', set_values=set_values, conditions=conditions)
  108. print(f"Updated {affected_rows} rows")
  109. # 生成token
  110. return get_template(0, get_error_message(0), {
  111. "login_type": login_type,
  112. "username": username,
  113. "uid": uid,
  114. "token": token,
  115. "credential": credential
  116. })
  117. else:
  118. template = get_template(1005, get_error_message(1005), {})
  119. return template
  120. else:
  121. mysql_tool.disconnect()
  122. return get_template(1004, get_error_message(1004), {})
  123. else:
  124. template = get_template(2004, get_error_message(2004), {})
  125. return template
  126. def wechatLogin(data):
  127. print("wechat data")
  128. return data
  129. mysql_tool = MySQLTool(host='localhost', user='root', password='wyt615115@', database='apiserver')
  130. def login(data):
  131. logintype = data["login_type"]
  132. if logintype == "1":
  133. print("111")
  134. return accountLogin(data)
  135. elif logintype == "2":
  136. print("222")
  137. return wechatLogin(data)
  138. else:
  139. print("3333")