package common import ( "bytes" "fmt" "github.com/gin-gonic/gin" "net/http" ) type ResponseJson struct { Code uint `yaml:"code" json:"code"` // response code Message string `yaml:"message" json:"message"` // response message Data interface{} `yaml:"data" json:"data,omitempty"` // response data } /** 生成Response */ func GenResp(c *gin.Context, code int, message string, data interface{}) { if data != nil { c.JSON(http.StatusOK, gin.H{ "code": code, "message": message, "data": data, }) } else { c.JSON(http.StatusOK, gin.H{ "code": code, "message": message, }) } } /** 生成带错误信息的Response */ func GenMessageResp(c *gin.Context, code int, msg string, data interface{}) { if data != nil { c.JSON(http.StatusOK, gin.H{ "code": code, "message": msg, "data": data, }) } else { c.JSON(http.StatusOK, gin.H{ "code": code, "message": msg, }) } } // StringsJoin string array join func StringsJoin(strs ...string) string { var str string var b bytes.Buffer strsLen := len(strs) if strsLen == 0 { return str } for i := 0; i < strsLen; i++ { b.WriteString(strs[i]) } str = b.String() return str } func Join2String(split string, strs ...interface{}) string { var str string var b bytes.Buffer strsLen := len(strs) if strsLen == 0 { return str } for i := 0; i < strsLen; i++ { var str interface{} switch str.(type) { case string: b.WriteString(str.(string)) case int: b.WriteString(fmt.Sprintf("%d", str.(int))) case int64: b.WriteString(fmt.Sprintf("%d", str.(int))) } } str = b.String() return str } func LogError(msg string) { if LoggerInfo.Open { Logger.Error(msg) } } func LogInfo(msg string) { if LoggerInfo.Open { Logger.Info(msg) } } func LogDebug(msg string) { if LoggerInfo.Open { Logger.Debug(msg) } } func LogWarn(msg string) { if LoggerInfo.Open { Logger.Warn(msg) } } func LogSync() error { if LoggerInfo.Open { return Logger.Sync() } return nil }