base.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "wechat-conf/service"
  6. "wechat-conf/utils"
  7. "github.com/astaxie/beego"
  8. "github.com/astaxie/beego/config"
  9. )
  10. //BaseController 基础controller
  11. type BaseController struct {
  12. beego.Controller
  13. Context *utils.Context
  14. Configer map[string]config.Configer
  15. RunMode string
  16. serv *service.SysServ
  17. }
  18. //Prepare 继承beego的
  19. func (c *BaseController) Prepare() {
  20. // 读取配置文件
  21. c.initConfig()
  22. // 初始化上下文
  23. c.initContext()
  24. // 鉴权
  25. c.auth()
  26. // 路由对应的实际 Controller 初始化
  27. c.initController()
  28. }
  29. // ResponseJSON 返回JSON数据
  30. func (c *BaseController) ResponseJSON(data interface{}) {
  31. c.ResponseData(data, "", http.StatusOK)
  32. }
  33. // ResponseError 返回错误
  34. func (c *BaseController) ResponseError(err error, code ...int) {
  35. if len(code) > 0 {
  36. c.ResponseData(nil, err, code[0])
  37. }
  38. c.ResponseData(nil, err, http.StatusBadRequest)
  39. }
  40. // ResponseRaw 返回
  41. func (c *BaseController) ResponseRaw(msg []byte, stop ...bool) {
  42. c.Ctx.ResponseWriter.Write(msg)
  43. if stop != nil && len(stop) > 0 {
  44. if !stop[0] {
  45. return
  46. }
  47. }
  48. c.destroyContext(true)
  49. c.StopRun()
  50. }
  51. // ResponseData 自定义的JSON返回
  52. func (c *BaseController) ResponseData(data interface{}, msg interface{}, code int) {
  53. status := code
  54. sendMessage := ""
  55. switch msgVal := msg.(type) {
  56. case error:
  57. sendMessage = msgVal.Error()
  58. case string:
  59. sendMessage = msgVal
  60. default:
  61. sendMessage = ""
  62. }
  63. c.Data["json"] = JSONMessage{status, sendMessage, data}
  64. c.Ctx.Output.Header("Access-Control-Expose-Headers", utils.TokenHeader)
  65. if status == http.StatusOK {
  66. // 设置旧 token 过期
  67. // c.SetTokenExipre()
  68. // 生成新 token
  69. // c.CreateNewToken()
  70. //token := c.Context.Get("token").(string)
  71. //c.Ctx.Output.Header(utils.TokenHeader, utils.TokenSchema+" "+token)
  72. }
  73. c.destroyContext(status < http.StatusMultipleChoices)
  74. c.crosPolicy()
  75. c.ServeJSON()
  76. c.StopRun()
  77. }
  78. // initAppController 执行当前路由请求对应的 Controller 初始化
  79. func (c *BaseController) initController() {
  80. if ctrl, ok := c.AppController.(ControllerInterface); ok {
  81. ctrl.Constructor()
  82. }
  83. }
  84. func (c *BaseController) GetBodyData() (map[string]interface{}, error) {
  85. var data map[string]interface{}
  86. err := json.Unmarshal(c.Ctx.Input.RequestBody, &data)
  87. return data, err
  88. }
  89. func (c *BaseController) ResponseOtherEndPoint(msg map[string]interface{}) {
  90. c.destroyContext(true)
  91. c.Context.Destroy()
  92. c.Data["json"] = msg
  93. c.ServeJSON()
  94. c.StopRun()
  95. }