package common import ( "errors" "fmt" "net/http" "os" "time" "videoweb/pub/common/enum/consts" "videoweb/pub/common/ipaddr" "videoweb/pub/common/log" "videoweb/pub/common/srvdef" "videoweb/pub/common/useragent" "strconv" "github.com/gin-gonic/gin" ) var ( cryptSecret string env consts.RunMod //routineCount int64 ) // InitGinSecret InitResponseParam func InitGinSecret(secret string, env_ consts.RunMod) { cryptSecret = secret env = env_ } // JSONWriterCache 返回数据并标记需要写入缓存 func JSONWriterCache(c *gin.Context, codeOrErr srvdef.ErrInfoer, data interface{}) { code := codeOrErr.GetCode() tip := codeOrErr.GetTip() msg := codeOrErr.GetMsg() var hash bool if code != srvdef.Success { //发生错误,记录日志 var version, sysType, devType string id := GetUserId(c) ua, _ := GetUA(c) if ua.Ver != "" { version = ua.Ver } if ua.SysType != "" { sysType = ua.SysType } if devType != "" { devType = ua.DevType } log.Warn("Error:", log.Any("UID", strconv.FormatInt(id, 10)), log.Any("IP", ipaddr.ResolveClientIP(c)), log.Any("Version", version), log.Any(consts.CtxSysType, 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.Header("writer-cache", "HIT") c.JSON(http.StatusOK, gin.H{ "code": code, "hash": hash, "msg": msg, "tip": tip, "data": data, "time": time.Now().UTC().Format("2006-01-02T15:04:05.000Z"), }) } // ServerJSON 返回数据并处理多语言 func ServerJSON(c *gin.Context, codeOrErr srvdef.ErrInfoer, data interface{}) { code := codeOrErr.GetCode() tip := codeOrErr.GetTip() msg := codeOrErr.GetMsg() var hash bool if code != srvdef.Success { //发生错误,记录日志 var version, sysType, devType string id := GetUserId(c) ua, _ := GetUA(c) if ua.Ver != "" { version = ua.Ver } if ua.SysType != "" { sysType = ua.SysType } if devType != "" { devType = ua.DevType } log.Warn("Error:", log.Any("UID", strconv.FormatInt(id, 10)), log.Any("IP", ipaddr.ResolveClientIP(c)), log.Any("Version", version), log.Any(consts.CtxSysType, 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, "hash": hash, "msg": msg, "tip": 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(consts.CtxUserID) if !exists { err = errors.New("userId not exist") return } uid, ok := val.(uint64) if !ok { return 0, errors.New("userId type error") } return } func GetUserId(ctx *gin.Context) int64 { return ctx.GetInt64(consts.CtxUserID) } // GetUA func GetUA(ctx *gin.Context) (u useragent.UA, err error) { val, exists := ctx.Get(consts.CtxUA) if !exists { err = errors.New("user-agent not exists") return } u, ok := val.(useragent.UA) if !ok { err = errors.New("user-agent type error") return } return u, nil } // GetIP 获取真实IP func GetIP(ctx *gin.Context) string { relIP, exists := ctx.Get(consts.CtxIP) if !exists { if relIP == "" { relIP = ipaddr.ResolveClientIP(ctx) } } v, ok := relIP.(string) if !ok { return "" } log.Info(fmt.Sprintf("[IP-ROUTER] %s,PID %d", ctx.GetHeader("X-Forwarded-For"), os.Getpid())) return v } func IsNilOrEmpty(in interface{}) bool { if in == nil || in == "" { return true } return false } func AlphabeticColl() []string { return []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"} }