response.go 1.97 KB
Newer Older
wangp's avatar
wangp committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package base

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

/*
{
	"code": 10000, // 程序中的错误码
	"msg": xx,     // 提示信息
	"data": {},    // 数据
}

*/

// ResponseData 统一返回数据
wangp's avatar
wangp committed
18 19 20 21 22
//type ResponseData struct {
//	Code int         `json:"code"`
//	Msg  interface{} `json:"msg"`
//	Data interface{} `json:"data,omitempty"`
//}
wangp's avatar
wangp committed
23
type ResponseData struct {
wangp's avatar
wangp committed
24 25 26
	Status  int         `json:"Status"`
	Message interface{} `json:"Message"`
	Data    interface{} `json:"Data,omitempty"`
wangp's avatar
wangp committed
27 28 29
}

// ResponseErrorWithMsg 返回错误
wangp's avatar
wangp committed
30 31 32 33 34 35 36
//func ResponseErrorWithMsg(c *gin.Context, code int) {
//	c.JSON(http.StatusOK, &ResponseData{
//		Code: code,
//		Msg:  InternationalizedMsg(c, code),
//		Data: nil,
//	})
//}
wangp's avatar
wangp committed
37 38
func ResponseErrorWithMsg(c *gin.Context, code int) {
	c.JSON(http.StatusOK, &ResponseData{
wangp's avatar
wangp committed
39 40
		Status: code,
		Message:  InternationalizedMsg(c, code),
wangp's avatar
wangp committed
41 42 43 44 45
		Data: nil,
	})
}

// ResponseSuccess 返回正确
wangp's avatar
wangp committed
46 47 48 49 50 51 52
//func ResponseSuccess(c *gin.Context, data interface{}) {
//	c.JSON(http.StatusOK, &ResponseData{
//		Code: Success,
//		Msg:  InternationalizedMsg(c, Success),
//		Data: data,
//	})
//}
wangp's avatar
wangp committed
53 54
func ResponseSuccess(c *gin.Context, data interface{}) {
	c.JSON(http.StatusOK, &ResponseData{
wangp's avatar
wangp committed
55 56
		Status: Success,
		Message:  InternationalizedMsg(c, Success),
wangp's avatar
wangp committed
57 58 59 60 61
		Data: data,
	})
}

// ResponseErrorMsg 直接返回msg
wangp's avatar
wangp committed
62 63 64 65 66 67 68
//func ResponseErrorMsg(c *gin.Context, msg string) {
//	c.JSON(http.StatusOK, &ResponseData{
//		Code: Warning,
//		Msg:  msg,
//		Data: nil,
//	})
//}
wangp's avatar
wangp committed
69 70
func ResponseErrorMsg(c *gin.Context, msg string) {
	c.JSON(http.StatusOK, &ResponseData{
wangp's avatar
wangp committed
71 72
		Status: Warning,
		Message:  msg,
wangp's avatar
wangp committed
73 74
		Data: nil,
	})
75 76 77 78 79 80 81 82 83 84 85 86
}

// ResponseDataWxNotice 微信回调后 处理完逻辑返回给微信的信息
type ResponseDataWxNotice struct {
	Code    string `json:"code"` //SUCCESS/FAIL
	Message string `json:"message"`  //执行成功/错误原因
}
func ResponseWxNotice(c *gin.Context, data *ResponseDataWxNotice) {
	c.JSON(http.StatusOK, &ResponseDataWxNotice{
		Code:    data.Code,
		Message: data.Message,
	})
wangp's avatar
wangp committed
87
}