123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package logic
- import (
- MC "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/redis"
- "context"
- "fmt"
- "github.com/zeromicro/go-zero/core/logx"
- IC "jyBXCore/api/init"
- "jyBXCore/api/internal/svc"
- "jyBXCore/api/internal/types"
- "jyBXCore/api/internal/util"
- "strings"
- )
- type MobileHotWordLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewMobileHotWordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MobileHotWordLogic {
- return &MobileHotWordLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx,
- }
- }
- func (l *MobileHotWordLogic) MobileHotWord(req *types.MobileHotWordReq) (resp *types.CommonResp, err error) {
- var rsData []string
- if req.Refresh == 0 { //非刷新动作 自动获取默认缓存热词 5分钟更新一次
- rsData = MC.If(req.UserId == "", IC.HotKeyArrUnLogin, IC.HotKeyArrLoginEd).([]string)
- } else { // 刷新热词 需过滤已经看过的热词
- var reqWord []string
- if req.Word != "" {
- reqWord = strings.Split(req.Word, ",")
- }
- redisKey := MC.If(req.UserId == "", "mobileHotWordUnLogin", fmt.Sprintf("mobileHotWordLogin_%s", req.UserId)).(string)
- rData, _ := redis.Get("newother", redisKey).([]interface{})
- redisWord := MC.ObjArrToStringArr(rData) //已浏览热词
- if redisWord != nil {
- reqWord = append(reqWord, redisWord...)
- }
- aData := MC.If(req.UserId == "", IC.C.MobileIndexHotKeyUnLogin, IC.C.MobileIndexHotKey).([]string)
- rsData = util.FilterWord(aData, reqWord, int(IC.C.MobileIndexHotKeyLimit))
- if len(reqWord) >= len(aData)-int(IC.C.MobileIndexHotKeyLimit) { //剩余热词已经不足 重新获取
- redis.Put("newother", redisKey, nil, 60*60*2) //剩余热词不足 重置
- } else {
- redis.Put("newother", redisKey, reqWord, 60*60*2)
- }
- }
- var rDataArr []map[string]interface{}
- for _, datum := range rsData {
- rDataArr = append(rDataArr, map[string]interface{}{
- "keyword": datum,
- })
- }
- return &types.CommonResp{
- Data: rDataArr,
- }, nil
- }
|