ming před 3 roky
rodič
revize
f63b983c66
3 změnil soubory, kde provedl 47 přidání a 1 odebrání
  1. binární
      api/__pycache__/main.cpython-39.pyc
  2. 22 1
      api/main.py
  3. 25 0
      api/templates/script_index.js

binární
api/__pycache__/main.cpython-39.pyc


+ 22 - 1
api/main.py

@@ -151,6 +151,27 @@ def protected(request: Request, Authorize: AuthJWT = Depends()):
     current_user = Authorize.get_jwt_subject()
     return current_user
 
+@app.post('/get_user')
+def get_user_post(token: str = Depends(oauth2_scheme)):
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
+    credentials_exception = HTTPException(
+        status_code=status.HTTP_401_UNAUTHORIZED,
+        detail="Could not validate credentials",
+        headers={"WWW-Authenticate": "Bearer"},
+    )
+    try:
+        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
+        username: str = payload.get("sub")
+        if username is None:
+            raise credentials_exception
+        token_data = models.TokenData(username=username)
+    except JWTError:
+        raise credentials_exception
+    user = get_user(username=token_data.username)
+    if user is None:
+        raise credentials_exception
+    return user
+
 
 @app.get('/logout')
 def logout(request: Request, Authorize: AuthJWT = Depends()):
@@ -290,7 +311,7 @@ def get_user(username: str):
         return False
     user_dict = next(
         iter(db.query('SELECT * FROM AI_anchor.users where username ="'+username+'"')))
-    user = User(**user_dict)
+    user = models.User(**user_dict)
     return user
     
 def user_register(user):

+ 25 - 0
api/templates/script_index.js

@@ -21,3 +21,28 @@ $("#go_login").click(function () {
   xhr.send(data);
 
 });
+
+
+$(".gen_avatar").click(function () {
+  dataOBJ = { "imgurl": $('.img_src').val() }
+  objstr = JSON.stringify(dataOBJ);
+  console.log(dataOBJ)
+  //alert('資料已送出! 請耐心等候')
+  $.ajax({
+    url: 'http://127.0.0.1:8000/refresh',
+    headers: {
+      'x-csrf-jwt': token
+  },
+    dataType: 'json', // 預期從server接收的資料型態
+    contentType: 'application/json; charset=utf-8', // 要送到server的資料型態
+    type: 'post',
+    success: function (suc_data) {
+      alert(suc_data.msg)
+    },
+    //data:JSON.stringify({n1:"12",n2:"22"}),
+    error: function (error) {
+      console.error(error)
+    }
+  });
+
+});