/
index.go
81 lines (63 loc) · 2.04 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package response
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
)
const (
ResponseOkMessage = "操作成功"
ResponseErrorMessage = "操作失败"
)
type Response struct {
Code int `json:"status"`
Data interface{} `json:"data"`
Msg string `json:"message"`
}
func Result(code int, data interface{}, msg string, ctx *gin.Context) {
ctx.JSON(http.StatusOK, Response{code, data, msg})
}
func Ok(ctx *gin.Context) {
Result(http.StatusOK, map[string]interface{}{}, ResponseOkMessage, ctx)
}
func OkWithMessage(message string, ctx *gin.Context) {
Result(http.StatusOK, map[string]interface{}{}, message, ctx)
}
func OkWithData(data interface{}, ctx *gin.Context) {
Result(http.StatusOK, data, ResponseOkMessage, ctx)
}
func OkWithDetailed(data interface{}, message string, ctx *gin.Context) {
Result(http.StatusOK, data, message, ctx)
}
func Fail(ctx *gin.Context) {
Result(http.StatusBadRequest, map[string]interface{}{}, ResponseErrorMessage, ctx)
}
func UnauthorizedFailWithMessage(message string, ctx *gin.Context) {
Result(http.StatusUnauthorized, map[string]interface{}{}, message, ctx)
}
func UnauthorizedFailWithDetailed(data interface{}, message string, ctx *gin.Context) {
Result(http.StatusUnauthorized, data, message, ctx)
}
func ForbiddenFailWithMessage(message string, ctx *gin.Context) {
Result(http.StatusForbidden, map[string]interface{}{}, message, ctx)
}
func FailWithMessage(message string, ctx *gin.Context) {
Result(http.StatusBadRequest, map[string]interface{}{}, message, ctx)
}
func FailWithDetailed(data interface{}, message string, ctx *gin.Context) {
Result(http.StatusBadRequest, data, message, ctx)
}
type PageResult struct {
List interface{} `json:"list"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
}
type BaseResponse struct {
Id uint `json:"id"`
CreatedAt *time.Time `json:"createdAt"`
UpdatedAt *time.Time `json:"updatedAt"`
}
type SelectOption struct {
Id uint `json:"id"`
Name string `json:"name"`
}