check_token.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
57
58
59
60
package middlewares
import (
"github.com/beego/beego/v2/server/web/context"
)
func setUserId(userId int64, ctx *context.Context) {
ctx.Input.SetData("_UserId", userId)
}
func GetUserId(ctx *context.Context) int64 {
userId := ctx.Input.GetData("_UserId")
return userId.(int64)
}
func setCompanyId(companyId int64, ctx *context.Context) {
ctx.Input.SetData("_CompanyId", companyId)
}
func GetCompanyId(ctx *context.Context) int64 {
companyId := ctx.Input.GetData("_CompanyId")
return companyId.(int64)
}
func setCompanyType(companyId int, ctx *context.Context) {
ctx.Input.SetData("_CompanyType", companyId)
}
func GetCompanyType(ctx *context.Context) int {
companyId := ctx.Input.GetData("_CompanyType")
return companyId.(int)
}
func invalidOrExpired(ctx *context.Context) {
resp := map[string]interface{}{
"code": 902,
"msg": "Authorization过期或无效,需要进行重新获取令牌",
}
_ = ctx.Output.JSON(resp, false, false)
}
func CheckToken() func(ctx *context.Context) {
return func(ctx *context.Context) {
tokenStr := ctx.Input.Header("x-mmm-accesstoken")
if tokenStr == "" { //没有带token
invalidOrExpired(ctx)
return
}
//userServe := service.UserService{}
//userTk, err := userServe.ValidLoginToken(tokenStr)
//if err != nil {
// invalidOrExpired(ctx)
// return
//}
//setUserId(userTk.UserId, ctx)
//setCompanyId(userTk.CompanyId, ctx)
//setCompanyType(userTk.CompanyType, ctx)
}
}