mobilehotwordlogic.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package logic
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. "context"
  6. "fmt"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. IC "jyBXCore/api/init"
  9. "jyBXCore/api/internal/svc"
  10. "jyBXCore/api/internal/types"
  11. "jyBXCore/api/internal/util"
  12. )
  13. type MobileHotWordLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewMobileHotWordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MobileHotWordLogic {
  19. return &MobileHotWordLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. }
  24. }
  25. func (l *MobileHotWordLogic) MobileHotWord(req *types.MobileHotWordReq) (resp *types.CommonResp, err error) {
  26. var rsData []string
  27. if req.Refresh == 0 { //非刷新动作 自动获取默认缓存热词 5分钟更新一次
  28. rsData = MC.If(req.UserId == "", IC.HotKeyArrUnLogin, IC.HotKeyArrLoginEd).([]string)
  29. } else { // 刷新热词 需过滤已经看过的热词
  30. redisKey := MC.If(req.UserId == "", "mobileHotWordUnLogin", fmt.Sprintf("mobileHotWordLogin_%s", req.UserId)).(string)
  31. rData, _ := redis.Get("newother", redisKey).([]interface{})
  32. defaultWord := MC.If(req.UserId == "", IC.HotKeyArrUnLogin, IC.HotKeyArrLoginEd).([]string) //排除默认词
  33. redisWord := MC.ObjArrToStringArr(rData)
  34. aData := MC.If(req.UserId == "", IC.C.MobileIndexHotKeyUnLogin, IC.C.MobileIndexHotKey).([]string)
  35. if len(defaultWord)+len(redisWord) >= len(aData)-int(IC.C.MobileIndexHotKeyLimit) { //剩余热词已经不足 重新获取
  36. rsData = util.FilterWord(aData, append(defaultWord, redisWord...), int(IC.C.MobileIndexHotKeyLimit))
  37. redis.Put("newother", redisKey, rsData, 60*60)
  38. } else {
  39. rsData = util.FilterWord(aData, append(defaultWord, redisWord...), int(IC.C.MobileIndexHotKeyLimit))
  40. redisWord = append(redisWord, rsData...)
  41. redis.Put("newother", redisKey, redisWord, 60*60)
  42. }
  43. }
  44. return &types.CommonResp{
  45. Data: rsData,
  46. }, nil
  47. }