base_controller.go
5.0 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
package controllers
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
adminuserquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
adminuserservice "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/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 {
return json.Unmarshal(controller.Ctx.Input.RequestBody, v)
}
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)
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
}
newAdminuserquery := adminuserquery.GetAdminUserQuery{Id: userId}
newAdminUserService := adminuserservice.NewAdminUserService(nil)
adminuser, err := newAdminUserService.GetAdminUser(&newAdminuserquery)
if err != nil {
logs.Error("获取用户数据失败:%s", err)
controller.ResponseError(errors.New("无操作权限"))
return false
}
if adminuser.IsDefault {
logs.Debug("用户是超级管理员")
return true
}
if !adminuser.IsUsable {
controller.ResponseError(errors.New("用户被禁用"))
return false
}
for _, v := range adminuser.Permission {
if v.Code == permissionCode {
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)
}