From 85d59b2799bec90acbe329be28a31428ab3163f8 Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 11 Jul 2025 20:59:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E5=BA=A6=E5=88=B0=E6=9B=B4=E6=96=B0us?= =?UTF-8?q?erinfo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin_ops/routers/api_users.go | 519 ++++++++++++++------------- gin_ops/routers/api_users_head.go | 11 +- gin_ops/routers/routers_api.go | 10 +- gin_ops/routers/routers_file_head.go | 4 + gin_ops/routers/routers_flie.go | 13 +- gin_ops/static/dist/js/my_js_func.js | 2 +- gin_ops/templates/setting-my.html | 90 +++-- 7 files changed, 357 insertions(+), 292 deletions(-) create mode 100644 gin_ops/routers/routers_file_head.go diff --git a/gin_ops/routers/api_users.go b/gin_ops/routers/api_users.go index 0c368a2..3cc5f07 100644 --- a/gin_ops/routers/api_users.go +++ b/gin_ops/routers/api_users.go @@ -1,8 +1,8 @@ package routers import ( + "fmt" "saas/models" - "strings" "time" "github.com/gin-gonic/gin" @@ -11,53 +11,53 @@ import ( func V1_user_api(r *gin.RouterGroup) { - var err_code = Error_code["api_ok"] - var err_msg string - r.POST("/add", func(ctx *gin.Context) { //返回前端的数据 //转换传进来的数据 //转换传进来的数据 - var jsonData Add_user_from - data, _ := ctx.Get("data") - if err := mapstructure.Decode(data, &jsonData); err == nil { - //转换字段 - newUser := models.User{ - Name: jsonData.Username, - Email: jsonData.Useremail, - Pass: jsonData.Userpass, // 实际应替换为哈希值 - Date: time.Now(), - // Date 字段无需赋值,数据库会自动填充默认值 - } - //fmt.Println(newUser) - //对用户的密码进行哈希替换 - newUser.Pass = models.Hash_user_pass(newUser.Pass) + var jsonData From_user_add + data, is_have_data := ctx.Get("data") + if is_have_data { + if err := mapstructure.Decode(data, &jsonData); err == nil { + //转换字段 + newUser := models.User{ + Name: jsonData.Username, + Email: jsonData.Useremail, + Pass: jsonData.Userpass, // 实际应替换为哈希值 + Date: time.Now(), + // Date 字段无需赋值,数据库会自动填充默认值 + } + //fmt.Println(newUser) + //对用户的密码进行哈希替换 + newUser.Pass = models.Hash_user_pass(newUser.Pass) - //用户名是唯一的,先读取是否有这个用户名 - var user models.User - user.Name = newUser.Name + //用户名是唯一的,先读取是否有这个用户名 + var user models.User + user.Name = newUser.Name + + if models.DB.Where(user.Name).First(&user).Error == nil { + //fmt.Println("找到用户:", user.ID) + Return_json(ctx, "user_name_dup", nil) + } else { + //fmt.Println("用户不存在") + models.DB.Create(&newUser) // 传入指针 + + //创建info + var user_info models.User_info + user_info.AvatarPath = models.Configs_user.Avatar_path + user_info.UserID = newUser.ID + models.DB.Create(&user_info) // 传入指针 + + Return_json(ctx, "api_ok", nil) + } - if models.DB.Where(user.Name).First(&user).Error == nil { - //fmt.Println("找到用户:", user.ID) - Return_json(ctx, "user_name_dup", nil) } else { - //fmt.Println("用户不存在") - models.DB.Create(&newUser) // 传入指针 - - //创建info - var user_info models.User_info - user_info.AvatarPath = models.Configs_user.Avatar_path - user_info.UserID = newUser.ID - models.DB.Create(&user_info) // 传入指针 - - Return_json(ctx, "api_ok", nil) + Return_json(ctx, "json_error", nil) } - } else { Return_json(ctx, "json_error", nil) } - //Return_json(ctx, "api_err", nil) }) @@ -65,95 +65,97 @@ func V1_user_api(r *gin.RouterGroup) { //返回前端的数据 //转换传进来的数据 - var jsonData Login_from - data, _ := ctx.Get("data") - if err := mapstructure.Decode(data, &jsonData); err == nil { - if jsonData.Username != "" { - //转换字段 - newUser := models.User{ - Name: jsonData.Username, - Pass: jsonData.Password, // 实际应替换为哈希值 - // Date 字段无需赋值,数据库会自动填充默认值 - } + var jsonData From_user_loggin + data, is_have_data := ctx.Get("data") + if is_have_data { + if err := mapstructure.Decode(data, &jsonData); err == nil { + if jsonData.Username != "" { + //转换字段 + newUser := models.User{ + Name: jsonData.Username, + Pass: jsonData.Password, // 实际应替换为哈希值 + // Date 字段无需赋值,数据库会自动填充默认值 + } - //对用户的密码进行哈希替换 - newUser.Pass = models.Hash_user_pass(newUser.Pass) + //对用户的密码进行哈希替换 + newUser.Pass = models.Hash_user_pass(newUser.Pass) - var user models.User - user.Name = newUser.Name - if models.DB.Where(&user).First(&user).Error == nil { - // 有数据 - //fmt.Println(user) - //fmt.Println(newUser) + var user models.User + user.Name = newUser.Name + if models.DB.Where(&user).First(&user).Error == nil { + // 有数据 + //fmt.Println(user) + //fmt.Println(newUser) - if user.Pass == newUser.Pass { - //成功登录 - //发送cookie - //cookie时间 - var cookie_time = 0 - if jsonData.Is_keep_login { - cookie_time = models.Configs_user.Cookie_timeout + if user.Pass == newUser.Pass { + //成功登录 + //发送cookie + //cookie时间 + var cookie_time = 0 + if jsonData.Is_keep_login { + cookie_time = models.Configs_user.Cookie_timeout + } + + cookie := models.Rand_str_32() //生成32字节cookie + //cookie := "testcookie" + //fmt.Println(cookie) + //将cookie写进数据库 + new_cookie := models.Cookie{} + new_cookie.Domain = models.Configs_wed.Host + new_cookie.Name = "user" + new_cookie.Value = cookie + new_cookie.UserID = user.ID + + //cookie时间 + new_cookie.CreatedAt = time.Now() + new_cookie.UpdatedAt = new_cookie.CreatedAt + //计算cookie失效时间 + new_cookie.ExpiresAt = time.Now().Add(time.Duration(models.Configs_user.Cookie_timeout) * time.Second) //计算过期时间 + new_cookie.SecureFlag = models.Configs_wed.Tls + + //发送到前端 + ctx.SetCookie("user", cookie, cookie_time, "/", models.Configs_wed.Host, models.Configs_wed.Tls, true) + ctx.Set("cookie", new_cookie) + //写到数据库 + models.DB.Create(&new_cookie) // 传入指针 + + //获取用户info + user_info := models.User_info{ + UserID: user.ID, + } + + models.DB.Where(&user_info).First(&user_info) + + red := map[string]interface{}{ + "cookie": new_cookie, + "user_info": user_info, + } + + Return_json(ctx, "api_ok", red) + + } else { + //密码错误 + + Return_json(ctx, "user_password_err", nil) } - cookie := models.Rand_str_32() //生成32字节cookie - //cookie := "testcookie" - //fmt.Println(cookie) - //将cookie写进数据库 - new_cookie := models.Cookie{} - new_cookie.Domain = models.Configs_wed.Host - new_cookie.Name = "user" - new_cookie.Value = cookie - new_cookie.UserID = user.ID - - //cookie时间 - new_cookie.CreatedAt = time.Now() - new_cookie.UpdatedAt = new_cookie.CreatedAt - //计算cookie失效时间 - new_cookie.ExpiresAt = time.Now().Add(time.Duration(models.Configs_user.Cookie_timeout) * time.Second) //计算过期时间 - new_cookie.SecureFlag = models.Configs_wed.Tls - - //发送到前端 - ctx.SetCookie("user", cookie, cookie_time, "/", models.Configs_wed.Host, models.Configs_wed.Tls, true) - ctx.Set("cookie", new_cookie) - //写到数据库 - models.DB.Create(&new_cookie) // 传入指针 - - //获取用户info - user_info := models.User_info{ - UserID: user.ID, - } - - models.DB.Where(&user_info).First(&user_info) - - red := map[string]interface{}{ - "cookie": new_cookie, - "user_info": user_info, - } - - Return_json(ctx, "api_ok", red) - } else { - //密码错误 + //fmt.Println("用户不存在") - Return_json(ctx, "user_password_err", nil) + Return_json(ctx, "user_name_nofind", nil) } } else { - //fmt.Println("用户不存在") - Return_json(ctx, "user_name_nofind", nil) } } else { - Return_json(ctx, "user_name_nofind", nil) + Return_json(ctx, "json_error", nil) } - } else { Return_json(ctx, "json_error", nil) } - //Return_json(ctx, "api_err", nil) - }) r.POST("/logout", func(ctx *gin.Context) { @@ -190,180 +192,207 @@ func V1_user_api(r *gin.RouterGroup) { }) r.POST("/updata_info", func(ctx *gin.Context) { - //返回前端的数据 - err_msg = "user_api_error" - err_code = Error_code[err_msg] + _, is_login := ctx.Get("user_info") + if is_login { - //先判断是否已经登录 - //获取中间件处理的结果 - is_login, _ := ctx.Get("is_login") - if is_login == true { //转换传进来的数据 - var jsonData map[string]interface{} - if err := ctx.ShouldBindJSON(&jsonData); err == nil { + var json_data From_user_updata_info + data, is_have_data := ctx.Get("data") + if is_have_data { + if err := mapstructure.Decode(data, &json_data); err == nil { + fmt.Println(json_data) + //先判断头像是否合法 + if json_data.Avatar_id != 0 { - user_info_, _ := ctx.Get("user_info") - user_info, _ := user_info_.(*models.User_info) //这个数据本身就是从数据库读出来的,理论上结构转换不会出错 - user_info_find := models.User_info{ - ID: user_info.ID, - } - - new_user_info := models.User_info{ - AvatarPath: jsonData["avatar"].(string), - FirstName: jsonData["first_name"].(string), - Username: jsonData["username"].(string), - Birthdate: models.Time_date_str_to_time(jsonData["birthday"].(string)), - } - - //需要验证传入数据的合法性 例如头像url是否站内的 - if strings.HasPrefix(new_user_info.AvatarPath, models.Configs_user.Avatar_ginrouter_path) { + } + Return_json(ctx, "api_ok", nil) } else { - new_user_info.AvatarPath = models.Configs_user.Avatar_path + Return_json(ctx, "json_error", nil) } - - //fmt.Printf("%%#v: %#v\n", new_user_info) - models.DB.Where(&user_info_find).Updates(&new_user_info) - - err_msg = "api_ok" - err_code = Error_code[err_msg] - } else { - err_msg = "json_error" - err_code = Error_code[err_msg] + Return_json(ctx, "json_error", nil) } - } else { - //fmt.Println("no loged") - err_msg = "user_no_sign" - err_code = Error_code[err_msg] - } + //var user_info models.User_info{}{} - ctx.JSON(200, map[string]interface{}{ - "api": "ok", - "err_code": err_code, - "err_msg": err_msg, - }) + } else { + Return_json(ctx, "user_no_sign", nil) + } + // //返回前端的数据 + // err_msg = "user_api_error" + // err_code = Error_code[err_msg] + + // //先判断是否已经登录 + // //获取中间件处理的结果 + // is_login, _ := ctx.Get("is_login") + // if is_login == true { + // //转换传进来的数据 + // var jsonData map[string]interface{} + // if err := ctx.ShouldBindJSON(&jsonData); err == nil { + + // user_info_, _ := ctx.Get("user_info") + // user_info, _ := user_info_.(*models.User_info) //这个数据本身就是从数据库读出来的,理论上结构转换不会出错 + // user_info_find := models.User_info{ + // ID: user_info.ID, + // } + + // new_user_info := models.User_info{ + // AvatarPath: jsonData["avatar"].(string), + // FirstName: jsonData["first_name"].(string), + // Username: jsonData["username"].(string), + // Birthdate: models.Time_date_str_to_time(jsonData["birthday"].(string)), + // } + + // //需要验证传入数据的合法性 例如头像url是否站内的 + // if strings.HasPrefix(new_user_info.AvatarPath, models.Configs_user.Avatar_ginrouter_path) { + + // } else { + // new_user_info.AvatarPath = models.Configs_user.Avatar_path + // } + + // //fmt.Printf("%%#v: %#v\n", new_user_info) + // models.DB.Where(&user_info_find).Updates(&new_user_info) + + // err_msg = "api_ok" + // err_code = Error_code[err_msg] + + // } else { + // err_msg = "json_error" + // err_code = Error_code[err_msg] + // } + + // } else { + // //fmt.Println("no loged") + // err_msg = "user_no_sign" + // err_code = Error_code[err_msg] + // } + + // ctx.JSON(200, map[string]interface{}{ + // "api": "ok", + // "err_code": err_code, + // "err_msg": err_msg, + // }) }) r.POST("/change_email", func(ctx *gin.Context) { - //返回前端的数据 - err_msg = "user_api_error" - err_code = Error_code[err_msg] + // //返回前端的数据 + // err_msg = "user_api_error" + // err_code = Error_code[err_msg] - //先判断是否已经登录 - //获取中间件处理的结果 - is_login, _ := ctx.Get("is_login") - if is_login == true { - //转换传进来的数据 - var jsonData map[string]interface{} - if err := ctx.ShouldBindJSON(&jsonData); err == nil { + // //先判断是否已经登录 + // //获取中间件处理的结果 + // is_login, _ := ctx.Get("is_login") + // if is_login == true { + // //转换传进来的数据 + // var jsonData map[string]interface{} + // if err := ctx.ShouldBindJSON(&jsonData); err == nil { - //需要验证传入数据的合法性 - if models.Is_email_valid(jsonData["new_email"].(string)) { - user_, _ := ctx.Get("user") - user, _ := user_.(*models.User) - user_find := models.User{ - ID: user.ID, - } - user_new := models.User{ - Email: jsonData["new_email"].(string), - } - models.DB.Where(&user_find).Updates(&user_new) - err_msg = "api_ok" - err_code = Error_code[err_msg] + // //需要验证传入数据的合法性 + // if models.Is_email_valid(jsonData["new_email"].(string)) { + // user_, _ := ctx.Get("user") + // user, _ := user_.(*models.User) + // user_find := models.User{ + // ID: user.ID, + // } + // user_new := models.User{ + // Email: jsonData["new_email"].(string), + // } + // models.DB.Where(&user_find).Updates(&user_new) + // err_msg = "api_ok" + // err_code = Error_code[err_msg] - } else { - err_msg = "email_error" - err_code = Error_code[err_msg] - } + // } else { + // err_msg = "email_error" + // err_code = Error_code[err_msg] + // } - } else { - err_msg = "json_error" - err_code = Error_code[err_msg] - } + // } else { + // err_msg = "json_error" + // err_code = Error_code[err_msg] + // } - } else { - //fmt.Println("no loged") - err_msg = "user_no_sign" - err_code = Error_code[err_msg] - } + // } else { + // //fmt.Println("no loged") + // err_msg = "user_no_sign" + // err_code = Error_code[err_msg] + // } - ctx.JSON(200, map[string]interface{}{ - "api": "ok", - "err_code": err_code, - "err_msg": err_msg, - }) + // ctx.JSON(200, map[string]interface{}{ + // "api": "ok", + // "err_code": err_code, + // "err_msg": err_msg, + // }) }) r.POST("/change_pass", func(ctx *gin.Context) { - //返回前端的数据 - err_msg = "user_api_error" - err_code = Error_code[err_msg] + // //返回前端的数据 + // err_msg = "user_api_error" + // err_code = Error_code[err_msg] - //先判断是否已经登录 - //获取中间件处理的结果 - is_login, _ := ctx.Get("is_login") - if is_login == true { - //转换传进来的数据 - var jsonData map[string]interface{} - if err := ctx.ShouldBindJSON(&jsonData); err == nil { + // //先判断是否已经登录 + // //获取中间件处理的结果 + // is_login, _ := ctx.Get("is_login") + // if is_login == true { + // //转换传进来的数据 + // var jsonData map[string]interface{} + // if err := ctx.ShouldBindJSON(&jsonData); err == nil { - //需要验证传入数据的合法性 + // //需要验证传入数据的合法性 - //读取已登录的用户信息 - user_, _ := ctx.Get("user") - user, _ := user_.(*models.User) - user_find := models.User{ - ID: user.ID, - } + // //读取已登录的用户信息 + // user_, _ := ctx.Get("user") + // user, _ := user_.(*models.User) + // user_find := models.User{ + // ID: user.ID, + // } - models.DB.Where(&user_find).First(&user_find) + // models.DB.Where(&user_find).First(&user_find) - pass_old := jsonData["pass_old"].(string) - pass_new := jsonData["pass_new"].(string) - //对用户的密码进行哈希替换 - pass_old = models.Hash_user_pass(pass_old) - pass_new = models.Hash_user_pass(pass_new) + // pass_old := jsonData["pass_old"].(string) + // pass_new := jsonData["pass_new"].(string) + // //对用户的密码进行哈希替换 + // pass_old = models.Hash_user_pass(pass_old) + // pass_new = models.Hash_user_pass(pass_new) - if user_find.Pass == pass_old { + // if user_find.Pass == pass_old { - new_user := models.User{ - Pass: pass_new, - } + // new_user := models.User{ + // Pass: pass_new, + // } - //修改密码 - models.DB.Where(&user_find).Updates(&new_user) + // //修改密码 + // models.DB.Where(&user_find).Updates(&new_user) - //密码修改后所有cookie都应该失效 - cookie_find := models.Cookie{ - UserID: user.ID, - } - models.DB.Where(&cookie_find).Delete(&cookie_find) + // //密码修改后所有cookie都应该失效 + // cookie_find := models.Cookie{ + // UserID: user.ID, + // } + // models.DB.Where(&cookie_find).Delete(&cookie_find) - err_msg = "api_ok" - err_code = Error_code[err_msg] + // err_msg = "api_ok" + // err_code = Error_code[err_msg] - } else { - err_msg = "user_password_err" - err_code = Error_code[err_msg] - } + // } else { + // err_msg = "user_password_err" + // err_code = Error_code[err_msg] + // } - } else { - err_msg = "json_error" - err_code = Error_code[err_msg] - } + // } else { + // err_msg = "json_error" + // err_code = Error_code[err_msg] + // } - } else { - //fmt.Println("no loged") - err_msg = "user_no_sign" - err_code = Error_code[err_msg] - } + // } else { + // //fmt.Println("no loged") + // err_msg = "user_no_sign" + // err_code = Error_code[err_msg] + // } - ctx.JSON(200, map[string]interface{}{ - "api": "ok", - "err_code": err_code, - "err_msg": err_msg, - }) + // ctx.JSON(200, map[string]interface{}{ + // "api": "ok", + // "err_code": err_code, + // "err_msg": err_msg, + // }) }) } diff --git a/gin_ops/routers/api_users_head.go b/gin_ops/routers/api_users_head.go index ab2a8ad..19e7940 100644 --- a/gin_ops/routers/api_users_head.go +++ b/gin_ops/routers/api_users_head.go @@ -1,13 +1,20 @@ package routers -type Login_from struct { +type From_user_loggin struct { Is_keep_login bool `json:"is_keep_login"` Username string `json:"username"` Password string `json:"password"` } -type Add_user_from struct { +type From_user_add struct { Useremail string `json:"useremail"` Username string `json:"username"` Userpass string `json:"userpass"` } + +type From_user_updata_info struct { + Avatar_id uint `json:"avatar_id"` + Username string `json:"username"` + First_name string `json:"first_name"` + Birthday string `json:"birthday"` +} diff --git a/gin_ops/routers/routers_api.go b/gin_ops/routers/routers_api.go index 672ca9c..eb0fc89 100644 --- a/gin_ops/routers/routers_api.go +++ b/gin_ops/routers/routers_api.go @@ -1,8 +1,6 @@ package routers import ( - "saas/models" - "github.com/gin-gonic/gin" "github.com/mitchellh/mapstructure" ) @@ -18,13 +16,9 @@ func Router_api(r *gin.RouterGroup) { var jsonData map[string]interface{} if err := ctx.ShouldBindJSON(&jsonData); err == nil { //分离数据 - var cookie_t models.Cookie - if err = mapstructure.Decode(jsonData["cookie"], &cookie_t); err == nil { - if cookie_t.Value != "" { - cookie_vel := cookie_t.Value - ctx.Set("cookie_value", cookie_vel) - } + if jsonData["cookie"] != "" { + ctx.Set("cookie_value", jsonData["cookie"]) } var data_t map[string]interface{} diff --git a/gin_ops/routers/routers_file_head.go b/gin_ops/routers/routers_file_head.go new file mode 100644 index 0000000..4cc4c9c --- /dev/null +++ b/gin_ops/routers/routers_file_head.go @@ -0,0 +1,4 @@ +package routers + +var Url_flie_download_from_id_head string = "/file/download/id" +var Url_flie_preview_from_id_head string = "/file/preview/id" diff --git a/gin_ops/routers/routers_flie.go b/gin_ops/routers/routers_flie.go index 9563004..42aa51d 100644 --- a/gin_ops/routers/routers_flie.go +++ b/gin_ops/routers/routers_flie.go @@ -167,7 +167,7 @@ func Router_file(r *gin.RouterGroup) { models.DB.Where(&fund_file_info).Find(&fund_file_info2) if fund_file_info2.ID != 0 { - fmt.Println(fund_file_info2) + //fmt.Println(fund_file_info2) fund_file_info2.Const += 1 models.DB.Where(&fund_file_info).Updates(&fund_file_info2) } else { @@ -176,8 +176,17 @@ func Router_file(r *gin.RouterGroup) { fund_file_info2 = fund_file_info } + //url_download:=fmt.Sprintf("") + file_id_str := fmt.Sprintf("%d", fund_file_info2.ID) + url_download := path.Join(Url_flie_download_from_id_head, file_id_str) + url_preview := path.Join(Url_flie_preview_from_id_head, file_id_str) + red := map[string]interface{}{ - "data": fund_file_info2, + "id": fund_file_info2.ID, + "name": fund_file_info2.Name, + "sha256": fund_file_info2.Sha256, + "download": url_download, + "preview": url_preview, } Return_json(ctx, "api_ok", red) diff --git a/gin_ops/static/dist/js/my_js_func.js b/gin_ops/static/dist/js/my_js_func.js index b4241ba..bfca91f 100644 --- a/gin_ops/static/dist/js/my_js_func.js +++ b/gin_ops/static/dist/js/my_js_func.js @@ -78,7 +78,7 @@ function post_json(path, json, callback) { data["data"] = json; var cookie = load_json("cookie"); if (cookie) { - data["cookie"] = cookie; + data["cookie"] = cookie.Value; } var re_data = {}; diff --git a/gin_ops/templates/setting-my.html b/gin_ops/templates/setting-my.html index f24ec0b..5d3fe3d 100644 --- a/gin_ops/templates/setting-my.html +++ b/gin_ops/templates/setting-my.html @@ -149,7 +149,7 @@ -
+
@@ -378,26 +378,24 @@ } /* 消息提示 */ - .alert { + .alertavater { padding: 15px; border-radius: 6px; margin-top: 20px; display: none; } - .alert-success { + .alertavater-success { background: #dff0d8; color: #3c763d; } - .alert-error { + .alertavater-error { background: #f2dede; color: var(--error-color); } - input[type="file"] { - display: none; - } + @@ -453,6 +451,7 @@ const image = document.getElementById('cropper-image'); const message = document.getElementById('message'); let currentScale = 1; + var avatar_id = 0; // 初始化Cropper function initCropper(imageSrc) { @@ -562,17 +561,18 @@ post_file("/image", blob, `avatar.png`, (c) => { if (c.statusCode == 200) { if (c.data.err_code == 0) { - - //banner_alert('success', "更换成功", 950) showMessage(`✅ 上传成功!`, 'success'); - //avatar_toolt.hide(); + avatar_id = c.data.return.id; + set_user_avatar(c.data.return.preview); + avatar_toolt.hide(); + } else { - showMessage(`❌ 上传失败:`+c.data.err_msg, 'error'); + showMessage(`❌ 上传失败:` + c.data.err_msg, 'error'); //banner_alert('warning', "服务错误", 3000) } } else { //banner_alert('danger', "网络连接错误:" + c.statusCode, 3000) - showMessage(`❌ 网络连接错误:`+c.statusCode, 'error'); + showMessage(`❌ 网络连接错误:` + c.statusCode, 'error'); } }) @@ -631,36 +631,58 @@ //更新用户资料 function updata_user_info() { - const url = '/api/v1/user/updata_info'; - const sumt_data = { - avatar: get_user_avatar(), + post_json("/user/updata_info", { + avatar_id: avatar_id, username: document.getElementById("username").value, first_name: document.getElementById("first_name").value, birthday: document.getElementById("birthday-picker").value, + }, (c) => { + if (c.statusCode == 200) { - - }; - try { - const response = axios.post(url, sumt_data, { - headers: { - 'Content-Type': 'application/json' - } - }).then(response => { - //console.log(response) - if (response.data.err_code == 0) { - location.reload() + if (c.data.err_code == 0) { + banner_alert('success', "更新成功", 950) } else { - console.log(response.data) + banner_alert('warning', "错误:"+c.data.err_msg, 1000) } - }); - } catch (error) { - if (error.response) { - // 服务器返回了错误状态码(如 4xx, 5xx) - console.error('服务器错误:', error.response.data); + + } else { - console.error('请求未完成:', error.message); + banner_alert('danger', "网络连接错误:" + c.error, 10000) } - } + }); + // const url = '/api/v1/user/updata_info'; + // const sumt_data = { + // avatar: get_user_avatar(), + // avatar_id:avatar_id, + // username: document.getElementById("username").value, + // first_name: document.getElementById("first_name").value, + // birthday: document.getElementById("birthday-picker").value, + + + // }; + // try { + // const response = axios.post(url, sumt_data, { + // headers: { + // 'Content-Type': 'application/json' + // } + // }).then(response => { + // //console.log(response) + // if (response.data.err_code == 0) { + // location.reload() + // } else { + // console.log(response.data) + // } + + // }); + // } catch (error) { + // if (error.response) { + // // 服务器返回了错误状态码(如 4xx, 5xx) + // console.error('服务器错误:', error.response.data); + // } else { + // console.error('请求未完成:', error.message); + // } + // } + } \ No newline at end of file