鲁浩 10 månader sedan
förälder
incheckning
8674fd3de8

+ 8 - 0
server/flaskProject/.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
+# Editor-based HTTP Client requests
+/httpRequests/

+ 19 - 0
server/flaskProject/.idea/flaskProject.iml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="PYTHON_MODULE" version="4">
+  <component name="Flask">
+    <option name="enabled" value="true" />
+  </component>
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+  <component name="TemplatesService">
+    <option name="TEMPLATE_CONFIGURATION" value="Jinja2" />
+    <option name="TEMPLATE_FOLDERS">
+      <list>
+        <option value="$MODULE_DIR$/templates" />
+      </list>
+    </option>
+  </component>
+</module>

+ 29 - 0
server/flaskProject/.idea/inspectionProfiles/Project_Default.xml

@@ -0,0 +1,29 @@
+<component name="InspectionProjectProfileManager">
+  <profile version="1.0">
+    <option name="myName" value="Project Default" />
+    <inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
+      <Languages>
+        <language minSize="188" name="Python" />
+      </Languages>
+    </inspection_tool>
+    <inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
+      <option name="ignoredPackages">
+        <value>
+          <list size="2">
+            <item index="0" class="java.lang.String" itemvalue="wxpy" />
+            <item index="1" class="java.lang.String" itemvalue="Flask" />
+          </list>
+        </value>
+      </option>
+    </inspection_tool>
+    <inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
+      <option name="ignoredErrors">
+        <list>
+          <option value="N806" />
+          <option value="N802" />
+          <option value="N803" />
+        </list>
+      </option>
+    </inspection_tool>
+  </profile>
+</component>

+ 6 - 0
server/flaskProject/.idea/inspectionProfiles/profiles_settings.xml

@@ -0,0 +1,6 @@
+<component name="InspectionProjectProfileManager">
+  <settings>
+    <option name="USE_PROJECT_PROFILE" value="false" />
+    <version value="1.0" />
+  </settings>
+</component>

+ 8 - 0
server/flaskProject/.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/flaskProject.iml" filepath="$PROJECT_DIR$/.idea/flaskProject.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
server/flaskProject/.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
+  </component>
+</project>

+ 33 - 0
server/flaskProject/TokenUtils.py

@@ -0,0 +1,33 @@
+import time
+import jwt
+
+JWT_TOKEN_EXPIRE_SECONDS = 3600 * 24 * 7  # token有效时间 7天
+JWT_TOKEN_SECRET_SALT = 'jasonphd.2024.ttb'
+JWT_TOKEN_ALGORITHM = 'HS256'  # HASH算法
+
+
+def generate_jwt_token(user: str) -> str:
+    """根据用户id生成token"""
+    data = {'user_id': user, 'exp': int(time.time()) + JWT_TOKEN_EXPIRE_SECONDS}
+    print("generate data:", data)
+    jwtToken = jwt.encode(data, JWT_TOKEN_SECRET_SALT, algorithm=JWT_TOKEN_ALGORITHM)
+    return jwtToken
+
+
+def verify_jwt_token(user: str, jwtToken: str) -> bool:
+    """验证用户token"""
+    data = {'user_id': user}
+    try:
+        payload = jwt.decode(jwtToken, JWT_TOKEN_SECRET_SALT, algorithms=[JWT_TOKEN_ALGORITHM])
+        print("verify:", payload)
+        exp = int(payload.pop('exp'))
+        if time.time() > exp:
+            print('已失效')
+            return False
+        return data == payload
+    except jwt.exceptions.ExpiredSignatureError as ex:
+        print('token签名过期:', ex)
+    except jwt.PyJWTError as ex:
+        print('token解析失败:', ex)
+
+    return False

BIN
server/flaskProject/__pycache__/data.cpython-311.pyc


BIN
server/flaskProject/__pycache__/user.cpython-311.pyc


+ 6 - 6
server/flaskProject/app.py

@@ -1,12 +1,12 @@
-from flask import Flask,request
+from flask import Flask, request
 from user import User
 import logging
 from logging.handlers import TimedRotatingFileHandler
 from datetime import datetime
 
-
 app = Flask(__name__)
 
+
 @app.route('/')
 def hello_world():  # put application's code here
     return 'it works!'
@@ -15,7 +15,7 @@ def hello_world():  # put application's code here
 @app.route('/user/<action>', methods=['POST'])
 def user_action(action):
     data = request.get_json()
-    r = User(action=action, data=data,logger=logger).user_router()
+    r = User(action=action, data=data, logger=logger).user_router()
     return r
 
 
@@ -42,6 +42,7 @@ class CustomLogger:
     def log(self, message):
         self.logger.info(message)
 
+
 # 使用自定义的 logging 工具类
 # 获取当前日期
 current_date = datetime.now()
@@ -53,15 +54,14 @@ day = current_date.day
 
 # 将年月日格式化为字符串
 formatted_date = current_date.strftime("%Y-%m-%d")
