scrm-go

base_api_controller.go 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package controllers
  2. import (
  3. "SCRM/enums"
  4. "SCRM/service/admin_service"
  5. "SCRM/models"
  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. }