user_act_log.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package service
  2. import (
  3. "context"
  4. "crypto/sha1"
  5. "encoding/base64"
  6. "encoding/json"
  7. "strconv"
  8. "time"
  9. "go-common/app/admin/main/passport/model"
  10. "go-common/library/database/elastic"
  11. "go-common/library/log"
  12. )
  13. type userLogsExtra struct {
  14. EncryptTel string `json:"tel"`
  15. EncryptEmail string `json:"email"`
  16. }
  17. // UserBindLog User bind log
  18. func (s *Service) UserBindLog(c context.Context, userActLogReq *model.UserBindLogReq) (res *model.UserBindLogRes, err error) {
  19. e := s.dao.EsCli
  20. nowYear := time.Now().Year()
  21. index1 := "log_user_action_54_" + strconv.Itoa(nowYear)
  22. index2 := "log_user_action_54_" + strconv.Itoa(nowYear-1)
  23. r := e.NewRequest("log_user_action").Fields("mid", "str_0", "extra_data", "ctime").Index(index1, index2)
  24. r.Order("ctime", elastic.OrderDesc).Order("mid", elastic.OrderDesc).Pn(userActLogReq.Page).Ps(userActLogReq.Size)
  25. if userActLogReq.Mid != 0 {
  26. r.WhereEq("mid", userActLogReq.Mid)
  27. }
  28. if userActLogReq.Query != "" {
  29. hash := sha1.New()
  30. hash.Write([]byte(userActLogReq.Query))
  31. telHash := base64.StdEncoding.EncodeToString(hash.Sum(s.hashSalt))
  32. r.WhereEq("str_0", telHash)
  33. }
  34. if userActLogReq.Action != "" {
  35. r.WhereEq("action", userActLogReq.Action)
  36. }
  37. if userActLogReq.From != 0 && userActLogReq.To != 0 {
  38. ftm := time.Unix(userActLogReq.From, 0)
  39. sf := ftm.Format("2006-01-02 15:04:05")
  40. ttm := time.Unix(userActLogReq.To, 0)
  41. tf := ttm.Format("2006-01-02 15:04:05")
  42. r.WhereRange("ctime", sf, tf, elastic.RangeScopeLoRo)
  43. }
  44. esres := new(model.EsRes)
  45. if err = r.Scan(context.Background(), &esres); err != nil {
  46. log.Error("userActLogs search error(%v)", err)
  47. }
  48. var users = make([]*model.UserBindLog, 0)
  49. for _, value := range esres.Result {
  50. var email, tel string
  51. //var model.UserBindLog
  52. userLogExtra := userLogsExtra{}
  53. err = json.Unmarshal([]byte(value.ExtraData), &userLogExtra)
  54. if err != nil {
  55. log.Error("cannot convert json(%s) to struct,err(%+v) ", value.ExtraData, err)
  56. continue
  57. }
  58. if userLogExtra.EncryptEmail != "" {
  59. email, err = s.decrypt(userLogExtra.EncryptEmail)
  60. if err != nil {
  61. log.Error("EncryptEmail decode err(%v)", err)
  62. continue
  63. }
  64. }
  65. if userLogExtra.EncryptTel != "" {
  66. tel, err = s.decrypt(userLogExtra.EncryptTel)
  67. if err != nil {
  68. log.Error("EncryptTel decode err(%v)", err)
  69. continue
  70. }
  71. }
  72. ulr := model.UserBindLog{Mid: value.Mid, EMail: email, Phone: tel, Time: value.CTime}
  73. users = append(users, &ulr)
  74. }
  75. res = &model.UserBindLogRes{Page: esres.Page, Result: users}
  76. return
  77. }
  78. // DecryptBindLog decrypt bind log
  79. func (s *Service) DecryptBindLog(c context.Context, reqParams *model.DecryptBindLogParam) (res map[string]string, err error) {
  80. if len(reqParams.EncryptText) == 0 {
  81. return make(map[string]string), nil
  82. }
  83. res = make(map[string]string, len(reqParams.EncryptText))
  84. for _, v := range reqParams.EncryptText {
  85. var tel string
  86. if tel, err = s.decrypt(v); err != nil {
  87. return
  88. }
  89. res[v] = tel
  90. }
  91. return
  92. }