base.go
3.7 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
package controllers
import (
"encoding/json"
"fmt"
"oppmg/common/log"
"oppmg/protocol"
"oppmg/utils/exceltool"
"strconv"
serveauth "oppmg/services/auth"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/validation"
)
//BaseController 基础
type BaseController struct {
beego.Controller
}
//Prepare 实现beego.ControllerInterface 的接口
func (this *BaseController) Prepare() {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
if this.Ctx.Input.Method() == "OPTIONS" {
this.Ctx.ResponseWriter.WriteHeader(204)
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
this.Ctx.WriteString("")
return
}
p := fmt.Sprint(this.Ctx.Input.GetData("RouterPattern"))
userid := this.GetUserId()
companyid := this.GetCompanyId()
userCompanyId := this.GetUserCompanyId()
ok := serveauth.ValidUserPermission(p, userid, companyid, userCompanyId)
if !ok {
msg := protocol.NewMessage("10210")
this.ResposeJson(msg)
return
}
//权限校验
return
}
func (this *BaseController) GetAppHead() (appHead protocol.BaseHeader) {
appHead.AccessToken = this.Ctx.Input.Header(protocol.HeaderAccessToken)
return
}
//Finish 实现beego.ControllerInterface 的接口
func (this *BaseController) Finish() {
strByte, _ := json.Marshal(this.Data["json"])
length := len(strByte)
if length > 1000 {
log.Info(fmt.Sprintf("<====Send to client: RspBodyData: %s......", string(strByte[:1000])))
} else {
log.Info(fmt.Sprintf("<====Send to client: RspBodyData: %s", string(strByte)))
}
}
func (this *BaseController) ResposeJson(msg *protocol.ResponseMessage) {
this.Data["json"] = msg
this.ServeJSON()
}
func (this *BaseController) GetCompanyId() int64 {
v := this.Ctx.Input.GetData(protocol.HeaderCompanyid)
companyid, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64)
if beego.BConfig.RunMode != "prod" && companyid == 0 {
companyid, _ = strconv.ParseInt(this.Ctx.Input.Header("x-mmm-cid"), 10, 64)
}
return companyid
}
func (this *BaseController) GetUserId() int64 {
v := this.Ctx.Input.GetData(protocol.HeaderUserid)
userid, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64)
if beego.BConfig.RunMode != "prod" && userid == 0 {
userid, _ = strconv.ParseInt(this.Ctx.Input.Header("x-mmm-uid"), 10, 64)
}
return userid
}
func (this *BaseController) GetUserCompanyId() int64 {
v := this.Ctx.Input.GetData(protocol.HeaderUCompanyid)
userid, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64)
return userid
}
//Valid valid struct
func (this *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) {
/*校验*/
var err error
valid := validation.Validation{}
result, err = valid.Valid(obj)
if err != nil {
log.Error("参数校验失败:%s", err)
msg = protocol.BadRequestParam("1")
return
}
if !result {
log.Error("校验结果:%v", result)
msg = protocol.BadRequestParam("1")
return
}
return
}
func (this *BaseController) ResponseExcelByFile(ctx *context.Context, excelMaker *exceltool.ExcelMaker) error {
fname := excelMaker.GetFileName()
ctx.Output.Header("Content-Disposition", "attachment; filename="+fname)
ctx.Output.Header("Content-Description", "File Transfer")
ctx.Output.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
ctx.Output.Header("Content-Transfer-Encoding", "binary")
ctx.Output.Header("Expires", "0")
ctx.Output.Header("Cache-Control", "must-revalidate")
ctx.Output.Header("Pragma", "public")
//跳过保存文件,直接写入ctx.ResponseWriter
excelMaker.Xlsx.Write(ctx.ResponseWriter)
return nil
}