51 lines
1010 B
Go
51 lines
1010 B
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const (
|
|
codeSuccess = 0
|
|
codeError = 7
|
|
codePasswordChangeRequired = 10001
|
|
)
|
|
|
|
type response struct {
|
|
Code int `json:"code"`
|
|
Data any `json:"data"`
|
|
Msg string `json:"msg"`
|
|
}
|
|
|
|
type pageResult struct {
|
|
List any `json:"list"`
|
|
Total int64 `json:"total"`
|
|
Page int `json:"page"`
|
|
PageSize int `json:"pageSize"`
|
|
}
|
|
|
|
func writeResult(c *gin.Context, code int, data any, message string) {
|
|
c.JSON(http.StatusOK, response{Code: code, Data: data, Msg: message})
|
|
}
|
|
|
|
func ok(c *gin.Context) {
|
|
writeResult(c, codeSuccess, gin.H{}, "操作成功")
|
|
}
|
|
|
|
func okWithData(c *gin.Context, data any) {
|
|
writeResult(c, codeSuccess, data, "成功")
|
|
}
|
|
|
|
func fail(c *gin.Context, message string) {
|
|
writeResult(c, codeError, gin.H{}, message)
|
|
}
|
|
|
|
func noAuth(c *gin.Context, message string) {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, response{
|
|
Code: codeError,
|
|
Data: nil,
|
|
Msg: message,
|
|
})
|
|
}
|