base_controller.go 4.0 KB

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