数据转换
This commit is contained in:
@@ -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
@@ -6,7 +6,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/mitchellh/mapstructure"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrorCode map[string]interface{}
|
var ErrorCode map[string]interface{}
|
||||||
@@ -26,28 +25,30 @@ func init() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ApiRoot(r *gin.RouterGroup) {
|
// 把数据分离成cookie和json
|
||||||
|
func SeparateData(ctx *gin.Context) (map[string]interface{}, string) {
|
||||||
r.Use(func(ctx *gin.Context) {
|
var jsonData map[string]interface{}
|
||||||
//转换传进来的数据
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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"))
|
ApiUser(r.Group("/users"))
|
||||||
|
|
||||||
|
|||||||
@@ -65,9 +65,10 @@ func ApiUser(r *gin.RouterGroup) {
|
|||||||
r.POST("/register", func(ctx *gin.Context) {
|
r.POST("/register", func(ctx *gin.Context) {
|
||||||
//转换传进来的数据
|
//转换传进来的数据
|
||||||
var jsonData From_user_add
|
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 {
|
if err := mapstructure.Decode(data, &jsonData); err == nil {
|
||||||
//转换字段
|
//转换字段
|
||||||
newUser := models.TabUser_{
|
newUser := models.TabUser_{
|
||||||
|
|||||||
@@ -19,6 +19,6 @@ func ReturnJson(ctx *gin.Context, errMsg string, data map[string]interface{}) {
|
|||||||
|
|
||||||
ctx.JSON(200, &returnData)
|
ctx.JSON(200, &returnData)
|
||||||
|
|
||||||
ctx.Abort()
|
//ctx.Abort()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,8 @@
|
|||||||
"already_have_an_account": "Already have an account?",
|
"already_have_an_account": "Already have an account?",
|
||||||
"network_err":"Network error",
|
"network_err":"Network error",
|
||||||
"username_dup":"Duplicate username",
|
"username_dup":"Duplicate username",
|
||||||
"registration_successful":"Registration successful!"
|
"registration_successful":"Registration successful!",
|
||||||
|
"server_error":"Server Error"
|
||||||
},
|
},
|
||||||
"button": {
|
"button": {
|
||||||
"submit": "Submit",
|
"submit": "Submit",
|
||||||
|
|||||||
@@ -36,7 +36,8 @@
|
|||||||
"already_have_an_account":"已经有账户了?",
|
"already_have_an_account":"已经有账户了?",
|
||||||
"network_err":"网络错误",
|
"network_err":"网络错误",
|
||||||
"username_dup":"用户名重复",
|
"username_dup":"用户名重复",
|
||||||
"registration_successful":"注册成功!"
|
"registration_successful":"注册成功!",
|
||||||
|
"server_error":"服务端错误"
|
||||||
},
|
},
|
||||||
"button": {
|
"button": {
|
||||||
"submit": "提交",
|
"submit": "提交",
|
||||||
|
|||||||
@@ -93,6 +93,9 @@ function createAccount() {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
mos.value?.showAlert("danger", t("message.server_error"), 5000);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user