-logger = CustomLogger(f'logs/{formatted_date}.log')
+logger = CustomLogger(f'/Users/jasonphd/Desktop/gogs/JasonPhdWork/server/flaskProject/logs/{formatted_date}.log')
 
 
 # 自定义中间件
 @app.before_request
 def log_request_info():
     logger.log(f"url:{request.url} params:{request.get_json()} method:{request.method} ip:{request.remote_addr}")
-    
 
-    
+
 if __name__ == '__main__':
     app.run()

+ 67 - 0
server/flaskProject/data.py

@@ -0,0 +1,67 @@
+import mysql.connector
+
+
+class MySQLTool:
+    def __init__(self, host, user, password, database):
+        self.host = host
+        self.user = user
+        self.password = password
+        self.database = database
+        self.connection = None
+        self.cursor = None
+
+    def connect(self):
+        self.connection = mysql.connector.connect(
+            host=self.host,
+            user=self.user,
+            password=self.password,
+            database=self.database
+        )
+        self.cursor = self.connection.cursor()
+
+    def disconnect(self):
+        if self.cursor:
+            self.cursor.close()
+        if self.connection:
+            self.connection.close()
+
+    def select(self, table, columns='*', conditions=None):
+        query = f"SELECT {columns} FROM {table}"
+        if conditions:
+            query += " WHERE " + " AND ".join([f"{key} = %s" for key in conditions.keys()])
+            values = tuple(conditions.values())
+            self.cursor.execute(query, values)
+        else:
+            self.cursor.execute(query)
+        result = self.cursor.fetchall()
+        return result
+
+    def insert(self, table, columns, values):
+        query = f"INSERT INTO {table} ({', '.join(columns)}) VALUES ({', '.join(['%s' for _ in columns])})"
+        self.cursor.execute(query, values)
+        self.connection.commit()
+        affected_rows = self.cursor.rowcount
+        return affected_rows
+
+    def update(self, table, set_values, conditions=None):
+        query = f"UPDATE {table} SET " + ", ".join([f"{key} = %s" for key in set_values.keys()])
+        values = tuple(set_values.values())
+        if conditions:
+            query += " WHERE " + " AND ".join([f"{key} = %s" for key in conditions.keys()])
+            values += tuple(conditions.values())
+        self.cursor.execute(query, values)
+        self.connection.commit()
+        affected_rows = self.cursor.rowcount
+        return affected_rows
+
+    def delete(self, table, conditions=None):
+        query = f"DELETE FROM {table}"
+        if conditions:
+            query += " WHERE " + " AND ".join([f"{key} = %s" for key in conditions.keys()])
+            values = tuple(conditions.values())
+            self.cursor.execute(query, values)
+        else:
+            self.cursor.execute(query)
+        self.connection.commit()
+        affected_rows = self.cursor.rowcount
+        return affected_rows

+ 6 - 0
server/flaskProject/logs/2024-02-20.log

@@ -0,0 +1,6 @@
+2024-02-20 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-20 INFO: 😂
+2024-02-20 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-20 INFO: 😂
+2024-02-20 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-20 INFO: 😂

+ 15 - 0
server/flaskProject/logs/2024-02-21.log

@@ -0,0 +1,15 @@
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'uuid': '', 'token': 'xxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'aaaa', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'aaaa', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-21 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'aaaa', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1

+ 42 - 0
server/flaskProject/logs/2024-02-22.log

@@ -0,0 +1,42 @@
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'aaaa', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'aaaa', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'aaaa', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'aaaa', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'aaaa', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'aaaa', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'aaaa', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'aaaa', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'aaaa', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'aaaa', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'John Doe', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'John Doe', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'John Doe', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'John Doe', 'password': 'bbbb', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'luhao', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'luhao', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'luhao', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'luhao', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'luhao', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'luhao', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'luhao', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'luhao', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'luhao', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'luhao', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'luhao', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'luhao', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'ss', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-22 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 's', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1

+ 39 - 0
server/flaskProject/logs/2024-02-23.log

