base.go
1.4 KB
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
package controllers
import (
"github.com/astaxie/beego/validation"
"github.com/gin-gonic/gin"
"github.com/tiptok/gocomm/pkg/mygin"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
"strconv"
)
type BaseController struct {
mygin.BaseController
}
func (this *BaseController) Resp(c *gin.Context, httpCode int, rsp *protocol.ResponseMessage) {
c.Set("outputData", rsp)
c.JSON(httpCode, rsp)
//c.Abort()
}
func (controller *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) {
/*校验*/
var err error
valid := validation.Validation{}
result, err = valid.Valid(obj)
if err != nil {
}
if !result {
msg = protocol.BadRequestParam(2)
return
}
return
}
//获取请求头信息
func (this *BaseController) GetRequestHeader(c *gin.Context) *protocol.RequestHeader {
h := &protocol.RequestHeader{}
h.AccessToken = c.Query("x-mmm-accesstoken")
h.AppProject = c.Query("x-mmm-appproject")
h.DeviceType, _ = strconv.Atoi(c.Query("x-mmm-devicetype"))
h.Sign = c.Query("x-mmm-sign")
h.Uuid = c.Query("x-mmm-uuid")
h.TimeStamp = c.Query("x-mmm-timestamp")
h.Version = c.Query("x-mmm-version")
h.UserId, _ = strconv.ParseInt(c.Query("x-mmm-id"), 10, 64)
if len(h.AccessToken) > 0 {
if claim, err := utils.ParseJWTToken(h.AccessToken); err == nil {
h.UserId = claim.UserId
}
}
return h
}