base_api_controller.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package controllers
  2. import (
  3. "SCRM/enums"
  4. "SCRM/models"
  5. "SCRM/service/admin_service"
  6. )
  7. type BaseAPIController struct {
  8. BaseController
  9. }
  10. // 输出数据格式化
  11. /*
  12. success json:
  13. {
  14. "state": 1,
  15. "code": 0,
  16. "data": json,
  17. }
  18. fail json:
  19. {
  20. "state": 0,
  21. "code": int,
  22. "msg": string,
  23. }
  24. */
  25. func (this *BaseAPIController) ServeSuccessJSON(data map[string]interface{}) {
  26. this.Data["json"] = enums.MakeSuccessResponseJSON(data)
  27. this.ServeJSON()
  28. }
  29. func (this *BaseAPIController) ServeFailJSONWithSGJErrorCode(code int) {
  30. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(code)
  31. this.ServeJSON()
  32. }
  33. func (this *BaseAPIController) ServeFailJSONWithSGJError(err *enums.SGJError) {
  34. this.Data["json"] = enums.MakeFailResponseJSONWithSGJError(err)
  35. this.ServeJSON()
  36. }
  37. func (this *BaseAPIController) ServeFailJsonSend(code int, msg string) {
  38. this.Data["json"] = enums.MakeFailResponseJSON(msg, code)
  39. this.ServeJSON()
  40. }
  41. type BaseAuthAPIController struct {
  42. BaseAPIController
  43. }
  44. func (this *BaseAuthAPIController) Prepare() {
  45. this.BaseAPIController.Prepare()
  46. if this.GetAdminUserInfo() == nil {
  47. var userAdmin models.AdminUser
  48. userAdmin.Id = 380
  49. userAdmin.Mobile = "13532250447"
  50. userAdmin.IsSuperAdmin = true
  51. userAdmin.Status = 1
  52. userAdmin.CreateTime = 1530786071
  53. userAdmin.ModifyTime = 1530786071
  54. var subscibe models.ServeSubscibe
  55. subscibe.ID = 1
  56. subscibe.OrgId = 4
  57. subscibe.PeriodStart = 1538035409
  58. subscibe.PeriodEnd = 1569571409
  59. subscibe.State = 1
  60. subscibe.Status = 1
  61. subscibe.CreatedTime = 1538035409
  62. subscibe.UpdatedTime = 1538035409
  63. subscibes := make(map[int64]*models.ServeSubscibe, 0)
  64. subscibes[4] = &subscibe
  65. var adminUserInfo admin_service.AdminUserInfo
  66. adminUserInfo.CurrentOrgId = 4
  67. adminUserInfo.CurrentAppId = 5
  68. adminUserInfo.AdminUser = &userAdmin
  69. adminUserInfo.Subscibes = subscibes
  70. this.SetSession("admin_user_info", &adminUserInfo)
  71. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  72. this.StopRun()
  73. }
  74. }
  75. type BaseServeAPIController struct {
  76. BaseAPIController
  77. }
  78. func (this *BaseServeAPIController) Prepare() {
  79. this.BaseAPIController.Prepare()
  80. if this.GetAdminUserInfo() == nil {
  81. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  82. this.StopRun()
  83. }
  84. }