base_controller.go 2.6 KB

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