数据转换

This commit is contained in:
2025-11-12 16:05:53 +08:00
parent c60c23c0d1
commit 58fce7ec2a
7 changed files with 50 additions and 26 deletions
+17
View File
@@ -0,0 +1,17 @@
package models
import "time"
type APIRequestLog struct {
ID int64 `gorm:"primaryKey;column:id" json:"id"`
IPAddress string `gorm:"column:ip_address;size:45;not null" json:"ip_address"`
Path string `gorm:"column:path;size:500;not null" json:"path"`
Method string `gorm:"column:method;size:10;not null" json:"method"`
StatusCode int `gorm:"column:status_code;index" json:"status_code"`
Message string `gorm:"column:error_message;type:text" json:"error_message"`
CreatedAt time.Time `gorm:"column:created_at;default:CURRENT_TIMESTAMP" json:"created_at"`
}
func LogAdd() {
}
+22 -21
View File
@@ -6,7 +6,6 @@ import (
"os"
"github.com/gin-gonic/gin"
"github.com/mitchellh/mapstructure"
)
var ErrorCode map[string]interface{}
@@ -26,28 +25,30 @@ func init() {
}
func ApiRoot(r *gin.RouterGroup) {
r.Use(func(ctx *gin.Context) {
//转换传进来的数据
var jsonData map[string]interface{}
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
//分离数据
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)
}
}
// 把数据分离成cookie和json
func SeparateData(ctx *gin.Context) (map[string]interface{}, string) {
var jsonData map[string]interface{}
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
//分离数据
cookie, ok := jsonData["cookie"].(string)
if !ok {
cookie = ""
}
})
data, ok := jsonData["data"].(map[string]interface{})
if !ok {
data = nil
}
return data, cookie
}
return nil, ""
}
func ApiRoot(r *gin.RouterGroup) {
ApiUser(r.Group("/users"))
+3 -2
View File
@@ -65,9 +65,10 @@ func ApiUser(r *gin.RouterGroup) {
r.POST("/register", func(ctx *gin.Context) {
//转换传进来的数据
var jsonData From_user_add
data, isHaveData := ctx.Get("data")
if isHaveData {
data, _ := SeparateData(ctx)
if data != nil {
if err := mapstructure.Decode(data, &jsonData); err == nil {
//转换字段
newUser := models.TabUser_{
+1 -1
View File
@@ -19,6 +19,6 @@ func ReturnJson(ctx *gin.Context, errMsg string, data map[string]interface{}) {
ctx.JSON(200, &returnData)
ctx.Abort()
//ctx.Abort()
}
+2 -1
View File
@@ -36,7 +36,8 @@
"already_have_an_account": "Already have an account?",
"network_err":"Network error",
"username_dup":"Duplicate username",
"registration_successful":"Registration successful!"
"registration_successful":"Registration successful!",
"server_error":"Server Error"
},
"button": {
"submit": "Submit",
+2 -1
View File
@@ -36,7 +36,8 @@
"already_have_an_account":"已经有账户了?",
"network_err":"网络错误",
"username_dup":"用户名重复",
"registration_successful":"注册成功!"
"registration_successful":"注册成功!",
"server_error":"服务端错误"
},
"button": {
"submit": "提交",
@@ -93,6 +93,9 @@ function createAccount() {
}
);
break;
default:
mos.value?.showAlert("danger", t("message.server_error"), 5000);
break;
}
break;
default: