checksearchlogic.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package logic
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "bytes"
  5. "context"
  6. "crypto/md5"
  7. "fmt"
  8. IC "jyBXBase/rpc/init"
  9. "log"
  10. "net/url"
  11. "sort"
  12. "strings"
  13. "jyBXBase/rpc/bxbase"
  14. "jyBXBase/rpc/internal/svc"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type CheckSearchLogic struct {
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. logx.Logger
  21. }
  22. func NewCheckSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckSearchLogic {
  23. return &CheckSearchLogic{
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. Logger: logx.WithContext(ctx),
  27. }
  28. }
  29. // 校验搜索列表
  30. func (l *CheckSearchLogic) CheckSearch(in *bxbase.AddSearchReq) (res *bxbase.CheckRes, err error) {
  31. res = new(bxbase.CheckRes)
  32. if in.UserId == "" {
  33. res.ErrCode = 1
  34. res.ErrMsg = "用户未登录"
  35. return
  36. }
  37. if in.Keywords == "" {
  38. res.ErrCode = 1
  39. res.ErrMsg = "请先输入关键词"
  40. return
  41. }
  42. log.Println("校验搜索列表:", in)
  43. in.NotKey = strings.Replace(in.NotKey, " ", ",", -1)
  44. in.Keywords = strings.Replace(in.Keywords, " ", ",", -1)
  45. in.AdditionalWords = strings.Replace(in.AdditionalWords, " ", ",", -1)
  46. query := map[string]interface{}{
  47. "user_id": in.UserId,
  48. }
  49. if IC.Mgo.Count("search_condition", query) >= 10 {
  50. res.ErrCode = 1
  51. res.ErrMsg = "对不起,最多可保存10个筛选条件。"
  52. return
  53. }
  54. in.Keywords = ValueSort(in.Keywords)
  55. in.Area = ValueSort(in.Area)
  56. in.City = ValueSort(in.City)
  57. in.Subtype = ValueSort(in.Subtype)
  58. in.Industry = ValueSort(in.Industry)
  59. in.SelectType = ValueSort(in.SelectType)
  60. in.BuyerClass = ValueSort(in.BuyerClass)
  61. in.NotKey = ValueSort(in.NotKey)
  62. in.AdditionalWords = ValueSort(in.AdditionalWords)
  63. inMap := common.StructToMapMore(in)
  64. inKey := GetKeysByParam(inMap)
  65. query["in_key"] = inKey
  66. res.Data = inKey
  67. if IC.Mgo.Count("search_condition", query) > 0 {
  68. res.ErrCode = 1
  69. res.ErrMsg = "该条件已保存,无需重复添加。"
  70. }
  71. return
  72. }
  73. func ValueSort(v string) string {
  74. vs := strings.Split(v, ",")
  75. sort.Slice(vs, func(i, j int) bool {
  76. return vs[i] < vs[j]
  77. })
  78. return strings.Join(vs, ",")
  79. }
  80. func GetKeysByParam(param map[string]interface{}) string {
  81. ps := &paramSorter{[]string{}, []string{}}
  82. for k, v := range param {
  83. ps.Keys = append(ps.Keys, k)
  84. ps.Values = append(ps.Values, common.ObjToString(v))
  85. }
  86. ps.Sort()
  87. reqStr := ps.String()
  88. str := percentEncode(reqStr)
  89. str = SP(str, "%3A", "%253A", -1)
  90. return MD5(str)
  91. }
  92. func MD5(str string) string {
  93. data := []byte(str)
  94. has := md5.Sum(data)
  95. md5str := fmt.Sprintf("%x", has)
  96. return md5str
  97. }
  98. var SP = strings.Replace
  99. func percentEncode(str string) string {
  100. str = url.QueryEscape(str)
  101. str = SP(SP(SP(str, "+", "%20", -1), "*", "%2A", -1), "%7E", "~", -1)
  102. return str
  103. }
  104. type paramSorter struct {
  105. Keys []string
  106. Values []string
  107. }
  108. func (ps *paramSorter) String() string {
  109. str := ""
  110. for n, k := range ps.Keys {
  111. str += k + "=" + ps.Values[n]
  112. if n < len(ps.Keys)-1 {
  113. str += "&"
  114. }
  115. }
  116. return str
  117. }
  118. func (ps *paramSorter) Sort() {
  119. sort.Sort(ps)
  120. }
  121. func (ps *paramSorter) Len() int {
  122. return len(ps.Values)
  123. }
  124. func (ps *paramSorter) Less(i, j int) bool {
  125. return bytes.Compare([]byte(ps.Keys[i]), []byte(ps.Keys[j])) < 0
  126. }
  127. func (ps *paramSorter) Swap(i, j int) {
  128. ps.Values[i], ps.Values[j] = ps.Values[j], ps.Values[i]
  129. ps.Keys[i], ps.Keys[j] = ps.Keys[j], ps.Keys[i]
  130. }