数据转换
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"
|
||||
|
||||
"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"))
|
||||
|
||||
|
||||
@@ -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_{
|
||||
|
||||
@@ -19,6 +19,6 @@ func ReturnJson(ctx *gin.Context, errMsg string, data map[string]interface{}) {
|
||||
|
||||
ctx.JSON(200, &returnData)
|
||||
|
||||
ctx.Abort()
|
||||
//ctx.Abort()
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user