base_controller.go
3.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
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
package controllers
import (
"fmt"
"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"
"io"
"path/filepath"
)
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,
}
// 打印测试日志
log.Logger.Debug("operator " + json.MarshalToString(op))
return op
}
func (controller *BaseController) GetDeviceType() string {
return controller.Ctx.Input.Header("x-mmm-devicetype")
}
func (controller *BaseController) BodyKeys() map[string]interface{} {
var bodyKV map[string]interface{}
controller.Unmarshal(&bodyKV)
return bodyKV
}
func (controller *BaseController) GetExcelFile() (io.Reader, error) {
excelFile, fileHeader, err := controller.GetFile("file")
if err != nil {
log.Logger.Error(err.Error())
return nil, fmt.Errorf("上传文件不存在")
}
ext := filepath.Ext(fileHeader.Filename)
if !(ext == ".xlsx" || ext == ".xls" || ext == ".csv") {
return nil, fmt.Errorf("仅支持上传文件格式 xls/xlsx/csv")
}
return excelFile, nil
}
func (controller *BaseController) GetFileWithExt() (io.Reader, string, error) {
excelFile, fileHeader, err := controller.GetFile("file")
if err != nil {
log.Logger.Error(err.Error())
return nil, "", fmt.Errorf("上传文件不存在")
}
ext := filepath.Ext(fileHeader.Filename)
if !(ext == ".xlsx" || ext == ".xls" || ext == ".csv") {
return nil, "", fmt.Errorf("仅支持上传文件格式 xls/xlsx/csv")
}
return excelFile, ext, nil
}
func Must(err error) {
if err != nil {
log.Logger.Error(err.Error())
}
}