فهرست منبع

Merge branch 'dev/v1.1.51_dx' of BaseService/jyMicroservices into feature/v1.1.51

duxin 1 سال پیش
والد
کامیت
102edd6cbc

+ 28 - 0
jyBXCore/api/internal/handler/mobilehotwordhandler.go

@@ -0,0 +1,28 @@
+package handler
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+	"jyBXCore/api/internal/logic"
+	"jyBXCore/api/internal/svc"
+	"jyBXCore/api/internal/types"
+)
+
+func mobileHotWordHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.MobileHotWordReq
+		if err := httpx.Parse(r, &req); err != nil {
+			httpx.Error(w, err)
+			return
+		}
+
+		l := logic.NewMobileHotWordLogic(r.Context(), svcCtx)
+		resp, err := l.MobileHotWord(&req)
+		if err != nil {
+			httpx.Error(w, err)
+		} else {
+			httpx.OkJson(w, resp)
+		}
+	}
+}

+ 51 - 0
jyBXCore/api/internal/logic/mobilehotwordlogic.go

@@ -0,0 +1,51 @@
+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"
+)
+
+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 { // 刷新热词 需过滤已经看过的热词
+		redisKey := MC.If(req.UserId == "", "mobileHotWordUnLogin", fmt.Sprintf("mobileHotWordLogin_%s", req.UserId)).(string)
+		rData, _ := redis.Get("newother", redisKey).([]interface{})
+		defaultWord := MC.If(req.UserId == "", IC.HotKeyArrUnLogin, IC.HotKeyArrLoginEd).([]string) //排除默认词
+		redisWord := MC.ObjArrToStringArr(rData)
+		aData := MC.If(req.UserId == "", IC.C.MobileIndexHotKeyUnLogin, IC.C.MobileIndexHotKey).([]string)
+		if len(defaultWord)+len(redisWord) >= len(aData)-int(IC.C.MobileIndexHotKeyLimit) { //剩余热词已经不足 重新获取
+			rsData = util.FilterWord(aData, append(defaultWord, redisWord...), int(IC.C.MobileIndexHotKeyLimit))
+			redis.Put("newother", redisKey, rsData, 60*60)
+		} else {
+			rsData = util.FilterWord(aData, append(defaultWord, redisWord...), int(IC.C.MobileIndexHotKeyLimit))
+			redisWord = append(redisWord, rsData...)
+			redis.Put("newother", redisKey, redisWord, 60*60)
+		}
+	}
+	return &types.CommonResp{
+		Data: rsData,
+	}, nil
+}

+ 25 - 0
jyBXCore/api/internal/util/util.go

@@ -70,3 +70,28 @@ func GetFlag(r *http.Request, w http.ResponseWriter, limitFlag string) (string,
 	http.SetCookie(w, c)
 	return limitFlag, false
 }
+
+// aData 词库 rData过滤词 count数量
+func FilterWord(aData, fData []string, count int) []string {
+	var rsData []string
+	for _, datum := range aData {
+		if len(rsData) >= count {
+			break
+		}
+		var isExist bool
+		for _, datum1 := range fData {
+			if datum == datum1 {
+				isExist = true
+				break
+			}
+		}
+		if !isExist {
+			rsData = append(rsData, datum)
+		}
+	}
+
+	if ct := count - len(rsData); ct > 0 && len(aData) >= ct {
+		rsData = append(rsData, aData[:ct]...) //追加
+	}
+	return rsData
+}