@@ -0,0 +1,39 @@
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 's', 'password': '11111111', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': '测试', 'password': '这是一个密码吗', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': '测试', 'password': '这是一个密码吗', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': '测试', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'password': '', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'password': '1', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'password': '1', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'password': '1', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'password': '1', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'password': '1', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'password': '1', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'password': '1', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'password': '1', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'password': '1', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'password': '1', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': '测试', 'password': '1', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'jasonphd', 'password': 'jasonphd', 'uuid': 'jasonphd11111'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': 'jasonphd', 'password': 'jasonphd', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': 'jasonphd', 'password': 'jasonphd1', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': 'jasonphd', 'password': 'jasonphd', 'uuid': 'xxxxxxx'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/t params:{'token': '04e0fb949ef833661d8f371ac02c9b738e3fb44b2cd657a3bfed96e17e7cc419', 'uid': '062e8915-8b74-4f7c-bfa7-a62fa6d9d108'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/t params:{'token': '04e0fb949ef833661d8f371ac02c9b738e3fb44b2cd657a3bfed96e17e7cc419', 'uid': '062e8915-8b74-4f7c-bfa7-a62fa6d9d108'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/t params:{'token': '04e0fb949ef833661d8f371ac02c9b738e3fb44b2cd657a3bfed96e17e7cc419', 'uid': '062e8915-8b74-4f7c-bfa7-a62fa6d9d108'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/t params:{'token': '04e0fb949ef833661d8f371ac02c9b738e3fb44b2cd657a3bfed96e17e7cc419', 'uid': '062e8915-8b74-4f7c-bfa7-a62fa6d9d108'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/t params:{'token': '04e0fb949ef833661d8f371ac02c9b738e3fb44b2cd657a3bfed96e17e7cc419', 'uid': '062e8915-8b74-4f7c-bfa7-a62fa6d9d108'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/t params:{'token': '04e0fb949ef833661d8f371ac02c9b738e3fb44b2cd657a3bfed96e17e7cc419', 'uid': '062e8915-8b74-4f7c-bfa7-a62fa6d9d108'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/t params:{'token': '04e0fb949ef833661d8f371ac02c9b738e3fb44b2cd657a3bfed96e17e7cc419', 'uid': '062e8915-8b74-4f7c-bfa7-a62fa6d9d108'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/t params:{'token': '04e0fb949ef833661d8f371ac02c9b738e3fb44b2cd657a3bfed96e17e7cc419', 'uid': '062e8915-8b74-4f7c-bfa7-a62fa6d9d108'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/t params:{'token': '04e0fb949ef833661d8f371ac02c9b738e3fb44b2cd657a3bfed96e17e7cc419', 'uid': '062e8915-8b74-4f7c-bfa7-a62fa6d9d108'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/t params:{'token': '04e0fb949ef833661d8f371ac02c9b738e3fb44b2cd657a3bfed96e17e7cc419', 'uid': '062e8915-8b74-4f7c-bfa7-a62fa6d9d108'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/t params:{'token': '04e0fb949ef833661d8f371ac02c9b738e3fb44b2cd657a3bfed96e17e7cc419', 'uid': '062e8915-8b74-4f7c-bfa7-a62fa6d9d108'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/t params:{'token': '04e0fb949ef833661d8f371ac02c9b738e3fb44b2cd657a3bfed96e17e7cc419', 'uid': '062e8915-8b74-4f7c-bfa7-a62fa6d9d108'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/register params:{'login_type': '1', 'account': 'jasonphd', 'password': 'jasonphd', 'uuid': 'jasonphd11111'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/login params:{'login_type': '1', 'account': 'jasonphd', 'password': 'jasonphd', 'uuid': 'xixixixix'} method:POST ip:127.0.0.1
+2024-02-23 INFO: url:http://127.0.0.1:5000/user/t params:{'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiN2YxMGViOWQtZWIwZC00ZWE2LWI2MTUtYmExOGEyNmUxNDUzIiwiZXhwIjoxNzA4NjgyMzE2fQ.xU0DZOE-RoPl0QkQnlbn-b51aZkIBM0-3U_NgoIn0ik', 'uid': '7f10eb9d-eb0d-4ea6-b615-ba18a26e1453'} method:POST ip:127.0.0.1

+ 2 - 0
server/flaskProject/requirements.txt

@@ -0,0 +1,2 @@
+Flask~=2.2.3
+PyJWT~=2.6.0

+ 166 - 5
server/flaskProject/user.py

@@ -1,7 +1,13 @@
-from flask import request,jsonify
+from time import sleep
+
+from data import MySQLTool
+import hashlib
+import uuid
+import TokenUtils
+
 
 class User:
-    def __init__(self, action, data,logger):
+    def __init__(self, action, data, logger):
         self.action = action
         self.data = data
         self.logger = logger
@@ -11,12 +17,167 @@ class User:
             return self.register()
         elif self.action == "login":
             return self.login()
+        else:
+            return self.test()
 
     def register(self):
         print("---register---")
-        return "register"
+        return register(self.data)
 
     def login(self):
         print("---login----")
-        self.logger.log("😂")
-        return self.data
+        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)
+    print(newtoken)
+    sleep(10)
+    newt = TokenUtils.verify_jwt_token(uid, newtoken)
+    print(newt)
+    return "1"
+
+
+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")
+    else:
+        print("3333")
+
+
+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(2004, get_error_message(2004), {})
+            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(200, "注册成功", {})
+    else:
+        template = get_template(2004, get_error_message(2004), {})
+        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(2004, get_error_message(2004), {})
+            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
+
+
+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":
+        print("222")
+    else:
+        print("3333")
+
+
+def get_template(status, message, data):
+    # 构建返回模板
+    template = {
+        "status": status,
+        "message": message,
+        "data": data
+    }
+    return template
+
+
+def get_error_message(code):
+    error_messages = {
+        0: "success",
+        1001: "账号不能为空",
+        1002: "密码不能为空",
+        1003: "用户名已存在",
+        1004: "该用户暂未注册",
+        1005: "用户密码错误",
+
+        2000: "请求参数不完整",
+
+        # 添加更多的错误码和错误信息
+    }
+
+    if code in error_messages:
+        return error_messages[code]
+    else:
+        return "未知错误"