From a6e4d6e219e063332d1ccca45a02c05a6a0b94d0 Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 30 Jun 2025 18:42:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E6=96=87=E4=BB=B6api?= =?UTF-8?q?=E8=BF=98=E9=9C=80=E8=A6=81=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin_ops/routers/api_files.go | 43 ++++++- gin_ops/routers/api_users.go | 7 +- gin_ops/routers/uses.go | 8 +- gin_ops/static/dist/js/my_js_func.js | 63 ++++++++-- gin_ops/templates/setting-my.html | 180 ++++++++++++++------------- 5 files changed, 191 insertions(+), 110 deletions(-) diff --git a/gin_ops/routers/api_files.go b/gin_ops/routers/api_files.go index 7ab3da0..3e60557 100644 --- a/gin_ops/routers/api_files.go +++ b/gin_ops/routers/api_files.go @@ -11,20 +11,51 @@ import ( "saas/models" "github.com/gin-gonic/gin" + "github.com/mitchellh/mapstructure" ) func V1_file_api(r *gin.RouterGroup) { r.GET("/", func(ctx *gin.Context) { - ctx.JSON(http.StatusOK, map[string]interface{}{ - "error": "you need use Post", - }) + Return_json(ctx, "json_error", nil) }) - r.POST("/upload", func(ctx *gin.Context) { + + //文件api是一定要登录的,直接用中间件判断登录状态 + // r.Use(func(ctx *gin.Context) { + // Use_is_login(ctx) + // }) + + r.Use(func(ctx *gin.Context) { + fmt.Println("file_api") + cookie := ctx.PostForm("cookie") + var cookie_t models.Cookie + if err := mapstructure.Decode(cookie, &cookie_t); err == nil { + if cookie_t.Value != "" { + cookie_vel := cookie_t.Value + fmt.Println(cookie_vel) + } + + } + + //fmt.Println(cookie) file, err := ctx.FormFile("file") if err == nil { - dst := path.Join("./data/upload", file.Filename) - ctx.SaveUploadedFile(file, dst) + fmt.Println(file) + } else { + fmt.Println(err) } + + }) + + r.POST("/upload", func(ctx *gin.Context) { + //先判断有没有登录 + //获取中间件处理的结果 + _, is_login := ctx.Get("user_info") + if is_login { + Return_json(ctx, "api_ok", nil) + } else { + Return_json(ctx, "user_no_sign", nil) + } + }) //接收头像的接口, diff --git a/gin_ops/routers/api_users.go b/gin_ops/routers/api_users.go index 7663ee5..c59ee31 100644 --- a/gin_ops/routers/api_users.go +++ b/gin_ops/routers/api_users.go @@ -1,7 +1,6 @@ package routers import ( - "fmt" "saas/models" "strings" "time" @@ -157,9 +156,9 @@ func V1_user_api(r *gin.RouterGroup) { //先判断是否已经登录 //获取中间件处理的结果 - user_info, is_login := ctx.Get("user_info") - fmt.Println(is_login) - fmt.Println(user_info) + _, is_login := ctx.Get("user_info") + //fmt.Println(is_login) + //fmt.Println(user_info) if is_login == true { //fmt.Println("loged") cookie_any, _ := ctx.Get("cookie") //这个cookie在中间件已经判断为有效的,否则is_login不可能为true,所以直接在数据库删除应该是安全的 diff --git a/gin_ops/routers/uses.go b/gin_ops/routers/uses.go index e746475..8601300 100644 --- a/gin_ops/routers/uses.go +++ b/gin_ops/routers/uses.go @@ -1,7 +1,6 @@ package routers import ( - "fmt" "saas/models" "time" @@ -28,6 +27,7 @@ func Fitst_use(ctx *gin.Context) { } } + var data_t map[string]interface{} if err = mapstructure.Decode(jsonData["data"], &data_t); err == nil { ctx.Set("data", &data_t) @@ -80,7 +80,7 @@ func Fitst_use(ctx *gin.Context) { models.DB.Create(&user_info) // 传入指针 } //写入当前登录的用户信息 传递给下一个组件 - fmt.Println(user_info) + //fmt.Println(user_info) ctx.Set("user_info", &user_info) ctx.Set("user", &user) } else { @@ -116,3 +116,7 @@ func Fitst_use(ctx *gin.Context) { } } + +func Use_is_login(ctx *gin.Context) { + +} diff --git a/gin_ops/static/dist/js/my_js_func.js b/gin_ops/static/dist/js/my_js_func.js index b65bf43..5a488b3 100644 --- a/gin_ops/static/dist/js/my_js_func.js +++ b/gin_ops/static/dist/js/my_js_func.js @@ -27,9 +27,51 @@ function load_json(key) { } } +function post_file(path, file, file_name, callback) { + var head_path = "/api/v1/file"; + // 创建FormData对象 + const formData = new FormData(); + formData.append("file", file, file_name); // 'file' 是后端接收文件的字段名 + + //获取保存的cookie + var cookie = load_json("cookie"); + //console.log(cookie); + formData.append("cookie", JSON.stringify(cookie)); //插入cookie + + var re_data = {}; + + // 发送请求 + axios + .post(head_path + path, formData, { + headers: { + "Content-Type": "multipart/form-data", // 这里实际上可以省略,因为FormData会被正确识别 + }, + }) + .then((response) => { + //console.log(response) + re_data["statusCode"] = response.status; + //载入服务器返回的数据 + if (response.data) { + re_data["data"] = response.data; + } + //自动保存服务器发送的cookie + if (response.cookie) { + if (response.cookie.Value == "") { + dele("cookie"); + } else { + save_json("cookie", response.cookie); + } + } + callback(re_data); + }) + .catch((error) => { + re_data["statusCode"] = -1; + re_data["error"] = error; + callback(re_data); + }); +} + function post_json(path, json, callback) { - var host = ""; - var port = 0; var head_path = "/api/v1"; //把cookie插入json var data = {}; @@ -52,20 +94,21 @@ function post_json(path, json, callback) { //载入服务器返回的数据 if (response.data) { re_data["data"] = response.data; - } - //自动保存服务器发送的cookie - if (response.cookie) { - if (response.cookie.Value == "") { - dele("cookie"); - } else { - save_json("cookie", response.cookie); + //自动保存服务器发送的cookie + if (response.data.cookie) { + if (response.data.cookie.Value == "") { + dele("cookie"); + } else { + save_json("cookie", response.data.cookie); + } } } + callback(re_data); }) .catch((error) => { re_data["statusCode"] = -1; - re_data["error"]=error; + re_data["error"] = error; callback(re_data); }); } diff --git a/gin_ops/templates/setting-my.html b/gin_ops/templates/setting-my.html index 23a8e41..0418c9a 100644 --- a/gin_ops/templates/setting-my.html +++ b/gin_ops/templates/setting-my.html @@ -83,12 +83,14 @@
名字
- +
备注
- +
@@ -104,10 +106,10 @@ - +