base.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
61
62
63
64
65
66
67
68
69
70
package controllers
import (
"apigame/service/constd"
"apigame/sign"
"encoding/json"
"github.com/astaxie/beego"
)
type BaseController struct {
beego.Controller
}
// RetData 返回封装
func (c *BaseController) RetData(resp map[string]any) {
c.Data["json"] = resp
c.ServeJSON()
}
// RetRspData 返回封装
func (c *BaseController) RetRspData(rspData any) {
resp := make(map[string]any)
resp["data"] = rspData
c.RetData(resp)
}
// RetRspCodeData 返回封装
func (c *BaseController) RetRspCodeData(code string, rspData any) {
resp := make(map[string]any)
resp = constd.CodeMsg(resp, code)
resp["data"] = rspData
c.RetData(resp)
}
// RetCode 返回错误封装
func (c *BaseController) RetCode(code string) {
resp := make(map[string]any)
resp = constd.CodeMsg(resp, code)
c.RetData(resp)
}
// GetPostData 获取 PostData
func (c *BaseController) GetPostData(postData any) bool {
req := c.Ctx.Input.RequestBody
// 本机调试不加密解密
if checkLocal() {
err := json.Unmarshal(req, &postData)
if err != nil {
c.RetCode(constd.RECODE_PARAMERROR)
return false
}
return true
}
// 接收参数,验证签名登录
code, err := sign.Check(req, &postData, true, true)
if err != nil {
resp := make(map[string]interface{})
resp = constd.CodeMsg(resp, code)
c.RetData(resp)
return false
}
return true
}
func checkLocal() bool {
return beego.AppConfig.String("env") == "local"
}