qnc-server-old/model/response/response.go
2024-09-14 10:48:09 +08:00

94 lines
2.2 KiB
Go

package response
import (
"github.com/gin-gonic/gin"
"net/http"
)
type Response struct {
Code int `json:"code"`
Data interface{} `json:"data"`
Msg string `json:"msg"`
}
const (
SUCCESS = 0 //成功
EMPTY = 1 //为空
SIGN = 2
NOTIFY = 3 //通知
NeedAuth = 4 //需要登录
NeedManualAuth = 5 //需要手动授权登录
Wait = 6 //等待结果
ERROR = 7 //报错
NeedPay = 8 //需要支付
Refund = 9 //查询为空或错误,提示退款
)
func Result(code int, data interface{}, msg string, c *gin.Context) {
c.JSON(http.StatusOK, Response{
code,
data,
msg,
})
}
func Ok(c *gin.Context) {
Result(SUCCESS, map[string]interface{}{}, "操作成功", c)
}
func OkWithMessage(message string, c *gin.Context) {
Result(SUCCESS, map[string]interface{}{}, message, c)
}
func OkWithData(data interface{}, c *gin.Context) {
Result(SUCCESS, data, "查询成功", c)
}
func OkWithDetailed(data interface{}, message string, c *gin.Context) {
Result(SUCCESS, data, message, c)
}
func OkWithEmpty(c *gin.Context) {
Result(EMPTY, map[string]interface{}{}, "查询为空", c)
}
func Fail(c *gin.Context) {
Result(ERROR, map[string]interface{}{}, "操作失败", c)
}
func FailWithMessage(message string, c *gin.Context) {
Result(ERROR, map[string]interface{}{}, message, c)
}
func NoAuth(message string, c *gin.Context) {
c.JSON(http.StatusUnauthorized, Response{
NeedAuth,
nil,
message,
})
}
func ManualAuth(c *gin.Context) {
c.JSON(SUCCESS, Response{
NeedManualAuth,
nil,
"需要手动授权",
})
}
func FailWithDetailed(data interface{}, message string, c *gin.Context) {
Result(ERROR, data, message, c)
}
func OkNeedPay(c *gin.Context) {
Result(NeedPay, map[string]interface{}{}, "请支付", c)
}
func FailRefund(c *gin.Context) {
Result(Refund, map[string]interface{}{}, "无相关记录或系统错误", c)
}
func OkWait(c *gin.Context) {
Result(Wait, map[string]interface{}{}, "提交成功", c)
}
func FailNotify(c *gin.Context) {
Result(NOTIFY, map[string]interface{}{}, "通知", c)
}
func OkNeedSign(c *gin.Context) {
Result(SIGN, map[string]interface{}{}, "需要签名", c)
}