base_api_controller.go 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package controllers
  2. import (
  3. _"IC/service"
  4. "IC/enums"
  5. "github.com/astaxie/beego"
  6. )
  7. type BaseAPIController struct {
  8. beego.Controller
  9. }
  10. // func (this *BaseAPIController) Prepare() {
  11. // this.BaseController.Prepare()
  12. // beego.Trace("============================================================")
  13. // beego.Trace("session ID: %v", this.Ctx.Input.Cookie("beegosessionID"))
  14. // beego.Trace("session : %v", this.GetSession("info"))
  15. // this.SetSession("info", time.Now().Format("2006/01/02 15:04:05"))
  16. // beego.Trace("============================================================")
  17. // }
  18. // 输出数据格式化
  19. /*
  20. success json:
  21. {
  22. "state": 1,
  23. "code": 0,
  24. "data": json,
  25. }
  26. fail json:
  27. {
  28. "state": 0,
  29. "code": int,
  30. "msg": string,
  31. }
  32. */
  33. func (this *BaseAPIController) ServeSuccessJSON(data map[string]interface{}) {
  34. this.Data["json"] = enums.MakeSuccessResponseJSON(data)
  35. this.ServeJSON()
  36. }
  37. func (this *BaseAPIController) ServeFailJSONWithSGJErrorCode(code int) {
  38. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(code)
  39. this.ServeJSON()
  40. }
  41. func (this *BaseAPIController) ServeFailJSONWithSGJError(err *enums.SGJError) {
  42. this.Data["json"] = enums.MakeFailResponseJSONWithSGJError(err)
  43. this.ServeJSON()
  44. }
  45. func (this *BaseAPIController) ServeFailJsonSend(code int, msg string) {
  46. this.Data["json"] = enums.MakeFailResponseJSON(msg, code)
  47. this.ServeJSON()
  48. }