123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package controllers
-
- import (
- "encoding/json"
- "net/http"
- "wechat-conf/service"
- "wechat-conf/utils"
-
- "github.com/astaxie/beego"
- "github.com/astaxie/beego/config"
- )
-
-
- type BaseController struct {
- beego.Controller
- Context *utils.Context
- Configer map[string]config.Configer
- RunMode string
- serv *service.SysServ
- }
-
-
- func (c *BaseController) Prepare() {
-
- c.initConfig()
-
-
- c.initContext()
-
-
- c.auth()
-
-
- c.initController()
- }
-
-
- func (c *BaseController) ResponseJSON(data interface{}) {
- c.ResponseData(data, "", http.StatusOK)
- }
-
-
- func (c *BaseController) ResponseError(err error, code ...int) {
- if len(code) > 0 {
- c.ResponseData(nil, err, code[0])
- }
-
- c.ResponseData(nil, err, http.StatusBadRequest)
- }
-
-
- 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()
- }
-
-
- 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.Ctx.Output.Header("Access-Control-Expose-Headers", utils.TokenHeader)
-
- if status == http.StatusOK {
-
-
-
-
-
-
-
-
- }
-
- c.destroyContext(status < http.StatusMultipleChoices)
- c.crosPolicy()
- c.ServeJSON()
- c.StopRun()
- }
-
-
- func (c *BaseController) initController() {
- if ctrl, ok := c.AppController.(ControllerInterface); ok {
- ctrl.Constructor()
- }
- }
-
- func (c *BaseController) GetBodyData() (map[string]interface{}, error) {
- var data map[string]interface{}
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &data)
- return data, err
- }
-
- func (c *BaseController) ResponseOtherEndPoint(msg map[string]interface{}) {
- c.destroyContext(true)
- c.Context.Destroy()
-
- c.Data["json"] = msg
-
- c.ServeJSON()
- c.StopRun()
- }
|