base.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package controller
  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/cmd/cache"
  9. "xps/pkg/system/viewmodel"
  10. )
  11. var (
  12. validate *validator.Validate
  13. )
  14. func init() {
  15. validate = validator.New()
  16. }
  17. type Base struct {
  18. Ctx iris.Context
  19. }
  20. func (b *Base) GetCurOcId() int64 {
  21. ocId, _ := b.Ctx.Values().GetInt64("ocId")
  22. return ocId
  23. }
  24. func (b *Base) GetCurUserId() int64 {
  25. userId, _ := b.Ctx.Values().GetInt64("userId")
  26. return userId
  27. }
  28. func (b *Base) GetCurUserName() string {
  29. userId, _ := b.Ctx.Values().GetInt64("userId")
  30. userData := GetCurUser(userId)
  31. if userData != nil {
  32. return userData.UserName
  33. }
  34. return ""
  35. }
  36. func (b *Base) GetCurUserPositionId() int64 {
  37. userId, _ := b.Ctx.Values().GetInt64("userId")
  38. userData := GetCurUser(userId)
  39. if userData != nil {
  40. return userData.PositionId
  41. }
  42. return 0
  43. }
  44. func (b *Base) GetCurUserPositionName() string {
  45. userId, _ := b.Ctx.Values().GetInt64("userId")
  46. userData := GetCurUser(userId)
  47. if userData != nil {
  48. return userData.PositionName
  49. }
  50. return ""
  51. }
  52. func (b *Base) GetCurUserGroupId() int64 {
  53. userId, _ := b.Ctx.Values().GetInt64("userId")
  54. userData := GetCurUser(userId)
  55. if userData != nil {
  56. return userData.GroupId
  57. }
  58. return 0
  59. }
  60. func (b *Base) GetCurUserGroupName() string {
  61. userId, _ := b.Ctx.Values().GetInt64("userId")
  62. userData := GetCurUser(userId)
  63. if userData != nil {
  64. return userData.GroupName
  65. }
  66. return ""
  67. }
  68. func (b *Base) GetOcTypeId() int64 {
  69. ocTypeId, _ := b.Ctx.Values().GetInt64("ocTypeId")
  70. return ocTypeId
  71. }
  72. func (b *Base) GetAppKey() string {
  73. appKey := b.Ctx.Values().GetString("appKey")
  74. return appKey
  75. }
  76. func (b *Base) GetAppSecret() string {
  77. appSecret := b.Ctx.Values().GetString("appSecret")
  78. return appSecret
  79. }
  80. func (b *Base) GetJsonData() (map[string]interface{}, error) {
  81. data, err := io.ReadAll(b.Ctx.Request().Body)
  82. if err != nil {
  83. return nil, err
  84. }
  85. m, err := objx.FromJSON(string(data))
  86. if err != nil {
  87. return nil, err
  88. }
  89. return m, nil
  90. }
  91. func (b *Base) buildParams() map[string]interface{} {
  92. ocId, err := b.Ctx.Values().GetInt64("ocId")
  93. if err != nil {
  94. _ = b.Ctx.JSON(ResultErr(err.Error(), nil))
  95. b.Ctx.Next()
  96. }
  97. userId, err := b.Ctx.Values().GetInt64("userId")
  98. if err != nil {
  99. _ = b.Ctx.JSON(ResultErr(err.Error(), nil))
  100. b.Ctx.Next()
  101. }
  102. params := make(map[string]interface{})
  103. params["ocId"] = ocId
  104. params["userId"] = userId
  105. groupId := b.Ctx.URLParamInt64Default("groupId", 0)
  106. if groupId > 0 {
  107. params["groupId"] = groupId
  108. }
  109. limit := b.Ctx.URLParamIntDefault("limit", 0)
  110. if limit <= 0 {
  111. limit = 10
  112. }
  113. params["limit"] = limit
  114. page := b.Ctx.URLParamIntDefault("page", 0)
  115. if page <= 0 {
  116. page = 1
  117. }
  118. params["page"] = page
  119. keywords := b.Ctx.URLParamDefault("keywords", "")
  120. if keywords != "" {
  121. params["keywords"] = keywords
  122. }
  123. b.Ctx.URLParams()
  124. return params
  125. }
  126. // RetCode Result Code
  127. type RetCode int
  128. // Result Code List
  129. const (
  130. RetOk RetCode = 200
  131. RetErr RetCode = 500
  132. )
  133. type QueryConditions struct {
  134. OcId int64 `url:"ocId"`
  135. GroupId int64 `url:"groupId"`
  136. UserId int64 `url:"userId"`
  137. Page int `url:"page"`
  138. Limit int `url:"limit"`
  139. Keywords string `url:"keywords"`
  140. }
  141. type JsonResult struct {
  142. Code RetCode `json:"code"`
  143. Msg interface{} `json:"msg"`
  144. Data interface{} `json:"data"`
  145. }
  146. func ResultOk(msg string, objects interface{}) (r *JsonResult) {
  147. r = &JsonResult{Code: RetOk, Msg: msg, Data: objects}
  148. return
  149. }
  150. func ResultErr(msg string, objects interface{}) (r *JsonResult) {
  151. r = &JsonResult{Code: RetErr, Msg: msg, Data: objects}
  152. return
  153. }
  154. func GetCurUser(userId int64) *viewmodel.UserData {
  155. userData, err := cache.GetUserData(userId)
  156. if err != nil {
  157. log.Fatal(err.Error())
  158. }
  159. return userData
  160. }