-
Notifications
You must be signed in to change notification settings - Fork 2
/
BaseController.go
182 lines (145 loc) · 4.78 KB
/
BaseController.go
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
package controllers
import (
"encoding/json"
"io"
"time"
"strconv"
"strings"
"runtime"
"beluga/src/web_server/helpers"
"beluga/src/web_server/models"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
)
type BaseController struct {
beego.Controller
AccountInfo *models.Account
PageSize int
Orm orm.Ormer
}
// 初始化
func (c *BaseController) Prepare() {
c.AllowCross()
c.PageSize = beego.AppConfig.DefaultInt("page_size", 10)
token := c.Ctx.Request.Header.Get("Account-token")
auth_arr := []string{
"/login",
}
c.Orm = orm.NewOrm()
if token == "" {
if !helpers.In_array(c.Ctx.Request.RequestURI, auth_arr) && (c.Ctx.Request.RequestURI != "/") {
c.ResponseJson(helpers.LOGIN_FATL_CODE, false, helpers.LOGIN_FATL_MSG)
}
} else {
c.TokenToUserInfo(token)
c.TokenIsExpiry()
if c.AccountInfo.Status != 1 {
c.ResponseJson(helpers.USER_PROHIBIT_CODE, false, helpers.USER_PROHIBIT_MSG)
}
}
}
// AllowCross 跨域
func (c *BaseController) AllowCross() {
c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") //允许访问源
c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS") //允许post访问
c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "Content-Type,Authorization,X-Requested-With,Account-token") //header的类型
c.Ctx.ResponseWriter.Header().Set("Access-Control-Max-Age", "1728000")
c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Credentials", "true")
c.Ctx.ResponseWriter.Header().Set("Content-type", "application/json;charset=utf-8") //返回数据格式是json TODO 在开发的时候显示json
c.Ctx.ResponseWriter.Header().Set("Cache-Control", "no-cache, no-store")
}
// ResponseJson json返回
func (c *BaseController) ResponseJson(code int, status bool, msg string, data ...interface{}) {
resMap := make(map[string]interface{}, 3)
resMap["code"] = code
resMap["msg"] = msg
resMap["status"] = status
data_len := len(data)
if (data_len > 0) && data[0] != nil {
if data_len > 1 {
resMap["data"] = data
} else {
resMap["data"] = data[0]
}
}
resJson, err := json.Marshal(resMap)
if err != nil {
beego.Error(err)
}
c.recordOperationLog()
c.AllowCross()
io.WriteString(c.Ctx.ResponseWriter, string(resJson))
c.StopRun()
}
// 返回string
func (c *BaseController) ResponseStr(str string) {
c.AllowCross()
io.WriteString(c.Ctx.ResponseWriter, str)
c.StopRun()
}
func (c *BaseController) ResStr(code int, status bool, msg string, data ...interface{}) string {
resMap := make(map[string]interface{}, 3)
resMap["code"] = code
resMap["msg"] = msg
resMap["status"] = status
data_len := len(data)
if (data_len > 0) && data[0] != nil {
if data_len > 1 {
resMap["data"] = data
} else {
resMap["data"] = data[0]
}
}
resJson, err := json.Marshal(resMap)
if err != nil {
beego.Error(err)
}
return string(resJson)
}
// token 获取用户数据
func (c *BaseController) TokenToUserInfo(token string) {
c.AccountInfo = models.NewAccount()
account, err := c.AccountInfo.TokenToUser(c.Orm, token)
if err != nil {
c.ResponseJson(helpers.TOKEN_FATL_CODE, false, helpers.TOKEN_FATL_MSG)
}
c.AccountInfo = account
}
// token 是否过期
func (c *BaseController) TokenIsExpiry() {
current_time := time.Now().Unix()
token_effective := c.AccountInfo.TokenExpiryTime.Unix()
token_expired_time := beego.AppConfig.DefaultInt64("token_expired_time", 1296000)
if current_time > (token_effective + token_expired_time) {
c.ResponseJson(helpers.TOKEN_FATL_CODE, false, helpers.TOKEN_FATL_MSG)
}
}
// 获取系统信息
func (c *BaseController) GetSystemInfo() {
sys_data := map[string]string{
"year": strconv.Itoa(time.Now().Year()),
"golang_version": strings.ToUpper(runtime.Version()),
"appname": beego.AppConfig.DefaultString("appname", "beluga"),
"system_version": beego.AppConfig.DefaultString("system_version", "0.0.1"),
"official_network": beego.AppConfig.DefaultString("official_network", "http://www.beluga.org.cn"),
}
c.ResponseJson(helpers.SUCCESS_CODE, true, helpers.SUCCESS_MSG, sys_data)
}
func (c *BaseController) Options() {
c.AllowCross()
c.ResponseJson(helpers.SUCCESS_CODE, true, helpers.SUCCESS_MSG)
}
// 记录操作
func (c *BaseController) recordOperationLog() {
if c.Ctx.Request.Method != "OPTIONS"{
operation_log_model := models.NewOperationLog()
operation_log_model.C = c.Ctx.Request.RequestURI
operation_log_model.Ident = c.Ctx.Request.Header.Get("User-Agent")
operation_log_model.Params = string(c.Ctx.Input.RequestBody)
operation_log_model.CreateTime = time.Now()
if c.AccountInfo != nil {
operation_log_model.AccountId = int64(c.AccountInfo.Id)
}
operation_log_model.Add(c.Orm)
}
}