-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgin.go
68 lines (65 loc) · 1.36 KB
/
gin.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
package tool
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"net/url"
"strconv"
)
type Gin struct {
}
func (g Gin) BindJSON(c, req interface{}) error {
ctx, ok := c.(*gin.Context)
if !ok {
return errors.New("类型错误")
}
return ctx.BindJSON(req)
}
func (g Gin) Set(c interface{}, key string, value interface{}) {
ctx, ok := c.(*gin.Context)
if !ok {
return
}
ctx.Set(key, value)
}
func (g Gin) JSON(c, data interface{}) {
ctx, ok := c.(*gin.Context)
if !ok {
return
}
ctx.JSON(http.StatusOK, data)
}
func (g Gin) GetDocParam(ctx interface{}) *docParam {
c, ok := ctx.(*gin.Context)
if !ok {
return nil
}
pageTitle := c.Request.Header.Get("title")
if pageTitle == "" {
fmt.Println("not found title in header")
return nil
}
pageTitle, _ = url.PathUnescape(pageTitle)
//接口没有文件夹和顺序也没关系
catName := c.Request.Header.Get("dir")
if pageTitle != "" {
catName, _ = url.PathUnescape(catName)
}
sNumber, _ := strconv.Atoi(c.Request.Header.Get("number"))
req, _ := c.Get("req")
resp, _ := c.Get("rsp")
method := c.Request.Method
return &docParam{
ApiKey: cli.Doc.ApiKey,
ApiToken: cli.Doc.ApiToken,
CatName: catName,
PageTitle: pageTitle,
PageContent: "",
SNumber: sNumber,
Url: c.Request.URL.String(),
Req: req,
Resp: resp,
Method: method,
}
}