user_act_log.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package dao
  2. import (
  3. "context"
  4. "crypto/sha1"
  5. "encoding/base64"
  6. "encoding/json"
  7. "strconv"
  8. "time"
  9. "go-common/app/service/main/account-recovery/model"
  10. "go-common/library/database/elastic"
  11. "go-common/library/log"
  12. )
  13. // NickNameLog NickNameLog
  14. func (d *Dao) NickNameLog(c context.Context, nickNameReq *model.NickNameReq) (res *model.NickNameLogRes, err error) {
  15. nowYear := time.Now().Year()
  16. index1 := "log_user_action_14_" + strconv.Itoa(nowYear)
  17. index2 := "log_user_action_14_" + strconv.Itoa(nowYear-1)
  18. r := d.es.NewRequest("log_user_action").Fields("str_0", "str_1").Index(index1, index2)
  19. r.Order("ctime", elastic.OrderDesc).Order("mid", elastic.OrderDesc).Pn(nickNameReq.Page).Ps(nickNameReq.Size)
  20. if nickNameReq.Mid != 0 {
  21. r.WhereEq("mid", nickNameReq.Mid)
  22. }
  23. if nickNameReq.From != 0 && nickNameReq.To != 0 {
  24. ftm := time.Unix(nickNameReq.From, 0)
  25. sf := ftm.Format("2006-01-02 15:04:05")
  26. ttm := time.Unix(nickNameReq.To, 0)
  27. tf := ttm.Format("2006-01-02 15:04:05")
  28. r.WhereRange("ctime", sf, tf, elastic.RangeScopeLoRo)
  29. }
  30. esres := new(model.NickESRes)
  31. if err = r.Scan(context.TODO(), &esres); err != nil {
  32. log.Error("nickNameLog search error(%v)", err)
  33. return
  34. }
  35. var nickNames = make([]*model.NickNameInfo, 0)
  36. for _, value := range esres.Result {
  37. ulr := model.NickNameInfo{OldName: value.OldName, NewName: value.NewName}
  38. nickNames = append(nickNames, &ulr)
  39. }
  40. res = &model.NickNameLogRes{Page: esres.Page, Result: nickNames}
  41. return
  42. }
  43. type userLogsExtra struct {
  44. EncryptTel string `json:"tel"`
  45. EncryptEmail string `json:"email"`
  46. }
  47. // UserBindLog User bind log
  48. func (d *Dao) UserBindLog(c context.Context, userActLogReq *model.UserBindLogReq) (res *model.UserBindLogRes, err error) {
  49. e := d.es
  50. nowYear := time.Now().Year()
  51. var count = 2 //默认查询两年
  52. //2016年就有了手机历史记录,此处需要循环建立索引 , 2018年才有邮箱这个功能
  53. if userActLogReq.Action == "telBindLog" {
  54. count = nowYear - 2015
  55. }
  56. if userActLogReq.Action == "emailBindLog" {
  57. count = nowYear - 2017
  58. }
  59. indexs := make([]string, count)
  60. for i := 0; i < count; i++ {
  61. indexs[i] = "log_user_action_54_" + strconv.Itoa(nowYear-i)
  62. }
  63. r := e.NewRequest("log_user_action").Fields("mid", "str_0", "extra_data", "ctime").Index(indexs...)
  64. r.Order("ctime", elastic.OrderDesc).Order("mid", elastic.OrderDesc).Pn(userActLogReq.Page).Ps(userActLogReq.Size)
  65. if userActLogReq.Mid != 0 {
  66. r.WhereEq("mid", userActLogReq.Mid)
  67. }
  68. if userActLogReq.Query != "" {
  69. hash := sha1.New()
  70. hash.Write([]byte(userActLogReq.Query))
  71. telHash := base64.StdEncoding.EncodeToString(hash.Sum(d.hashSalt))
  72. r.WhereEq("str_0", telHash)
  73. }
  74. if userActLogReq.Action != "" {
  75. r.WhereEq("action", userActLogReq.Action)
  76. }
  77. if userActLogReq.From != 0 && userActLogReq.To != 0 {
  78. ftm := time.Unix(userActLogReq.From, 0)
  79. sf := ftm.Format("2006-01-02 15:04:05")
  80. ttm := time.Unix(userActLogReq.To, 0)
  81. tf := ttm.Format("2006-01-02 15:04:05")
  82. r.WhereRange("ctime", sf, tf, elastic.RangeScopeLoRo)
  83. }
  84. esres := new(model.EsRes)
  85. if err = r.Scan(context.Background(), &esres); err != nil {
  86. log.Error("userActLogs search error(%v)", err)
  87. return
  88. }
  89. var userBindLogs = make([]*model.UserBindLog, 0)
  90. for _, value := range esres.Result {
  91. var email, tel string
  92. //var model.UserBindLog
  93. userLogExtra := userLogsExtra{}
  94. err = json.Unmarshal([]byte(value.ExtraData), &userLogExtra)
  95. if err != nil {
  96. log.Error("cannot convert json(%s) to struct,err(%+v) ", value.ExtraData, err)
  97. continue
  98. }
  99. if userLogExtra.EncryptEmail != "" {
  100. email, err = d.decrypt(userLogExtra.EncryptEmail)
  101. if err != nil {
  102. log.Error("EncryptEmail decode err(%v)", err)
  103. continue
  104. }
  105. }
  106. if userLogExtra.EncryptTel != "" {
  107. tel, err = d.decrypt(userLogExtra.EncryptTel)
  108. if err != nil {
  109. log.Error("EncryptTel decode err(%v)", err)
  110. continue
  111. }
  112. }
  113. ulr := model.UserBindLog{Mid: value.Mid, Email: email, Phone: tel, Time: value.CTime}
  114. userBindLogs = append(userBindLogs, &ulr)
  115. }
  116. res = &model.UserBindLogRes{Page: esres.Page, Result: userBindLogs}
  117. return
  118. }