base.go 4.6 KB

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