scrm-go

base_api_controller.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package controllers
  2. import (
  3. "SCRM/enums"
  4. )
  5. type BaseAPIController struct {
  6. BaseController
  7. }
  8. // 输出数据格式化
  9. /*
  10. success json:
  11. {
  12. "state": 1,
  13. "code": 0,
  14. "data": json,
  15. }
  16. fail json:
  17. {
  18. "state": 0,
  19. "code": int,
  20. "msg": string,
  21. }
  22. */
  23. func (this *BaseAPIController) ServeSuccessJSON(data map[string]interface{}) {
  24. this.Data["json"] = enums.MakeSuccessResponseJSON(data)
  25. this.ServeJSON()
  26. }
  27. func (this *BaseAPIController) ServeFailJSONWithSGJErrorCode(code int) {
  28. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(code)
  29. this.ServeJSON()
  30. }
  31. func (this *BaseAPIController) ServeFailJSONWithSGJError(err *enums.SGJError) {
  32. this.Data["json"] = enums.MakeFailResponseJSONWithSGJError(err)
  33. this.ServeJSON()
  34. }
  35. func (this *BaseAPIController) ServeFailJsonSend(code int, msg string) {
  36. this.Data["json"] = enums.MakeFailResponseJSON(msg, code)
  37. this.ServeJSON()
  38. }
  39. type BaseAuthAPIController struct {
  40. BaseAPIController
  41. }
  42. func (this *BaseAuthAPIController) Prepare() {
  43. this.BaseAPIController.Prepare()
  44. if this.GetAdminUserInfo() == nil {
  45. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  46. this.StopRun()
  47. }
  48. }
  49. type BaseServeAPIController struct {
  50. BaseAPIController
  51. }
  52. func (this *BaseServeAPIController) Prepare() {
  53. this.BaseAPIController.Prepare()
  54. if this.GetAdminUserInfo() == nil {
  55. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  56. this.StopRun()
  57. }
  58. }