scrm-go

base_api_controller.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // var userAdmin models.AdminUser
  46. // userAdmin.Id = 380
  47. // userAdmin.Mobile = "13532250447"
  48. // userAdmin.IsSuperAdmin = true
  49. // userAdmin.Status = 1
  50. // userAdmin.CreateTime = 1530786071
  51. // userAdmin.ModifyTime = 1530786071
  52. // var subscibe models.ServeSubscibe
  53. // subscibe.ID = 1
  54. // subscibe.OrgId = 4
  55. // subscibe.PeriodStart = 1538035409
  56. // subscibe.PeriodEnd = 1569571409
  57. // subscibe.State = 1
  58. // subscibe.Status = 1
  59. // subscibe.CreatedTime = 1538035409
  60. // subscibe.UpdatedTime = 1538035409
  61. // subscibes := make(map[int64]*models.ServeSubscibe, 0)
  62. // subscibes[4] = &subscibe
  63. // var adminUserInfo admin_service.AdminUserInfo
  64. // adminUserInfo.CurrentOrgId = 4
  65. // adminUserInfo.CurrentAppId = 5
  66. // adminUserInfo.AdminUser = &userAdmin
  67. // adminUserInfo.Subscibes = subscibes
  68. // this.SetSession("admin_user_info", &adminUserInfo)
  69. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  70. this.StopRun()
  71. }
  72. }
  73. type BaseServeAPIController struct {
  74. BaseAPIController
  75. }
  76. func (this *BaseServeAPIController) Prepare() {
  77. this.BaseAPIController.Prepare()
  78. if this.GetAdminUserInfo() == nil {
  79. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  80. this.StopRun()
  81. }
  82. }