base_controller.go
2.9 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
package controllers
import (
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/json"
"github.com/linmadan/egglib-go/web/beego"
"github.com/linmadan/egglib-go/web/beego/utils"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/middleware"
)
type BaseController struct {
beego.BaseController
}
func (controller *BaseController) ReturnPageListData(count int64, data interface{}, err error, pageNumber int) {
dataMap := map[string]interface{}{
"grid": map[string]interface{}{
"total": count,
//"pageNumber": pageNumber,
"list": data,
},
}
controller.Response(dataMap, err)
}
func (controller *BaseController) ReturnListData(count int64, data interface{}, err error) {
dataMap := map[string]interface{}{
"total": count,
//"pageNumber": pageNumber,
"list": data,
}
controller.Response(dataMap, err)
}
func (controller *BaseController) Response(data interface{}, err error) {
var response utils.JsonResponse
if err != nil {
if _, ok := err.(*application.ServiceError); !ok {
controller.Data["json"] = map[string]interface{}{
"code": 1,
"msg": err.Error(),
}
log.Logger.Error(err.Error())
controller.ServeJSON()
return
}
}
if err != nil {
response = utils.ResponseError(controller.Ctx, err)
} else {
response = utils.ResponseData(controller.Ctx, data)
}
controller.Data["json"] = response
controller.ServeJSON()
}
func (controller *BaseController) GetUserId() int64 {
return 1
}
func (controller *BaseController) GetCompanyId() int64 {
return 1
}
func (controller *BaseController) GetUserBaseId() int64 {
return 1
}
func (controller *BaseController) GetOrgId() int64 {
return 1
}
func (controller *BaseController) GetOperator() domain.Operator {
token := controller.Ctx.Input.Header("X-Mmm-Accesstoken")
loginToken := &domain.LoginToken{}
err := loginToken.ParseToken(token)
if err != nil {
log.Logger.Error(err.Error())
*loginToken, _ = middleware.FormCtxLoginToken(controller.Ctx)
}
if tmpToken, ok := middleware.FormCtxLoginToken(controller.Ctx); ok {
log.Logger.Debug(json.MarshalToString(tmpToken))
}
op := domain.Operator{
UserId: loginToken.UserId,
CompanyId: loginToken.CompanyId,
OrgId: loginToken.OrgId,
UserBaseId: loginToken.UserBaseId,
Phone: loginToken.Account,
Token: token,
OrgIds: loginToken.OrgIds,
}
// TODO:测试数据后期删除
//if op.UserId == 0 {
// op.UserId = 1
// op.CompanyId = 1
// op.OrgId = 1
// op.UserBaseId = 1
//}
// TODO:打印测试日志
log.Logger.Debug("operator " + json.MarshalToString(op))
return op
}
func (controller *BaseController) GetDeviceType() string {
return controller.Ctx.Input.Header("x-mmm-devicetype")
}
func Must(err error) {
if err != nil {
log.Logger.Error(err.Error())
}
}