gplus.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package gplus
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. "strings"
  6. iContext "yx-dataset-server/app/context"
  7. "yx-dataset-server/app/errors"
  8. "yx-dataset-server/app/schema"
  9. "yx-dataset-server/library/logger"
  10. "yx-dataset-server/library/utils"
  11. )
  12. // 定义上下文中的键
  13. const (
  14. prefix = "gao-xin-tong"
  15. // UserIDKey 存储上下文中的键(用户ID)
  16. UserIDKey = prefix + "/user-id"
  17. // UserTypeKey 存储上下文中的键(用户类型)
  18. UserTypeKey = prefix + "/user-type"
  19. // TraceIDKey 存储上下文中的键(跟踪ID)
  20. TraceIDKey = prefix + "/trace-id"
  21. // ResBodyKey 存储上下文中的键(响应Body数据)
  22. ResBodyKey = prefix + "/res-body"
  23. )
  24. // ParseJson 解析请求参数Json
  25. func ParseJson(r *ghttp.Request, out interface{}) error {
  26. if err := r.Parse(out); err != nil {
  27. m := "解析请求参数发生错误"
  28. if utils.GetConfig("common.run_mode").String() == "debug" {
  29. m += "[" + err.Error() + "]"
  30. }
  31. return errors.Wrap400Response(err, m)
  32. }
  33. return nil
  34. }
  35. // GetPageIndex 获取当前页
  36. func GetPageIndex(r *ghttp.Request) int {
  37. defaultVal := 1
  38. if v := r.GetQuery("current").Int(); v > 0 {
  39. return v
  40. }
  41. return defaultVal
  42. }
  43. // GetPageSize 获取分页的页大小(最大50)
  44. func GetPageSize(r *ghttp.Request) int {
  45. defaultVal := 10
  46. if v := r.GetQuery("pageSize").Int(); v > 0 {
  47. if v > 50 {
  48. v = 50
  49. }
  50. return v
  51. }
  52. return defaultVal
  53. }
  54. func ResPage(r *ghttp.Request, data interface{}, pr *schema.PaginationResult) {
  55. result := schema.HTTPList{
  56. List: data,
  57. Pagination: &schema.HTTPPagination{
  58. Current: GetPageIndex(r),
  59. PageSize: GetPageSize(r),
  60. },
  61. }
  62. if data != nil {
  63. result.Pagination.Total = pr.Total
  64. }
  65. r.Response.WriteJson(&schema.HttpResponse{
  66. Code: 0,
  67. Message: "success",
  68. Data: result,
  69. })
  70. r.Exit()
  71. }
  72. func ResSuccess(r *ghttp.Request, result interface{}) {
  73. r.Response.WriteJson(&schema.HttpResponse{
  74. Code: 0,
  75. Message: "success",
  76. Data: result,
  77. })
  78. r.Exit()
  79. }
  80. func ResError(r *ghttp.Request, err error) {
  81. r.Response.WriteJson(&schema.HttpResponse{
  82. Code: -1,
  83. Message: err.Error(),
  84. Data: nil,
  85. })
  86. r.Exit()
  87. }
  88. // ResOK 响应OK
  89. func ResOK(c *ghttp.Request) {
  90. ResSuccess(c, schema.HTTPStatus{Status: "OK"})
  91. }
  92. // ResList 响应列表数据
  93. func ResList(c *ghttp.Request, v interface{}) {
  94. ResSuccess(c, schema.HTTPList{List: v})
  95. }
  96. // ResJSON 响应JSON结果
  97. func ResJSON(r *ghttp.Request, code int, v interface{}) {
  98. errors.JsonExit(r, code, v)
  99. }
  100. // SetUserId 上下文中设置用户Id
  101. func SetUserId(r *ghttp.Request, userId string) {
  102. r.SetCtxVar(UserIDKey, userId)
  103. }
  104. // GetUserId 获取上下文中的用户Id
  105. func GetUserId(r *ghttp.Request) string {
  106. return r.GetCtxVar(UserIDKey).String()
  107. }
  108. // GetToken 获取token
  109. func GetToken(r *ghttp.Request) string {
  110. var token string
  111. auth := r.GetHeader("Authorization")
  112. prefix := "Bearer "
  113. if auth != "" && strings.HasPrefix(auth, prefix) {
  114. token = auth[len(prefix):]
  115. }
  116. return token
  117. }
  118. // NewContext 封装上下文入口
  119. func NewContext(r *ghttp.Request) context.Context {
  120. parent := context.Background()
  121. traceId := GetTraceID(r)
  122. if traceId == "" {
  123. traceId = utils.NewTraceID()
  124. }
  125. parent = iContext.NewTraceID(parent, traceId)
  126. parent = logger.NewTraceIDContext(parent, traceId)
  127. if v := GetUserID(r); v != "" {
  128. parent = iContext.NewUserID(parent, v)
  129. parent = logger.NewUserIDContext(parent, v)
  130. }
  131. return parent
  132. }
  133. // GetTraceID 获取追踪ID
  134. func GetTraceID(c *ghttp.Request) string {
  135. return c.GetCtxVar(TraceIDKey).String()
  136. }
  137. // GetUserID 获取用户ID
  138. func GetUserID(c *ghttp.Request) string {
  139. return c.GetCtxVar(UserIDKey).String()
  140. }
  141. // GetUserType 获取用户类型
  142. func GetUserType(c *ghttp.Request) string {
  143. return c.GetCtxVar(UserTypeKey).String()
  144. }