91 lines
1.7 KiB
Go
91 lines
1.7 KiB
Go
package result
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"mc-client-updater-server/pkg/common"
|
|
"net/http"
|
|
)
|
|
|
|
type Result struct {
|
|
ctx *gin.Context
|
|
}
|
|
|
|
type Root struct {
|
|
Code int `json:"code"`
|
|
Msg string `json:"msg"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
func NewResult(c *gin.Context) *Result {
|
|
return &Result{ctx: c}
|
|
}
|
|
|
|
func (r *Result) Success(data interface{}) {
|
|
if data == nil {
|
|
data = gin.H{}
|
|
}
|
|
res := Root{
|
|
Code: http.StatusOK,
|
|
Msg: "",
|
|
Data: data,
|
|
}
|
|
r.ctx.JSON(http.StatusOK, res)
|
|
r.ctx.Next()
|
|
}
|
|
|
|
func (r *Result) Fail(code int, msg string) {
|
|
res := Root{
|
|
Code: code,
|
|
Msg: msg,
|
|
Data: nil,
|
|
}
|
|
r.ctx.JSON(http.StatusOK, res)
|
|
r.ctx.Abort()
|
|
}
|
|
|
|
//func (r *Result) Redirect302(location string) {
|
|
// r.ctx.Header("Location", location)
|
|
// r.ctx.Status(http.StatusFound)
|
|
// r.ctx.Next()
|
|
//}
|
|
|
|
func (r *Result) InternalServerError(msg string) {
|
|
r.Fail(http.StatusInternalServerError, msg)
|
|
}
|
|
|
|
func (r *Result) BadRequest() {
|
|
r.Fail(http.StatusBadRequest, "请求参数错误")
|
|
}
|
|
|
|
func (r *Result) BadRequestWithMsg(msg string) {
|
|
r.Fail(http.StatusBadRequest, msg)
|
|
}
|
|
|
|
func (r *Result) LoginError() {
|
|
r.Fail(common.LoginErrorCode, "账号或密码错误")
|
|
}
|
|
|
|
func (r *Result) UnLogin() {
|
|
r.Fail(http.StatusUnauthorized, "未登录")
|
|
}
|
|
|
|
func (r *Result) Unauthorized() {
|
|
r.Fail(http.StatusUnauthorized, "未授权")
|
|
}
|
|
|
|
func (r *Result) NoPermission() {
|
|
r.Fail(common.NoPermission, "权限不足")
|
|
}
|
|
|
|
func (r *Result) LoginExpired() {
|
|
r.Fail(common.LoginExpired, "登录过期")
|
|
}
|
|
|
|
func (r *Result) DuplicatedValue(msg string) {
|
|
r.Fail(common.DuplicatedValue, msg)
|
|
}
|
|
|
|
func (r *Result) InvalidInstance(instName string) {
|
|
r.Fail(common.InvalidInstance, "指定的实例不存在:"+instName)
|
|
}
|