checksearchlogic.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // todo 待测试
  32. res = new(bxbase.CheckRes)
  33. if in.UserId == "" {
  34. res.ErrCode = 1
  35. res.ErrMsg = "用户未登录"
  36. return
  37. }
  38. if in.Keywords == "" {
  39. res.ErrCode = 1
  40. res.ErrMsg = "请先输入关键词"
  41. return
  42. }
  43. log.Println("校验搜索列表:", in)
  44. in.NotKey = strings.Replace(in.NotKey, " ", ",", -1)
  45. in.Keywords = strings.Replace(in.Keywords, " ", ",", -1)
  46. in.AdditionalWords = strings.Replace(in.AdditionalWords, " ", ",", -1)
  47. query := map[string]interface{}{
  48. "user_id": in.UserId,
  49. }
  50. if IC.Mgo.Count("search_condition", query) >= 10 {
  51. res.ErrCode = 1
  52. res.ErrMsg = "对不起,最多可保存10个筛选条件。"
  53. return
  54. }
  55. in.Keywords = ValueSort(in.Keywords)
  56. in.Area = ValueSort(in.Area)
  57. in.City = ValueSort(in.City)
  58. in.Subtype = ValueSort(in.Subtype)
  59. in.Industry = ValueSort(in.Industry)
  60. in.SelectType = ValueSort(in.SelectType)
  61. in.BuyerClass = ValueSort(in.BuyerClass)
  62. in.NotKey = ValueSort(in.NotKey)
  63. in.AdditionalWords = ValueSort(in.AdditionalWords)
  64. inMap := common.StructToMapMore(in)
  65. inKey := GetKeysByParam(inMap)
  66. query["in_key"] = inKey
  67. res.Data = inKey
  68. if IC.Mgo.Count("search_condition", query) > 0 {
  69. res.ErrCode = 1
  70. res.ErrMsg = "该条件已保存,无需重复添加。"
  71. }
  72. return
  73. }
  74. func ValueSort(v string) string {
  75. vs := strings.Split(v, ",")
  76. sort.Slice(vs, func(i, j int) bool {
  77. return vs[i] < vs[j]
  78. })
  79. return strings.Join(vs, ",")
  80. }
  81. func GetKeysByParam(param map[string]interface{}) string {
  82. ps := &paramSorter{[]string{}, []string{}}
  83. for k, v := range param {
  84. ps.Keys = append(ps.Keys, k)
  85. ps.Values = append(ps.Values, common.ObjToString(v))
  86. }
  87. ps.Sort()
  88. reqStr := ps.String()
  89. str := percentEncode(reqStr)
  90. str = SP(str, "%3A", "%253A", -1)
  91. return MD5(str)
  92. }
  93. func MD5(str string) string {
  94. data := []byte(str)
  95. has := md5.Sum(data)
  96. md5str := fmt.Sprintf("%x", has)
  97. return md5str
  98. }
  99. var SP = strings.Replace
  100. func percentEncode(str string) string {
  101. str = url.QueryEscape(str)
  102. str = SP(SP(SP(str, "+", "%20", -1), "*", "%2A", -1), "%7E", "~", -1)
  103. return str
  104. }
  105. type paramSorter struct {
  106. Keys []string
  107. Values []string
  108. }
  109. func (ps *paramSorter) String() string {
  110. str := ""
  111. for n, k := range ps.Keys {
  112. str += k + "=" + ps.Values[n]
  113. if n < len(ps.Keys)-1 {
  114. str += "&"
  115. }
  116. }
  117. return str
  118. }
  119. func (ps *paramSorter) Sort() {
  120. sort.Sort(ps)
  121. }
  122. func (ps *paramSorter) Len() int {
  123. return len(ps.Values)
  124. }
  125. func (ps *paramSorter) Less(i, j int) bool {
  126. return bytes.Compare([]byte(ps.Keys[i]), []byte(ps.Keys[j])) < 0
  127. }
  128. func (ps *paramSorter) Swap(i, j int) {
  129. ps.Values[i], ps.Values[j] = ps.Values[j], ps.Values[i]
  130. ps.Keys[i], ps.Keys[j] = ps.Keys[j], ps.Keys[i]
  131. }