base_controller.go
5.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package controllers
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
userQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/query"
userService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego/protocol"
)
type BaseController struct {
beego.Controller
}
func (controller BaseController) BindJsonData(v interface{}) error {
newDecoder := json.NewDecoder(bytes.NewReader(controller.Ctx.Input.RequestBody))
newDecoder.UseNumber()
err := newDecoder.Decode(v)
return err
}
func (controller BaseController) ResponseError(err error) {
if e, ok := err.(lib.ServiceError); ok {
if !(e.Code == lib.ARG_ERROR || e.Code == lib.BUSINESS_ERROR) {
controller.Data["json"] = protocol.ResponseData{
Code: -1,
Msg: "操作失败",
Data: struct{}{},
}
controller.ServeJSON()
logs.Error(e.Prefix, e.Message)
return
} else {
logs.Error(e.Prefix, e.Message)
}
}
controller.Data["json"] = protocol.ResponseData{
Code: -1,
Msg: err.Error(),
Data: struct{}{},
}
controller.ServeJSON()
logs.Error(err)
return
}
func (controller BaseController) ResponseData(data interface{}) {
if data == nil {
data = struct{}{}
}
controller.Data["json"] = protocol.ResponseData{
Code: 0,
Msg: "ok",
Data: data,
}
controller.ServeJSON()
}
func (controller BaseController) ResponsePageList(data interface{}, totalRow int, pageNumber int) {
if data == nil {
data = []interface{}{}
}
controller.Data["json"] = protocol.ResponseData{
Code: 0,
Msg: "ok",
Data: map[string]map[string]interface{}{
"gridResult": map[string]interface{}{
"lists": data,
"totalRow": totalRow,
"pageNumber": pageNumber,
},
},
}
controller.ServeJSON()
}
//Finish 重写 beego.Controller 的Finish 方法
func (controller *BaseController) Finish() {
strByte, _ := json.Marshal(controller.Data["json"])
length := len(strByte)
if length > 1500 {
logs.Info("<====Send to client: RspBodyData: ", string(strByte[:1500]))
} else {
logs.Info("<====Send to client: RspBodyData: ", string(strByte))
}
}
////Prepare 重写 beego.Controller 的Prepare方法
func (controller *BaseController) Prepare() {
logs.Info("====>Recv Request:", controller.Ctx.Input.URI())
if controller.Ctx.Input.IsPost() || controller.Ctx.Input.IsPut() {
bodyByte := controller.Ctx.Input.RequestBody
if len(bodyByte) > 1000 {
logs.Info("====>Recv data from client: BodyData: ", string(bodyByte[0:1000]))
} else {
logs.Info("====>Recv data from client: BodyData: ", string(bodyByte))
}
}
return
}
func (controller *BaseController) GetHeaderToken() string {
authString := controller.Ctx.Input.Header("Authorization")
//解出需要的jwt串 例:头Authorization:Bearer 123token456
s := strings.Split(authString, "\u0020")
var tokenString string
if len(s) > 0 {
tokenString = s[len(s)-1]
}
return tokenString
}
func (controller *BaseController) ValidJWTToken() bool {
headerToken := controller.GetHeaderToken()
mytoken := new(lib.MyToken)
tokenData, err := mytoken.ValidJWTToken(headerToken)
if err != nil {
logs.Error("校验token失败", err)
if mytoken.IsJwtErrorExpired(err) {
//token超时
controller.Data["json"] = protocol.ResponseData{
Code: 2,
Msg: "token过期,请重新登录",
Data: struct{}{},
}
controller.ServeJSON()
} else {
controller.Data["json"] = protocol.ResponseData{
Code: -1,
Msg: "token校验失败",
Data: struct{}{},
}
controller.ServeJSON()
}
return false
}
controller.setUserId(tokenData.UID)
controller.setUserCompanyId(tokenData.CompanyId)
return true
}
func (controller *BaseController) ValidAdminPermission(permissionCode string, excludeURL ...string) bool {
//排除掉的请求
reqUrl := controller.Ctx.Input.URL()
for i := range excludeURL {
if reqUrl == excludeURL[i] {
return true
}
}
//权限校验
userId := controller.GetUserId()
if userId == 0 {
controller.ResponseError(errors.New("无操作权限"))
return false
}
comanyId := controller.GetUserCompany()
newQuery := userQuery.ValidatePermissionQuery{
UserId: userId,
PermissionCode: permissionCode,
CompanyId: comanyId,
}
newUserService := userService.NewUsersService(nil)
ok, err := newUserService.ValidateAdminpPermission(newQuery)
if err != nil {
logs.Error("用户鉴权失败:%s", err)
controller.ResponseError(errors.New("无操作权限"))
return false
}
if ok {
return true
}
controller.ResponseError(errors.New("无操作权限"))
return false
}
func (controller *BaseController) GetUserId() int64 {
idV := controller.Ctx.Input.GetData("token:admin_user_id")
uid, _ := strconv.ParseInt(fmt.Sprint(idV), 10, 64)
return uid
}
func (controller *BaseController) setUserId(id int64) {
logs.Info("token:admin_user_id = ", id)
controller.Ctx.Input.SetData("token:admin_user_id", id)
}
func (controller *BaseController) setUserCompanyId(id int64) {
logs.Info("token:company_id = ", id)
controller.Ctx.Input.SetData("token:company_id", id)
}
func (controller *BaseController) GetUserCompany() int64 {
idV := controller.Ctx.Input.GetData("token:company_id")
uid, err := strconv.ParseInt(fmt.Sprint(idV), 10, 64)
if err != nil {
logs.Error(err)
}
return uid
}