123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- package rpc
- import (
- "app.yhyue.com/moapp/jy_docs/rpc/stdlib/stdlib"
- "context"
- "fmt"
- "github.com/zeromicro/go-zero/core/discov"
- "github.com/zeromicro/go-zero/zrpc"
- "jy-docs/config"
- "jy-docs/public"
- "log"
- )
- // 标准库RPC接口
- var jyStdDocStdlib stdlib.Stdlib
- func init() {
- jyStdDocStdlib = stdlib.NewStdlib(zrpc.MustNewClient(zrpc.RpcClientConf{
- Etcd: discov.EtcdConf{
- Key: config.JyDocsAppConfig.RpcServers.StdDoc.Key,
- Hosts: config.JyDocsAppConfig.RpcServers.StdDoc.Address,
- },
- Timeout: config.JyDocsAppConfig.RpcServers.StdDoc.Timeout,
- }))
- }
- /*
- 检索文库
- param
- userId 用户id
- keyWord 关键词
- tag 分类
- pageNum 页码
- pageSize 每页数量
- tSort 时间排序
- dSort 下载排序
- vSort 浏览量排序
- */
- func GetDocQuery(userId, keyWord, tag string, pageNum, pageSize int64, sort string, productType, docFileType int64) ([]*stdlib.Doc, int64, error) {
- param := &stdlib.DocQueryRequest{
- AppId: config.JyDocsAppConfig.AppId,
- KeyWord: keyWord,
- PageSize: pageSize,
- PageNum: pageNum,
- ProductType: productType,
- DocFileType: docFileType,
- }
- if tag != "" {
- param.DocTag = []string{tag}
- }
- sortArr := []string{}
- switch sort { //倒序字段前加-,uploadDate:上架时间 viewTimes:浏览量 downTimes:下载量
- case "dSort": //下载量倒叙
- sortArr = append(sortArr, "-downTimes")
- case "vSort": //浏览量倒叙
- sortArr = append(sortArr, "-viewTimes")
- default: // "tSort"上传时间倒叙
- sortArr = append(sortArr, "-uploadDate")
- }
- param.Sort = sortArr
- resp, err := jyStdDocStdlib.DocQuery(context.Background(), param)
- if err != nil {
- log.Printf("%s SetUserCollect call error %v\n", userId, err)
- return nil, -1, err
- }
- if resp.Code != 1 {
- log.Printf("%s SetUserCollect fail Message %v\n", userId, resp.Msg)
- return nil, -1, fmt.Errorf("查询失败")
- }
- return resp.Docs, resp.Total, nil
- }
- /*
- 获取活动列表
- param
- userId 用户id
- code 活动编号
- pageNum 页码
- pageSize 每页数量
- return
- 文库列表、异常
- */
- func GeActivityList(userId string, code, pageNum, pageSize int64) ([]*stdlib.DocActivity, error) {
- resp, err := jyStdDocStdlib.DocActivity(context.Background(), &stdlib.DocActivityReq{
- AppId: config.JyDocsAppConfig.AppId,
- ActivityId: code,
- PageNum: pageNum,
- PageSize: pageSize,
- UserId: userId,
- })
- if err != nil {
- log.Printf("%s GeActivityList call error %v\n", userId, err)
- return nil, err
- }
- if resp.Code != 1 {
- log.Printf("%s GeActivityList fail Message %v\n", userId, resp.Msg)
- return nil, fmt.Errorf("获取列表失败")
- }
- return resp.Docs, nil
- }
- /*
- 获取文库详情
- param
- userId 用户id
- docId 文库id
- return
- DocInfo 文库详情
- error 异常
- */
- type returnDetail struct {
- *stdlib.DocInfo
- DocMemberPrice int64 `json:"docMemberPrice"`
- DocMemberDiscount int64 `json:"docMemberDiscount"`
- ImgId string `json:"imgId"`
- }
- func GetDocDetail(userId, docId string, isBuyDetail bool) (*returnDetail, bool, bool, error) {
- resp, err := jyStdDocStdlib.DocGetCheck(context.Background(), &stdlib.DocGetCheckReq{
- AppId: config.JyDocsAppConfig.AppId,
- UserId: userId,
- DocId: docId,
- IsBuyDetail: isBuyDetail,
- })
- if err != nil {
- log.Printf("%s GetDocDetail call error %v\n", userId, err)
- return nil, false, false, err
- }
- if resp.Code != 1 {
- log.Printf("%s GetDocDetail fail Message %v\n", userId, resp.Msg)
- return nil, false, false, fmt.Errorf("获取内容失败")
- }
- if resp.DocDeail == nil {
- return nil, false, false, fmt.Errorf("查询文档异常")
- }
- detail := resp.DocDeail
- imgId := detail.PreviewImgId
- if detail.Source == public.SourceDd {
- priceConfig := config.JyDocsAppConfig.Price[detail.Source][detail.ProductType]
- // 豆丁的预览图和价格需要重新计算
- detail.Price = priceConfig.Rate*detail.Price + priceConfig.Base
- detail.PreviewImgId = fmt.Sprintf(config.JyDocsAppConfig.DoudingImg, detail.PreviewImgId)
- } else {
- detail.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, detail.PreviewImgId)
- }
- // 计算会员价
- rs := returnDetail{
- DocInfo: detail,
- DocMemberDiscount: config.JyDocsAppConfig.DocMember.Discount,
- ImgId: imgId,
- }
- // 会员免费
- if detail.ProductType == public.ProductTypeMemberFree {
- rs.DocMemberPrice = 0
- }
- // 精品 8折
- if detail.ProductType == public.ProductTypePremium {
- rs.DocMemberPrice = detail.Price * config.JyDocsAppConfig.DocMember.Discount / 10
- }
- return &rs, resp.IsBuy, resp.IsCollect, nil
- }
- /*
- 文库浏览次数、下载次数统计
- param
- userId 用户id
- docId 文库id
- sign 1增加下载次数 2增加浏览次数 3评分
- */
- const Down int32 = 1
- const View int32 = 2
- func DocStatistics(userId, docId string, sign int32) {
- resp, err := jyStdDocStdlib.DocStatistics(context.Background(), &stdlib.DocStatisticsReq{
- AppId: config.JyDocsAppConfig.AppId,
- DocId: docId,
- DocStatisticsType: sign,
- })
- if err != nil {
- log.Printf("%s DocStatistics call error %v\n", userId, err)
- } else if resp != nil && !resp.State {
- log.Printf("%s DocStatistics fail Message %v\n", userId, resp)
- }
- }
- func GetIndexTags() ([]string, error) {
- resp, err := jyStdDocStdlib.DocIndexTag(context.Background(), &stdlib.DocIndexTagReq{})
- if err != nil {
- log.Printf("GetIndexTags call error %v\n", err)
- return nil, err
- }
- if resp.Code != 1 {
- log.Printf("GetIndexTags fail Message %v\n", resp.Msg)
- return nil, fmt.Errorf("获取内容失败")
- }
- if resp.Tags == nil {
- return nil, fmt.Errorf("查询异常")
- }
- return resp.Tags, nil
- }
|