修复一些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
+25 -86
View File
@@ -51,6 +51,7 @@ func V1_user_api(r *gin.RouterGroup) {
//转换传进来的数据 //转换传进来的数据
var jsonData From_user_add var jsonData From_user_add
data, is_have_data := ctx.Get("data") data, is_have_data := ctx.Get("data")
//fmt.Println(data)
if is_have_data { if is_have_data {
if err := mapstructure.Decode(data, &jsonData); err == nil { if err := mapstructure.Decode(data, &jsonData); err == nil {
//转换字段 //转换字段
@@ -61,28 +62,33 @@ func V1_user_api(r *gin.RouterGroup) {
Date: time.Now(), Date: time.Now(),
// Date 字段无需赋值,数据库会自动填充默认值 // Date 字段无需赋值,数据库会自动填充默认值
} }
//fmt.Println(newUser) if newUser.Name != "" && newUser.Pass != "" && newUser.Email != "" {
//对用户的密码进行哈希替换 //fmt.Println(newUser)
newUser.Pass = models.Hash_user_pass(newUser.Pass) //对用户的密码进行哈希替换
newUser.Pass = models.Hash_user_pass(newUser.Pass)
//用户名是唯一的,先读取是否有这个用户名 //用户名是唯一的,先读取是否有这个用户名
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 {
//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 { } else {
//fmt.Println("用户不存在") Return_json(ctx, "json_error", nil)
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)
} }
} else { } else {
@@ -320,73 +326,6 @@ func V1_user_api(r *gin.RouterGroup) {
} else { } else {
Return_json(ctx, "user_no_sign", nil) 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,
// })
}) })
} }
+9 -4
View File
@@ -1,6 +1,8 @@
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"
) )
@@ -17,13 +19,16 @@ func Router_api(r *gin.RouterGroup) {
if err := ctx.ShouldBindJSON(&jsonData); err == nil { if err := ctx.ShouldBindJSON(&jsonData); err == nil {
//分离数据 //分离数据
if jsonData["cookie"] != "" { if jsonData["cookie"] != "" && jsonData["cookie"] != nil {
ctx.Set("cookie_value", jsonData["cookie"]) ctx.Set("cookie_value", jsonData["cookie"])
} }
var data_t map[string]interface{} if jsonData["data"] != nil {
if err = mapstructure.Decode(jsonData["data"], &data_t); err == nil { fmt.Println(jsonData["data"])
ctx.Set("data", &data_t) 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) { 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 { if is_login == true {
@@ -74,8 +74,8 @@ func Router_def(r *gin.RouterGroup) {
}) })
r.GET("/warehouses", func(ctx *gin.Context) { 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 { if is_login == true {
total_pages := models.Warehouse_get_total_pages() //获取总页数 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) { r.GET("/warehouses/:id", func(ctx *gin.Context) {
is_login, _ := ctx.Get("is_login") user_info, is_login := ctx.Get("user_info")
user_info, _ := ctx.Get("user_info")
//判断是否登录 //判断是否登录
if is_login == true { if is_login == true {
id := ctx.Param("id") id := ctx.Param("id")
@@ -153,8 +152,8 @@ func Router_def(r *gin.RouterGroup) {
}) })
r.GET("/warehouse/:id", func(ctx *gin.Context) { 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 { if is_login == true {
id := ctx.Param("id") id := ctx.Param("id")
+3 -1
View File
@@ -1,6 +1,7 @@
package routers package routers
import ( import (
"fmt"
"saas/models" "saas/models"
"time" "time"
@@ -12,10 +13,11 @@ 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)
if models.DB.Where(&cookie).First(&cookie).Error == nil { if models.DB.Where(&cookie).First(&cookie).Error == nil {
// 有数据 // 有数据