-
Notifications
You must be signed in to change notification settings - Fork 3
/
admin.go
79 lines (65 loc) · 1.75 KB
/
admin.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
package main
import (
"fmt"
"net/http"
"strconv"
"github.com/DoloresTeam/organization"
"github.com/gin-gonic/gin"
)
const (
department = `Department`
member = `Member`
)
// CORSMiddleware ...
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
if c.Request.Method == "OPTIONS" {
fmt.Println("OPTIONS")
c.AbortWithStatus(200)
} else {
c.Next()
}
}
}
func findPageControl(c *gin.Context) (page uint32, pageSize uint32, cookie []byte) {
p, e := strconv.Atoi(c.DefaultQuery(`pageSize`, `25`))
if e != nil {
p = 100
}
pageSize = uint32(p)
p0, e := strconv.Atoi(c.DefaultQuery(`page`, `1`))
if e != nil {
p0 = 1
}
page = uint32(p0)
if page == 0 {
page = 1
}
cookie = []byte(c.DefaultQuery(`pageCookie`, ``))
return
}
func sendResult(c *gin.Context, r *organization.SearchResult) {
page, pageSize, _ := findPageControl(c)
total := (page-1)*pageSize + uint32(len(r.Data))
if r.Cookie != nil {
total += pageSize
}
c.JSON(http.StatusOK, map[string]interface{}{
`pageSize`: r.Size,
`total`: total,
`cookie`: string(r.Cookie),
`data`: r.Data,
`page`: page,
})
}
func sendError(c *gin.Context, e error) {
c.JSON(http.StatusInternalServerError, map[string]string{
`message`: e.Error(),
})
}