docquerylogic.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package logic
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "app.yhyue.com/moapp/jy_docs/rpc/stdlib/internal/svc"
  7. "app.yhyue.com/moapp/jy_docs/rpc/stdlib/stdlib"
  8. stdlibService "app.yhyue.com/moapp/jy_docs/services/stdlib"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. type DocQueryLogic struct {
  12. ctx context.Context
  13. svcCtx *svc.ServiceContext
  14. logx.Logger
  15. }
  16. var (
  17. QuerySort = map[string]bool{
  18. "uploadDate": true,
  19. "viewTimes": true,
  20. "downTimes": true,
  21. }
  22. )
  23. func NewDocQueryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DocQueryLogic {
  24. return &DocQueryLogic{
  25. ctx: ctx,
  26. svcCtx: svcCtx,
  27. Logger: logx.WithContext(ctx),
  28. }
  29. }
  30. func (l *DocQueryLogic) DocQuery(in *stdlib.DocQueryRequest) (*stdlib.DocQueryResponse, error) {
  31. if in.PageNum < 1 {
  32. in.PageNum = 1
  33. }
  34. if in.UserId != "" && in.AppId == "" {
  35. return &stdlib.DocQueryResponse{
  36. Code: 0,
  37. Msg: "缺少参数appId",
  38. }, nil
  39. }
  40. for _, v := range in.Sort {
  41. if !QuerySort[v] && !QuerySort[strings.TrimLeft(v, "-")] {
  42. return &stdlib.DocQueryResponse{
  43. Code: 0,
  44. Msg: fmt.Sprintf("sort数组中%s值不合法", v),
  45. }, nil
  46. }
  47. }
  48. return stdlibService.DocQuery(in, l.svcCtx.Config.SearchSource), nil
  49. }