1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package controllers
-
- import (
- "net/http"
- "support-game/utils"
-
- "github.com/astaxie/beego"
- "github.com/astaxie/beego/config"
- )
-
- //BaseController 基础controller
- type BaseController struct {
- beego.Controller
- Context *utils.Context
- Configer map[string]config.Configer
- RunMode string
- }
-
- //Prepare 继承beego的
- func (c *BaseController) Prepare() {
- // 读取配置文件
- c.initConfig()
-
- // 初始化上下文
- c.initContext()
-
- // 路由对应的实际 Controller 初始化
- c.initController()
- }
-
- // ResponseJSON 返回JSON数据
- func (c *BaseController) ResponseJSON(data interface{}) {
- c.ResponseData(data, "", http.StatusOK)
- }
-
- // ResponseError 返回错误
- func (c *BaseController) ResponseError(err error, code ...int) {
- if len(code) > 0 {
- c.ResponseData(nil, err, code[0])
- }
-
- c.ResponseData(nil, err, http.StatusBadRequest)
- }
-
- // ResponseRaw 返回
- func (c *BaseController) ResponseRaw(msg []byte, stop ...bool) {
- c.Ctx.ResponseWriter.Write(msg)
-
- if stop != nil && len(stop) > 0 {
- if !stop[0] {
- return
- }
- }
-
- c.destroyContext(true)
- c.StopRun()
- }
-
- // ResponseData 自定义的JSON返回
- func (c *BaseController) ResponseData(data interface{}, msg interface{}, code int) {
- status := code
-
- sendMessage := ""
- switch msgVal := msg.(type) {
- case error:
- sendMessage = msgVal.Error()
- case string:
- sendMessage = msgVal
- default:
- sendMessage = ""
- }
-
- c.Data["json"] = JSONMessage{status, sendMessage, data}
-
- c.destroyContext(status < http.StatusMultipleChoices)
- c.crosPolicy()
- c.ServeJSON()
- c.StopRun()
- }
-
- // initAppController 执行当前路由请求对应的 Controller 初始化
- func (c *BaseController) initController() {
- if ctrl, ok := c.AppController.(ControllerInterface); ok {
- ctrl.Constructor()
- }
- }
|