diff --git a/gin_ops/main.go b/gin_ops/main.go index 5390177..5232f8e 100644 --- a/gin_ops/main.go +++ b/gin_ops/main.go @@ -77,7 +77,7 @@ func main() { r.Static("/dist/", "./static/dist/") //静态用户上传的文件 - r.Static("/avatar/", models.Configs_file.Pahts["avatar"]) + //r.Static("/avatar/", models.Configs_file.Pahts["avatar"]) //store := cookie.NewStore([]byte("secret")) // 自定义 404 页面(需要提前加载模板) diff --git a/gin_ops/models/database.go b/gin_ops/models/database.go index 99de474..2e9c8bb 100644 --- a/gin_ops/models/database.go +++ b/gin_ops/models/database.go @@ -60,7 +60,7 @@ type Cookie struct { Path string `gorm:"size:255;not null;default:/"` ExpiresAt time.Time `gorm:"type:datetime;index"` CreatedAt time.Time `gorm:"type:datetime;not null;default:CURRENT_TIMESTAMP"` - UpdatedAt time.Time `gorm:"type:datetime;not null;default:CURRENT_TIMESTAMP"` + UpdatedAt time.Time `gorm:"type:datetime;index;not null;default:CURRENT_TIMESTAMP"` SecureFlag bool `gorm:"not null;default:false"` HttpOnly bool `gorm:"not null;default:false"` SameSite string `gorm:"size:10;not null;default:'Lax'"` diff --git a/gin_ops/routers/api_users.go b/gin_ops/routers/api_users.go index 231e67a..6f90a81 100644 --- a/gin_ops/routers/api_users.go +++ b/gin_ops/routers/api_users.go @@ -24,7 +24,7 @@ func user_logout(ctx *gin.Context) { //删除数据库里的cookie var cookie models.Cookie if err := mapstructure.Decode(cookie_any, &cookie); err == nil { - models.DB.Where("ID=?", cookie.ID).Delete(&cookie) + models.DB.Where(&cookie).Delete(&cookie) //删除前端cookie ctx.SetCookie("user", "", -1, "/", models.Configs_wed.Host, models.Configs_wed.Tls, true) ctx.Set("cookie", nil) @@ -71,7 +71,7 @@ func V1_user_api(r *gin.RouterGroup) { var user models.User user.Name = newUser.Name - if models.DB.Where("Name=?", user.Name).First(&user).Error == nil { + if models.DB.Where(&user).First(&user).Error == nil { //fmt.Println("找到用户:", user.ID) Return_json(ctx, "user_name_dup", nil) } else { @@ -121,7 +121,7 @@ func V1_user_api(r *gin.RouterGroup) { var user models.User user.Name = newUser.Name - if models.DB.Where("Name=?", user.Name).First(&user).Error == nil { + if models.DB.Where(&user).First(&user).Error == nil { // 有数据 //fmt.Println(user) //fmt.Println(newUser) @@ -163,7 +163,7 @@ func V1_user_api(r *gin.RouterGroup) { UserID: user.ID, } - models.DB.Where("ID=?", user_info.ID).First(&user_info) + models.DB.Where(&user_info).First(&user_info) red := map[string]interface{}{ "cookie": new_cookie, @@ -225,7 +225,7 @@ func V1_user_api(r *gin.RouterGroup) { if json_data.Avatar_id != 0 { file_info := models.File_info{} file_info.ID = json_data.Avatar_id - if models.DB.Where("ID=?", file_info.ID).First(&file_info).Error == nil { + if models.DB.Where(&file_info).First(&file_info).Error == nil { //读取到文件,判断是不是图片 if file_info.Type == "image" && file_info.UserID == user_info.UserID { file_id_str := fmt.Sprintf("%d", file_info.ID) @@ -273,7 +273,7 @@ func V1_user_api(r *gin.RouterGroup) { user_fund := models.User{ ID: user_info.UserID, } - if models.DB.Where("ID=?", user_fund.ID).Updates(&user_updata).Error == nil { + if models.DB.Where(&user_fund).Updates(&user_updata).Error == nil { Return_json(ctx, "api_ok", nil) } else { Return_json(ctx, "DB_err", nil) @@ -307,12 +307,12 @@ func V1_user_api(r *gin.RouterGroup) { user_fund := models.User{ ID: user_info.UserID, } - models.DB.Where("ID=?", user_fund.ID).First(&user_fund) + models.DB.Where(&user_fund).First(&user_fund) if user_fund.Pass == pass_old { user_new := models.User{ Pass: pass_new, } - if models.DB.Where("ID=?", user_fund.ID).Updates(&user_new).Error == nil { + if models.DB.Where(&user_fund).Updates(&user_new).Error == nil { user_logout(ctx) //Return_json(ctx, "api_ok", nil) } else { diff --git a/gin_ops/routers/routers_flie.go b/gin_ops/routers/routers_flie.go index b1d5a55..e4342fd 100644 --- a/gin_ops/routers/routers_flie.go +++ b/gin_ops/routers/routers_flie.go @@ -28,7 +28,7 @@ func Router_file(r *gin.RouterGroup) { file_info := models.File_info{ ID: uint(id_int), } - if models.DB.Where("ID=?", file_info.ID).First(&file_info).Error == nil { + if models.DB.Where(&file_info).First(&file_info).Error == nil { Return_file(ctx, &file_info, true) } else { //fmt.Println("not fund") @@ -53,7 +53,7 @@ func Router_file(r *gin.RouterGroup) { file_info := models.File_info{ ID: uint(id_int), } - if models.DB.Where("ID=?", file_info.ID).First(&file_info).Error == nil { + if models.DB.Where(&file_info).First(&file_info).Error == nil { Return_file(ctx, &file_info, false) } else { //fmt.Println("not fund") diff --git a/gin_ops/routers/uses.go b/gin_ops/routers/uses.go index 53dd47f..6462328 100644 --- a/gin_ops/routers/uses.go +++ b/gin_ops/routers/uses.go @@ -29,7 +29,7 @@ func Use_login_from_cookie(ctx *gin.Context) { var cookie_up models.Cookie cookie_up.UpdatedAt = time.Now() cookie_up.ExpiresAt = time.Now().Add(time.Duration(models.Configs_user.Cookie_timeout) * time.Second) //计算过期时间 - models.DB.Model(&models.Cookie{}).Where(&cookie).Updates(&cookie_up) + models.DB.Where("ID=?", cookie.ID).Updates(&cookie_up) //更新前端cookie ctx.SetCookie("user", cookie.Value, models.Configs_user.Cookie_timeout, "/", models.Configs_wed.Host, models.Configs_wed.Tls, true) cookie.UpdatedAt = cookie_up.UpdatedAt