base_controller.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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) buildParams() map[string]interface{} {
  39. ocId, err := b.Ctx.Values().GetInt64("ocId")
  40. if err != nil {
  41. _ = b.Ctx.JSON(ResultErr(err.Error(), nil))
  42. b.Ctx.Next()
  43. }
  44. userId, err := b.Ctx.Values().GetInt64("userId")
  45. if err != nil {
  46. _ = b.Ctx.JSON(ResultErr(err.Error(), nil))
  47. b.Ctx.Next()
  48. }
  49. params := make(map[string]interface{})
  50. params["ocId"] = ocId
  51. params["userId"] = userId
  52. groupId := b.Ctx.URLParamInt64Default("groupId", 0)
  53. if groupId > 0 {
  54. params["groupId"] = groupId
  55. }
  56. limit := b.Ctx.URLParamIntDefault("limit", 0)
  57. if limit <= 0 {
  58. limit = 10
  59. }
  60. params["limit"] = limit
  61. page := b.Ctx.URLParamIntDefault("page", 0)
  62. if page <= 0 {
  63. page = 1
  64. }
  65. params["page"] = page
  66. keywords := b.Ctx.URLParamDefault("keywords", "")
  67. params["keywords"] = keywords
  68. return params
  69. }
  70. // RetCode Result Code
  71. type RetCode int
  72. // Result Code List
  73. const (
  74. RetOk RetCode = 0
  75. RetErr RetCode = 500
  76. )
  77. type QueryConditions struct {
  78. OcId int64 `url:"ocId"`
  79. GroupId int64 `url:"groupId"`
  80. UserId int64 `url:"userId"`
  81. Page int `url:"page"`
  82. Limit int `url:"limit"`
  83. Keywords string `url:"keywords"`
  84. }
  85. type JsonResult struct {
  86. Code RetCode `json:"code"`
  87. Msg interface{} `json:"msg"`
  88. Data interface{} `json:"data"`
  89. }
  90. type ClientToken struct {
  91. token string `json:"token"`
  92. }
  93. func ResultOk(msg string, objects interface{}) (r *JsonResult) {
  94. r = &JsonResult{Code: RetOk, Msg: msg, Data: objects}
  95. return
  96. }
  97. func ResultErr(msg string, objects interface{}) (r *JsonResult) {
  98. r = &JsonResult{Code: RetErr, Msg: msg, Data: objects}
  99. return
  100. }
  101. func GetCurUser(userId int64) *viewmodels.UserData {
  102. userData, err := cache.GetUserData(userId)
  103. if err != nil {
  104. log.Fatal(err.Error())
  105. }
  106. return userData
  107. }