No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tabulator_response.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package response
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "git.x2erp.com/qdy/go-base/ctx"
  7. )
  8. // TabulatorResponse Tabulator期望的响应格式
  9. type TabulatorResponse struct {
  10. LastPage int `json:"last_page"` // 总页数
  11. Data interface{} `json:"data"` // 数据数组
  12. }
  13. // CreateTabulatorResponse 创建Tabulator格式的响应
  14. func CreateTabulatorResponse[T any](data T, totalCount int, pageSize int, reqCtx *ctx.RequestContext) *QueryResult[map[string]interface{}] {
  15. // 计算总页数
  16. lastPage := 1
  17. if pageSize > 0 && totalCount > 0 {
  18. lastPage = (totalCount + pageSize - 1) / pageSize
  19. }
  20. // 构建Tabulator格式数据
  21. tabulatorData := map[string]interface{}{
  22. "last_page": lastPage,
  23. "data": data,
  24. }
  25. // 创建成功结果
  26. result := &QueryResult[map[string]interface{}]{
  27. Success: true,
  28. Data: tabulatorData,
  29. TotalCount: totalCount,
  30. Time: time.Now().Format(time.RFC3339),
  31. Metadata: reqCtx,
  32. }
  33. // 如果数据是数组,设置Count
  34. switch v := any(data).(type) {
  35. case []interface{}:
  36. result.Count = len(v)
  37. case []map[string]interface{}:
  38. result.Count = len(v)
  39. default:
  40. // 尝试通过反射获取长度
  41. // 简化处理,如果无法获取长度,使用TotalCount
  42. result.Count = totalCount
  43. }
  44. return result
  45. }
  46. // AdaptToTabulator 将现有QueryResult适配为Tabulator格式
  47. // 此函数主要用于中间件或处理器直接输出Tabulator格式
  48. func AdaptToTabulator[T any](result *QueryResult[T], pageSize int) []byte {
  49. if !result.Success {
  50. // 错误响应格式
  51. errorResponse := map[string]interface{}{
  52. "error": result.Error,
  53. }
  54. if result.Message != "" {
  55. errorResponse["message"] = result.Message
  56. }
  57. jsonData, _ := json.Marshal(errorResponse)
  58. return jsonData
  59. }
  60. // 计算总页数
  61. lastPage := 1
  62. if pageSize > 0 && result.TotalCount > 0 {
  63. lastPage = (result.TotalCount + pageSize - 1) / pageSize
  64. }
  65. // 构建Tabulator格式响应
  66. tabulatorResponse := map[string]interface{}{
  67. "last_page": lastPage,
  68. "data": result.Data,
  69. }
  70. // 编码为JSON
  71. jsonData, err := json.Marshal(tabulatorResponse)
  72. if err != nil {
  73. // 编码失败,返回错误
  74. errorResponse := map[string]interface{}{
  75. "error": fmt.Sprintf("JSON编码失败: %v", err),
  76. }
  77. errorData, _ := json.Marshal(errorResponse)
  78. return errorData
  79. }
  80. return jsonData
  81. }
  82. // CreateTabulatorErrorResponse 创建Tabulator格式的错误响应
  83. func CreateTabulatorErrorResponse(errorMsg string, reqCtx *ctx.RequestContext) *QueryResult[map[string]interface{}] {
  84. errorResponse := map[string]interface{}{
  85. "error": errorMsg,
  86. }
  87. return &QueryResult[map[string]interface{}]{
  88. Success: false,
  89. Data: errorResponse,
  90. Error: errorMsg,
  91. Time: time.Now().Format(time.RFC3339),
  92. Metadata: reqCtx,
  93. }
  94. }