123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package response
- import (
- "github.com/gin-gonic/gin"
- )
- var (
- OK_CODE = 200
- FAIL_CODE = 400
- TOKEN_ERROR_CODE = 401
- )
- type ResultData struct {
- Code int `json:"code" description:"状态码"`
- Msg string `json:"msg" description:"提示信息"`
- Data interface{} `json:"data" description:"返回数据"`
- }
- func result(code int, resultData ResultData, c *gin.Context) {
- //jsonByte, _ := json.Marshal(resultData)
- //global.LOG.Debug("resultData:", string(jsonByte))
- c.JSON(code, resultData)
- }
- // 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)
- }
- //token异常
- func TokenError(data interface{}, message string, c *gin.Context) {
- resultData := ResultData{
- Code: TOKEN_ERROR_CODE,
- Msg: message,
- Data: data,
- }
- result(200, resultData, c)
- }
|