优化数据库兼容性与性能
This commit is contained in:
@@ -24,7 +24,7 @@ func user_logout(ctx *gin.Context) {
|
|||||||
//删除数据库里的cookie
|
//删除数据库里的cookie
|
||||||
var cookie models.Cookie
|
var cookie models.Cookie
|
||||||
if err := mapstructure.Decode(cookie_any, &cookie); err == nil {
|
if err := mapstructure.Decode(cookie_any, &cookie); err == nil {
|
||||||
models.DB.Where(&cookie).Delete(&cookie)
|
models.DB.Where("ID=?", cookie.ID).Delete(&cookie)
|
||||||
//删除前端cookie
|
//删除前端cookie
|
||||||
ctx.SetCookie("user", "", -1, "/", models.Configs_wed.Host, models.Configs_wed.Tls, true)
|
ctx.SetCookie("user", "", -1, "/", models.Configs_wed.Host, models.Configs_wed.Tls, true)
|
||||||
ctx.Set("cookie", nil)
|
ctx.Set("cookie", nil)
|
||||||
@@ -71,7 +71,7 @@ func V1_user_api(r *gin.RouterGroup) {
|
|||||||
var user models.User
|
var user models.User
|
||||||
user.Name = newUser.Name
|
user.Name = newUser.Name
|
||||||
|
|
||||||
if models.DB.Where(&user).First(&user).Error == nil {
|
if models.DB.Where("Name=?", user.Name).First(&user).Error == nil {
|
||||||
//fmt.Println("找到用户:", user.ID)
|
//fmt.Println("找到用户:", user.ID)
|
||||||
Return_json(ctx, "user_name_dup", nil)
|
Return_json(ctx, "user_name_dup", nil)
|
||||||
} else {
|
} else {
|
||||||
@@ -121,7 +121,7 @@ func V1_user_api(r *gin.RouterGroup) {
|
|||||||
|
|
||||||
var user models.User
|
var user models.User
|
||||||
user.Name = newUser.Name
|
user.Name = newUser.Name
|
||||||
if models.DB.Where(&user).First(&user).Error == nil {
|
if models.DB.Where("Name=?", user.Name).First(&user).Error == nil {
|
||||||
// 有数据
|
// 有数据
|
||||||
//fmt.Println(user)
|
//fmt.Println(user)
|
||||||
//fmt.Println(newUser)
|
//fmt.Println(newUser)
|
||||||
@@ -163,7 +163,7 @@ func V1_user_api(r *gin.RouterGroup) {
|
|||||||
UserID: user.ID,
|
UserID: user.ID,
|
||||||
}
|
}
|
||||||
|
|
||||||
models.DB.Where(&user_info).First(&user_info)
|
models.DB.Where("ID=?", user_info.ID).First(&user_info)
|
||||||
|
|
||||||
red := map[string]interface{}{
|
red := map[string]interface{}{
|
||||||
"cookie": new_cookie,
|
"cookie": new_cookie,
|
||||||
@@ -218,23 +218,26 @@ func V1_user_api(r *gin.RouterGroup) {
|
|||||||
Birthdate: models.Time_date_str_to_time(json_data.Birthday),
|
Birthdate: models.Time_date_str_to_time(json_data.Birthday),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sele := []string{"FirstName", "Username", "Birthdate"}
|
||||||
|
|
||||||
user_info := user_info_any.(*models.User_info)
|
user_info := user_info_any.(*models.User_info)
|
||||||
//先判断头像是否合法
|
//先判断头像是否合法
|
||||||
if json_data.Avatar_id != 0 {
|
if json_data.Avatar_id != 0 {
|
||||||
file_info := models.File_info{}
|
file_info := models.File_info{}
|
||||||
file_info.ID = json_data.Avatar_id
|
file_info.ID = json_data.Avatar_id
|
||||||
if models.DB.Where(&file_info).First(&file_info).Error == nil {
|
if models.DB.Where("ID=?", file_info.ID).First(&file_info).Error == nil {
|
||||||
//读取到文件,判断是不是图片
|
//读取到文件,判断是不是图片
|
||||||
if file_info.Type == "image" && file_info.UserID == user_info.UserID {
|
if file_info.Type == "image" && file_info.UserID == user_info.UserID {
|
||||||
file_id_str := fmt.Sprintf("%d", file_info.ID)
|
file_id_str := fmt.Sprintf("%d", file_info.ID)
|
||||||
url_preview := path.Join(Url_flie_preview_from_id_head, file_id_str)
|
url_preview := path.Join(Url_flie_preview_from_id_head, file_id_str)
|
||||||
updata_user_info.AvatarPath = url_preview
|
updata_user_info.AvatarPath = url_preview
|
||||||
|
sele = append(sele, "AvatarPath")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if models.DB.Where(&user_info).Select("FirstName", "Username", "Birthdate").Updates(&updata_user_info).Error == nil {
|
if models.DB.Where("ID=?", user_info.ID).Select(sele).Updates(&updata_user_info).Error == nil {
|
||||||
Return_json(ctx, "api_ok", nil)
|
Return_json(ctx, "api_ok", nil)
|
||||||
} else {
|
} else {
|
||||||
Return_json(ctx, "DB_err", nil)
|
Return_json(ctx, "DB_err", nil)
|
||||||
@@ -270,7 +273,7 @@ func V1_user_api(r *gin.RouterGroup) {
|
|||||||
user_fund := models.User{
|
user_fund := models.User{
|
||||||
ID: user_info.UserID,
|
ID: user_info.UserID,
|
||||||
}
|
}
|
||||||
if models.DB.Where(&user_fund).Updates(&user_updata).Error == nil {
|
if models.DB.Where("ID=?", user_fund.ID).Updates(&user_updata).Error == nil {
|
||||||
Return_json(ctx, "api_ok", nil)
|
Return_json(ctx, "api_ok", nil)
|
||||||
} else {
|
} else {
|
||||||
Return_json(ctx, "DB_err", nil)
|
Return_json(ctx, "DB_err", nil)
|
||||||
@@ -304,12 +307,12 @@ func V1_user_api(r *gin.RouterGroup) {
|
|||||||
user_fund := models.User{
|
user_fund := models.User{
|
||||||
ID: user_info.UserID,
|
ID: user_info.UserID,
|
||||||
}
|
}
|
||||||
models.DB.Where(&user_fund).First(&user_fund)
|
models.DB.Where("ID=?", user_fund.ID).First(&user_fund)
|
||||||
if user_fund.Pass == pass_old {
|
if user_fund.Pass == pass_old {
|
||||||
user_new := models.User{
|
user_new := models.User{
|
||||||
Pass: pass_new,
|
Pass: pass_new,
|
||||||
}
|
}
|
||||||
if models.DB.Where(&user_fund).Updates(&user_new).Error == nil {
|
if models.DB.Where("ID=?", user_fund.ID).Updates(&user_new).Error == nil {
|
||||||
user_logout(ctx)
|
user_logout(ctx)
|
||||||
//Return_json(ctx, "api_ok", nil)
|
//Return_json(ctx, "api_ok", nil)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package routers
|
package routers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
)
|
)
|
||||||
@@ -24,7 +22,7 @@ func Router_api(r *gin.RouterGroup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if jsonData["data"] != nil {
|
if jsonData["data"] != nil {
|
||||||
fmt.Println(jsonData["data"])
|
//fmt.Println(jsonData["data"])
|
||||||
var data_t map[string]interface{}
|
var data_t map[string]interface{}
|
||||||
if err = mapstructure.Decode(jsonData["data"], &data_t); err == nil {
|
if err = mapstructure.Decode(jsonData["data"], &data_t); err == nil {
|
||||||
ctx.Set("data", &data_t)
|
ctx.Set("data", &data_t)
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ func Router_file(r *gin.RouterGroup) {
|
|||||||
file_info := models.File_info{
|
file_info := models.File_info{
|
||||||
ID: uint(id_int),
|
ID: uint(id_int),
|
||||||
}
|
}
|
||||||
if models.DB.Where(&file_info).First(&file_info).Error == nil {
|
if models.DB.Where("ID=?", file_info.ID).First(&file_info).Error == nil {
|
||||||
Return_file(ctx, &file_info, true)
|
Return_file(ctx, &file_info, true)
|
||||||
} else {
|
} else {
|
||||||
//fmt.Println("not fund")
|
//fmt.Println("not fund")
|
||||||
@@ -53,7 +53,7 @@ func Router_file(r *gin.RouterGroup) {
|
|||||||
file_info := models.File_info{
|
file_info := models.File_info{
|
||||||
ID: uint(id_int),
|
ID: uint(id_int),
|
||||||
}
|
}
|
||||||
if models.DB.Where(&file_info).First(&file_info).Error == nil {
|
if models.DB.Where("ID=?", file_info.ID).First(&file_info).Error == nil {
|
||||||
Return_file(ctx, &file_info, false)
|
Return_file(ctx, &file_info, false)
|
||||||
} else {
|
} else {
|
||||||
//fmt.Println("not fund")
|
//fmt.Println("not fund")
|
||||||
@@ -169,7 +169,7 @@ func Router_file(r *gin.RouterGroup) {
|
|||||||
if fund_file_info2.ID != 0 {
|
if fund_file_info2.ID != 0 {
|
||||||
//fmt.Println(fund_file_info2)
|
//fmt.Println(fund_file_info2)
|
||||||
fund_file_info2.Const += 1
|
fund_file_info2.Const += 1
|
||||||
models.DB.Where(&fund_file_info).Updates(&fund_file_info2)
|
models.DB.Where("ID=?", fund_file_info.ID).Updates(&fund_file_info2)
|
||||||
} else {
|
} else {
|
||||||
fund_file_info.Path = dst
|
fund_file_info.Path = dst
|
||||||
models.DB.Create(&fund_file_info) // 传入指针
|
models.DB.Create(&fund_file_info) // 传入指针
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package routers
|
package routers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"saas/models"
|
"saas/models"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -13,7 +12,7 @@ func Use_login_from_cookie(ctx *gin.Context) {
|
|||||||
//先从缓存获取cookie值
|
//先从缓存获取cookie值
|
||||||
cookie_value, is_have_cookie := ctx.Get("cookie_value")
|
cookie_value, is_have_cookie := ctx.Get("cookie_value")
|
||||||
if is_have_cookie {
|
if is_have_cookie {
|
||||||
fmt.Println(cookie_value)
|
//fmt.Println(cookie_value)
|
||||||
|
|
||||||
var cookie models.Cookie
|
var cookie models.Cookie
|
||||||
cookie.Value = cookie_value.(string)
|
cookie.Value = cookie_value.(string)
|
||||||
|
|||||||
Reference in New Issue
Block a user