base_controller.go
1.1 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
package controllers
import (
"encoding/json"
"github.com/astaxie/beego"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/web/beego/utils"
)
type BaseController struct {
beego.Controller
}
func (controller BaseController) JsonUnmarshal(v interface{}) error {
body := controller.Ctx.Input.GetData("requestBody").([]byte)
//fmt.Println("【RequestBody】 ",controller.Ctx.Input.Method(),controller.Ctx.Input.URL(),string(body))
return json.Unmarshal(body, v)
}
func (controller BaseController) HandlerResponse(data interface{}, err error) {
var response utils.JsonResponse
defer func() {
controller.Data["json"] = response
controller.ServeJSON()
}()
if err == nil {
response = utils.ResponseData(controller.Ctx, data)
return
}
if _, ok := err.(*application.ServiceError); !ok {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
response = utils.ResponseError(controller.Ctx, err)
}
func (controller BaseController) GetLimitInfo() (offset int, limit int) {
offset, _ = controller.GetInt("offset")
limit, _ = controller.GetInt("limit")
return
}