修复一些bug

This commit is contained in:
2025-07-14 19:15:40 +08:00
parent 566aade03a
commit 983f21a35e
4 changed files with 44 additions and 99 deletions
+7 -68
View File
@@ -51,6 +51,7 @@ func V1_user_api(r *gin.RouterGroup) {
//转换传进来的数据
var jsonData From_user_add
data, is_have_data := ctx.Get("data")
//fmt.Println(data)
if is_have_data {
if err := mapstructure.Decode(data, &jsonData); err == nil {
//转换字段
@@ -61,6 +62,7 @@ func V1_user_api(r *gin.RouterGroup) {
Date: time.Now(),
// Date 字段无需赋值,数据库会自动填充默认值
}
if newUser.Name != "" && newUser.Pass != "" && newUser.Email != "" {
//fmt.Println(newUser)
//对用户的密码进行哈希替换
newUser.Pass = models.Hash_user_pass(newUser.Pass)
@@ -69,7 +71,7 @@ func V1_user_api(r *gin.RouterGroup) {
var user models.User
user.Name = newUser.Name
if models.DB.Where(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 {
@@ -88,6 +90,10 @@ func V1_user_api(r *gin.RouterGroup) {
} else {
Return_json(ctx, "json_error", nil)
}
} else {
Return_json(ctx, "json_error", nil)
}
} else {
Return_json(ctx, "json_error", nil)
}
@@ -320,73 +326,6 @@ func V1_user_api(r *gin.RouterGroup) {
} 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_, _ := ctx.Get("user")
// user, _ := user_.(*models.User)
// user_find := models.User{
// ID: user.ID,
// }
// 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)
// if user_find.Pass == pass_old {
// new_user := models.User{
// Pass: pass_new,
// }
// //修改密码
// models.DB.Where(&user_find).Updates(&new_user)
// //密码修改后所有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]
// } else {
// err_msg = "user_password_err"
// 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,
// })
})
}
+6 -1
View File
@@ -1,6 +1,8 @@
package routers
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/mitchellh/mapstructure"
)
@@ -17,14 +19,17 @@ func Router_api(r *gin.RouterGroup) {
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
//分离数据
if jsonData["cookie"] != "" {
if jsonData["cookie"] != "" && jsonData["cookie"] != nil {
ctx.Set("cookie_value", jsonData["cookie"])
}
if jsonData["data"] != nil {
fmt.Println(jsonData["data"])
var data_t map[string]interface{}
if err = mapstructure.Decode(jsonData["data"], &data_t); err == nil {
ctx.Set("data", &data_t)
}
}
}
+7 -8
View File
@@ -59,8 +59,8 @@ func Router_def(r *gin.RouterGroup) {
// })
r.GET("/workorders", func(ctx *gin.Context) {
is_login, _ := ctx.Get("is_login")
user_info, _ := ctx.Get("user_info")
user_info, is_login := ctx.Get("user_info")
//判断是否登录
if is_login == true {
@@ -74,8 +74,8 @@ func Router_def(r *gin.RouterGroup) {
})
r.GET("/warehouses", func(ctx *gin.Context) {
is_login, _ := ctx.Get("is_login")
user_info, _ := ctx.Get("user_info")
user_info, is_login := ctx.Get("user_info")
//判断是否登录
if is_login == true {
total_pages := models.Warehouse_get_total_pages() //获取总页数
@@ -103,8 +103,7 @@ func Router_def(r *gin.RouterGroup) {
}
})
r.GET("/warehouses/:id", func(ctx *gin.Context) {
is_login, _ := ctx.Get("is_login")
user_info, _ := ctx.Get("user_info")
user_info, is_login := ctx.Get("user_info")
//判断是否登录
if is_login == true {
id := ctx.Param("id")
@@ -153,8 +152,8 @@ func Router_def(r *gin.RouterGroup) {
})
r.GET("/warehouse/:id", func(ctx *gin.Context) {
is_login, _ := ctx.Get("is_login")
user_info, _ := ctx.Get("user_info")
user_info, is_login := ctx.Get("user_info")
//判断是否登录
if is_login == true {
id := ctx.Param("id")
+3 -1
View File
@@ -1,6 +1,7 @@
package routers
import (
"fmt"
"saas/models"
"time"
@@ -12,10 +13,11 @@ func Use_login_from_cookie(ctx *gin.Context) {
//先从缓存获取cookie值
cookie_value, is_have_cookie := ctx.Get("cookie_value")
if is_have_cookie {
//fmt.Println(cookie_value)
fmt.Println(cookie_value)
var cookie models.Cookie
cookie.Value = cookie_value.(string)
if models.DB.Where(&cookie).First(&cookie).Error == nil {
// 有数据