You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
1.8 KiB

package back
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
/*
程序错误、网络错误
*/
const (
ProgramSucCode = "SUCCESS"
)
type BLResultCode string
type subSuc struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
type suc struct {
Code string `json:"code"`
Data subSuc `json:"data"`
}
type subFail struct {
Code string `json:"code"`
Msg string `json:"msg"`
}
type fail struct {
Code string `json:"code"`
Data subFail `json:"data"`
}
// 注:返回列表数据的格式, 前端要求的返回标准格式
type DataList struct {
Total uint `json:"total"`
List interface{} `json:"list"`
}
// ======================================================================================
/*
程序错误时调用该方法返回
*/
func Err(c *gin.Context, msg string) {
if msg == gorm.ErrRecordNotFound.Error() {
msg = "不存在该记录"
} else if msg == "invalid connection" {
msg = "数据库失去连接"
}
c.JSON(
200,
&struct {
Code string `json:"code"`
Msg string `json:"msg"`
}{
Code: "FAIL",
Msg: msg,
},
)
}
/*
处理完全成功调用该方法
*/
func Suc(c *gin.Context, msg string, d interface{}) {
if len(msg) == 0 {
msg = "操作成功"
}
c.JSON(
200,
&suc{
Code: ProgramSucCode,
Data: subSuc{
Code: "SUCCESS",
Msg: msg,
Data: d,
},
},
)
}
/*
业务逻辑处理失败
*/
func Fail(c *gin.Context, msg string) {
c.JSON(
200,
&fail{
Code: "SUCCESS",
Data: subFail{
Code: "FAIL",
Msg: msg,
},
},
)
}
/*
未授权 基本情况登录令牌失效的原因 UNAUTHORIZED
*/
func Unauthorized(c *gin.Context) {
c.JSON(
200,
&fail{
Code: "UNAUTHORIZED",
Data: subFail{
Code: "FAIL",
Msg: "登录失效",
},
},
)
}