瀏覽代碼

feat:文库推荐新增接口

fuwencai 1 年之前
父節點
當前提交
f2c8457485
共有 4 個文件被更改,包括 142 次插入3 次删除
  1. 3 1
      jydocs-back/go.mod
  2. 7 1
      jydocs-back/public/consts.go
  3. 30 0
      jydocs-back/public/recDoc.go
  4. 102 1
      jydocs-back/servers/stdDoc.go

+ 3 - 1
jydocs-back/go.mod

@@ -1,6 +1,8 @@
 module jy-docs
 
-go 1.20
+go 1.21
+
+toolchain go1.21rc2
 
 require (
 	app.yhyue.com/moapp/jyPoints v1.1.2-0.20231020023521-1a4b1bbf9736

+ 7 - 1
jydocs-back/public/consts.go

@@ -23,5 +23,11 @@ const (
 	RepeatKey = "docBuy_%d" // 一秒内重复请求
 	RedisCode = "new_other"
 
-	file_upload_ing = "file_upload_ing_%s" // 文档正在上传
+	file_upload_ing       = "file_upload_ing_%s" // 文档正在上传
+	RegionStateHot        = 2                    //2:文库首页热门文档;
+	RegionStateMemberFree = 3                    // 3:文库首页会员免费文档;
+	RegionStatePremium    = 4                    // 4:文库首页精选推荐
+
 )
+
+var DocFileType = map[int]string{1: "doc", 2: "pdf", 3: "xls", 4: "ppt", 5: "txt", 6: "其他"}

+ 30 - 0
jydocs-back/public/recDoc.go

@@ -0,0 +1,30 @@
+package public
+
+import (
+	"fmt"
+)
+
+// 获取
+func GetRecDoc(regionState int, docClassCode string) (recList []map[string]interface{}) {
+	query := ""
+	values := []interface{}{regionState}
+	if regionState == RegionStatePremium {
+		query = `SELECT dr.doc_id,dr.create_date,d.docName,d.docPageSize,d.docFileSize,ds.downTimes,ds.viewTimes,d.uploadDate,d.docSummary,d.docFileType,d.previewImgId,d.productType,d.source,d.docTags FROM jydocs.doc_recommend dr left join doc_statistics ds  on(dr.doc_id=ds.docId)  left join doc  d on  dr.doc_id=d.id  where   dr.region_state=?  order by viewTimes desc;`
+	} else {
+		qBase := `SELECT dr.create_date,dr.doc_id,dr.create_date,d.docName,d.docPageSize,d.docFileSize,d.uploadDate,d.docSummary,d.docFileType,d.previewImgId,d.productType,d.source,d.docTags FROM jydocs.doc_recommend dr     left join doc  d on  dr.doc_id=d.id  where  dr.doc_class_code==? %s   order by create_date desc`
+		and := ""
+		if regionState == RegionStateHot {
+			and = "	and dr.doc_class_code=?"
+			values = append(values, docClassCode)
+		}
+		query = fmt.Sprintf(qBase, and)
+	}
+	// 精选文档
+	// 热门文档和会员免费文档
+	rs := BaseMysql.SelectBySql(query, values...)
+	if rs != nil || len(*rs) == 0 {
+		return []map[string]interface{}{}
+	}
+	return *rs
+
+}

+ 102 - 1
jydocs-back/servers/stdDoc.go

@@ -23,6 +23,7 @@ type StdDoc struct {
 	getDoc       xweb.Mapper `xweb:"/get(Show|Down)"`   //文库在线查看 or 下载
 	topList      xweb.Mapper `xweb:"/topList"`          //最新文档&热门下载
 	activityList xweb.Mapper `xweb:"/activityList"`     //活动文库(精品推荐、兑换榜)
+	docRecommend xweb.Mapper `xweb:"/docRecommend"`     // 文库首页 :热门文档推荐、 会员免费 、精选推荐
 }
 
 func (stdDoc *StdDoc) Search() {
@@ -111,7 +112,7 @@ func (stdDoc *StdDoc) Detail() {
 		detail.OssDocId = ""
 		downloadStatus := 0 // 1- 免费用户下载豆丁免费次数用完
 		// 如果是会员免费文档 并且不是文库会员 并且没有买过 时免费判断判断有没有超过10篇
-		if !isBuy && userId != "" && detail.ProductType == public.ProductTypeMemberFree &&detail.Source==public.SourceDd {
+		if !isBuy && userId != "" && detail.ProductType == public.ProductTypeMemberFree && detail.Source == public.SourceDd {
 			mData := jy.GetBigVipUserBaseMsg(stdDoc.Session(), *config.Middleground)
 			if mData != nil && mData.Data != nil {
 				if mData.Data.Docs.Status <= 0 {
@@ -294,3 +295,103 @@ func (stdDoc *StdDoc) ActivityList() {
 	}
 	stdDoc.ServeJson(NewResult(rData, errMsg))
 }
+
+// 文库推荐
+func (stdDoc *StdDoc) DocRecommend() {
+	userId := common.ObjToString(stdDoc.GetSession("userId"))
+	rData, errMsg := func() (interface{}, error) {
+
+		tag := stdDoc.GetString("tag") //标签
+		//sort := stdDoc.GetString("sort")                   //排序 tSort dSort vSort
+		pageSizeReq, _ := stdDoc.GetInteger("size")        //每页数量
+		productType, _ := stdDoc.GetInteger("productType") // 商品类型 1-会员免费 2-精品文档
+		if pageSizeReq > 50 {
+			return nil, fmt.Errorf("数量有误")
+
+		}
+		if (tag == "" && productType == 0) || (tag != "" && productType != 0) {
+			return nil, fmt.Errorf("不能为空")
+		}
+		var regionState int
+		if tag != "" {
+			regionState = public.RegionStateHot
+		} else if productType == public.ProductTypeMemberFree {
+			regionState = public.RegionStateMemberFree
+		} else if productType == public.ProductTypePremium {
+			regionState = public.RegionStatePremium
+		} else {
+			return nil, fmt.Errorf("参数无效")
+		}
+		topKey := fmt.Sprintf("jydoc_RecCache_%d_%s", regionState, tag)
+		listCache := redis.Get("other", topKey)
+		if listCache != nil {
+			list, ok := listCache.([]interface{})
+			if ok && list != nil && len(list) > 0 {
+				if len(list) > pageSizeReq {
+					list = list[:pageSizeReq]
+				}
+				return map[string]interface{}{
+					"total": len(list),
+					"list":  list,
+				}, nil
+			}
+		}
+		// 查库
+		list := []map[string]interface{}{}
+		rs := public.GetRecDoc(regionState, tag)
+		// 格式化数据
+		for i := 0; i < len(rs); i++ {
+			tags := strings.Split(common.ObjToString((rs)[i]["docTags"]), ",")
+			tmptags := []string{}
+			subTag := ""
+			for j := 0; j < len(tags); j++ {
+				if _, ok := public.DocClassInfo[tags[j]]; ok && len(tmptags) == 0 {
+					tmptags = append(tmptags, tags[j])
+				} else {
+					subTag = tags[j]
+				}
+				if subTag != "" && len(tmptags) > 0 {
+					tmptags = append(tmptags, subTag)
+					break
+				}
+			}
+			previewImgId := ""
+			if common.IntAll((rs)[i]["source"]) == public.SourceDd {
+				previewImgId = fmt.Sprintf(config.JyDocsAppConfig.DoudingImg, rs[i]["previewImgId"])
+			} else {
+				previewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, rs[i]["previewImgId"])
+			}
+			doc := map[string]interface{}{
+				"docId":        common.ObjToString((rs)[i]["id"]),
+				"docName":      common.ObjToString((rs)[i]["docName"]),
+				"docPageSize":  common.Int64All((rs)[i]["docPageSize"]),
+				"docFileSize":  common.Int64All((rs)[i]["docFileSize"]),
+				"downTimes":    common.Int64All((rs)[i]["downTimes"]),
+				"viewTimes":    common.Int64All((rs)[i]["(rs)[i]iewTimes"]),
+				"uploadDate":   common.ObjToString((rs)[i]["uploadDate"]),
+				"docSummary":   common.ObjToString((rs)[i]["docSummary"]),
+				"docFileType":  public.DocFileType[common.IntAll((rs)[i]["docFileType"])],
+				"previewImgId": previewImgId,
+				"productType":  common.Int64All((rs)[i]["productType"]),
+				"source":       common.Int64All((rs)[i]["source"]),
+				"docTags":      strings.Join(tmptags, "  "),
+			}
+			list = append(list, doc)
+		}
+		// 存缓存
+		if len(list) > 0 {
+			redis.Put("other", topKey, list, 60*60*2)
+		}
+		if len(list) > pageSizeReq {
+			list = list[:pageSizeReq]
+		}
+		return map[string]interface{}{
+			"total": len(list),
+			"list":  list,
+		}, nil
+	}()
+	if errMsg != nil {
+		log.Printf("%s StdDoc search err:%s\n", userId, errMsg.Error())
+	}
+	stdDoc.ServeJson(NewResult(rData, errMsg))
+}