base_controller.go 2.3 KB

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