Няма описание
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.

1234567891011121314151617181920212223242526272829303132333435363738
  1. package util
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "time"
  6. "git.x2erp.com/qdy/go-base/ctx"
  7. "git.x2erp.com/qdy/go-base/model/response"
  8. )
  9. // queryHandler - 最简单的版本
  10. func QueryHandlerJson[T any, F any](
  11. w http.ResponseWriter,
  12. r *http.Request,
  13. factory F,
  14. handlerFunc func(F, T, *ctx.RequestContext) *response.QueryResult[[]map[string]interface{}],
  15. ) {
  16. // 解析请求参数
  17. var req T
  18. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  19. // 如果解析失败,也返回 QueryResult 格式
  20. w.Header().Set("Content-Type", "application/json")
  21. json.NewEncoder(w).Encode(&response.QueryResult[[]map[string]interface{}]{
  22. Success: false,
  23. Error: "Invalid request body: " + err.Error(),
  24. Time: time.Now().Format(time.RFC3339),
  25. })
  26. return
  27. }
  28. // 执行业务逻辑
  29. result := handlerFunc(factory, req, ctx.GetContext(r))
  30. // 设置头,写入结果
  31. w.Header().Set("Content-Type", "application/json")
  32. json.NewEncoder(w).Encode(result)
  33. }