forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package stdLog
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"reflect"
|
||||
"stat/common/log"
|
||||
"stat/common/utils"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
//ServeJSON 返回数据并处理多语言
|
||||
func ServerJSON(c *gin.Context, code CodeType, data interface{}) {
|
||||
if code != Success { //发生错误,记录日志
|
||||
var version, sysType, devType string
|
||||
admin, _ := utils.GetAdmin(c)
|
||||
|
||||
log.Warn("Error:",
|
||||
log.Any("admin", admin),
|
||||
log.Any("IP", c.ClientIP()),
|
||||
log.Any("Version", version),
|
||||
log.Any("SysType", sysType),
|
||||
log.Any("DevType", devType),
|
||||
log.Any("Router", c.Request.RequestURI),
|
||||
log.Any("PID", os.Getpid()),
|
||||
log.Any("Error", data),
|
||||
log.Any("Code", code))
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": code,
|
||||
"msg": code.Msg(),
|
||||
"tip": code.Tip(),
|
||||
"data": data,
|
||||
"time": time.Now().UTC().Format("2006-01-02T15:04:05.000Z"),
|
||||
})
|
||||
} else {
|
||||
if data != nil {
|
||||
t := reflect.TypeOf(data)
|
||||
if !(t.Kind() == reflect.Map || t.Kind() == reflect.Struct) {
|
||||
//log.Warn("[===TypeError===] Return Data Type error is not struct", log.Any("path", c.Request.URL.Path))
|
||||
}
|
||||
}
|
||||
// 返回数据加密
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": code,
|
||||
"msg": code.Msg(),
|
||||
"tip": code.Tip(),
|
||||
"data": data,
|
||||
"time": time.Now().UTC().Format("2006-01-02T15:04:05.000Z"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//GetUID 获取用户uid
|
||||
//oauth.Auth执行后uid有效
|
||||
func GetUID(ctx *gin.Context) (uid uint64, err error) {
|
||||
val, exists := ctx.Get("USER_ID")
|
||||
if !exists {
|
||||
err = errors.New("userId not exist")
|
||||
return
|
||||
}
|
||||
uid, ok := val.(uint64)
|
||||
if !ok {
|
||||
return 0, errors.New("userId type error")
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user