package resp import ( "encoding/json" "fmt" "github.com/gin-gonic/gin" "hongze/fms_api/global" "strings" ) var ( OK_CODE = 200 //业务成功 FAIL_CODE = 400 //业务错误 TOKEN_ERROR_CODE = 401 //toke异常 NO_AUTH = 403 //没有权限 SPECIFIC_FAIL_CODE = 4001 // 业务指定错误 HASFORBIDDEN_CODE = 4010 // 管理员账号被禁用 PASSWORDCHANGE_CODE = 4011 // 管理员账号被禁用 ) type ResultData struct { Code int `json:"code" description:"状态码"` Msg string `json:"msg" description:"提示信息"` Data interface{} `json:"data" description:"返回数据"` ErrMsg string `json:"errMsg" description:"错误信息,不用返回给前端,只是做日志记录"` } func result(code int, resultData ResultData, c *gin.Context) { jsonByte, _ := json.Marshal(resultData) token := c.Request.Header.Get("Authorization") if token == "" { token = c.DefaultQuery("authorization", "") if token == "" { token = c.DefaultQuery("Authorization", "") } } logSlice := make([]string, 0) logSlice = append(logSlice, fmt.Sprint("Url:", c.Request.RequestURI)) logSlice = append(logSlice, fmt.Sprint("Token:", token)) logSlice = append(logSlice, fmt.Sprint("resultData:", string(jsonByte))) //记录错误日志 if resultData.ErrMsg != "" { logSlice = append(logSlice, fmt.Sprint("ErrMsg:", resultData.ErrMsg)) global.LOG.Info(strings.Join(logSlice, ";")) } // 测试环境不加密 //if global.CONFIG.Serve.RunMode == "debug" { c.JSON(code, resultData) /*} else { global.LOG.Info(strings.Join(logSlice, ";")) encryptResult := utils.DesBase64Encrypt(jsonByte) c.JSON(code, string(encryptResult)) }*/ c.Abort() } // OK 操作成功 func Ok(msg string, c *gin.Context) { resultData := ResultData{ Code: OK_CODE, Msg: msg, } result(200, resultData, c) } // OkData 成功返回数据 func OkData(msg string, data interface{}, c *gin.Context) { resultData := ResultData{ Code: OK_CODE, Msg: msg, Data: data, } result(200, resultData, c) } // Fail 操作失败 func Fail(msg string, c *gin.Context) { resultData := ResultData{ Code: FAIL_CODE, Msg: msg, } result(200, resultData, c) } // FailData 成功返回数据 func FailData(msg string, data interface{}, c *gin.Context) { resultData := ResultData{ Code: FAIL_CODE, Msg: msg, Data: data, } result(200, resultData, c) } // Custom 自定义状态码+操作成功 func Custom(code int, msg string, c *gin.Context) { resultData := ResultData{ Code: code, Msg: msg, } result(200, resultData, c) } // CustomData 自定义状态码+返回数据 func CustomData(code int, msg string, data interface{}, c *gin.Context) { resultData := ResultData{ Code: code, Msg: msg, Data: data, } result(200, resultData, c) } // TokenError token异常 func TokenError(data interface{}, message, errMsg string, c *gin.Context) { resultData := ResultData{ Code: TOKEN_ERROR_CODE, Msg: message, Data: data, ErrMsg: errMsg, } result(200, resultData, c) } // AuthError 没有权限 func AuthError(data interface{}, message string, c *gin.Context) { resultData := ResultData{ Code: NO_AUTH, Msg: message, Data: data, } result(200, resultData, c) } // SpecificFail 业务指定错误 func SpecificFail(code int, data interface{}, message string, c *gin.Context) { resultData := ResultData{ Code: code, Msg: message, Data: data, } result(200, resultData, c) } // FailMsg 操作失败 func FailMsg(msg, errMsg string, c *gin.Context) { resultData := ResultData{ Code: FAIL_CODE, Msg: msg, ErrMsg: errMsg, } result(200, resultData, c) }