base_controller.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package controllers
  2. import (
  3. "github.com/go-playground/validator/v10"
  4. "github.com/kataras/iris/v12"
  5. "github.com/stretchr/objx"
  6. "io"
  7. "log"
  8. "xps/cache"
  9. "xps/viewmodels"
  10. )
  11. var (
  12. validate *validator.Validate
  13. )
  14. func init() {
  15. validate = validator.New()
  16. }
  17. func errorData(errs ...error) {
  18. var s string
  19. for _, err := range errs {
  20. if err != nil {
  21. s += err.Error() + "<br/>"
  22. }
  23. }
  24. }
  25. type Base struct {
  26. Ctx iris.Context
  27. }
  28. func (b *Base) GetCurOcId() int64 {
  29. ocId, _ := b.Ctx.Values().GetInt64("ocId")
  30. return ocId
  31. }
  32. func (b *Base) GetCurUserId() int64 {
  33. userId, _ := b.Ctx.Values().GetInt64("userId")
  34. return userId
  35. }
  36. func (b *Base) GetOcTypeId() int64 {
  37. ocTypeId, _ := b.Ctx.Values().GetInt64("ocTypeId")
  38. return ocTypeId
  39. }
  40. func (b *Base) GetAppKey() string {
  41. appKey := b.Ctx.Values().GetString("appKey")
  42. return appKey
  43. }
  44. func (b *Base) GetAppSecret() string {
  45. appSecret := b.Ctx.Values().GetString("appSecret")
  46. return appSecret
  47. }
  48. func (b *Base) GetJsonData() (map[string]interface{}, error) {
  49. data, err := io.ReadAll(b.Ctx.Request().Body)
  50. if err != nil {
  51. return nil, err
  52. }
  53. m, err := objx.FromJSON(string(data))
  54. if err != nil {
  55. return nil, err
  56. }
  57. return m, nil
  58. }
  59. func (b *Base) buildParams() map[string]interface{} {
  60. ocId, err := b.Ctx.Values().GetInt64("ocId")
  61. if err != nil {
  62. _ = b.Ctx.JSON(ResultErr(err.Error(), nil))
  63. b.Ctx.Next()
  64. }
  65. userId, err := b.Ctx.Values().GetInt64("userId")
  66. if err != nil {
  67. _ = b.Ctx.JSON(ResultErr(err.Error(), nil))
  68. b.Ctx.Next()
  69. }
  70. params := make(map[string]interface{})
  71. params["ocId"] = ocId
  72. params["userId"] = userId
  73. groupId := b.Ctx.URLParamInt64Default("groupId", 0)
  74. if groupId > 0 {
  75. params["groupId"] = groupId
  76. }
  77. limit := b.Ctx.URLParamIntDefault("limit", 0)
  78. if limit <= 0 {
  79. limit = 10
  80. }
  81. params["limit"] = limit
  82. page := b.Ctx.URLParamIntDefault("page", 0)
  83. if page <= 0 {
  84. page = 1
  85. }
  86. params["page"] = page
  87. keywords := b.Ctx.URLParamDefault("keywords", "")
  88. if keywords != "" {
  89. params["keywords"] = keywords
  90. }
  91. return params
  92. }
  93. // RetCode Result Code
  94. type RetCode int
  95. // Result Code List
  96. const (
  97. RetOk RetCode = 0
  98. RetErr RetCode = 500
  99. )
  100. type QueryConditions struct {
  101. OcId int64 `url:"ocId"`
  102. GroupId int64 `url:"groupId"`
  103. UserId int64 `url:"userId"`
  104. Page int `url:"page"`
  105. Limit int `url:"limit"`
  106. Keywords string `url:"keywords"`
  107. }
  108. type JsonResult struct {
  109. Code RetCode `json:"code"`
  110. Msg interface{} `json:"msg"`
  111. Data interface{} `json:"data"`
  112. }
  113. func ResultOk(msg string, objects interface{}) (r *JsonResult) {
  114. r = &JsonResult{Code: RetOk, Msg: msg, Data: objects}
  115. return
  116. }
  117. func ResultErr(msg string, objects interface{}) (r *JsonResult) {
  118. r = &JsonResult{Code: RetErr, Msg: msg, Data: objects}
  119. return
  120. }
  121. func GetCurUser(userId int64) *viewmodels.UserData {
  122. userData, err := cache.GetUserData(userId)
  123. if err != nil {
  124. log.Fatal(err.Error())
  125. }
  126. return userData
  127. }