stdDocRpc.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package rpc
  2. import (
  3. "app.yhyue.com/moapp/jy_docs/rpc/stdlib/stdlib"
  4. "context"
  5. "fmt"
  6. "github.com/zeromicro/go-zero/core/discov"
  7. "github.com/zeromicro/go-zero/zrpc"
  8. "jy-docs/config"
  9. "jy-docs/public"
  10. "log"
  11. )
  12. // 标准库RPC接口
  13. var jyStdDocStdlib stdlib.Stdlib
  14. func init() {
  15. jyStdDocStdlib = stdlib.NewStdlib(zrpc.MustNewClient(zrpc.RpcClientConf{
  16. Etcd: discov.EtcdConf{
  17. Key: config.JyDocsAppConfig.RpcServers.StdDoc.Key,
  18. Hosts: config.JyDocsAppConfig.RpcServers.StdDoc.Address,
  19. },
  20. Timeout: config.JyDocsAppConfig.RpcServers.StdDoc.Timeout,
  21. }))
  22. }
  23. /*
  24. 检索文库
  25. param
  26. userId 用户id
  27. keyWord 关键词
  28. tag 分类
  29. pageNum 页码
  30. pageSize 每页数量
  31. tSort 时间排序
  32. dSort 下载排序
  33. vSort 浏览量排序
  34. */
  35. func GetDocQuery(userId, keyWord, tag string, pageNum, pageSize int64, sort string, productType, docFileType int64) ([]*stdlib.Doc, int64, error) {
  36. param := &stdlib.DocQueryRequest{
  37. AppId: config.JyDocsAppConfig.AppId,
  38. KeyWord: keyWord,
  39. PageSize: pageSize,
  40. PageNum: pageNum,
  41. ProductType: productType,
  42. DocFileType: docFileType,
  43. }
  44. if tag != "" {
  45. param.DocTag = []string{tag}
  46. }
  47. sortArr := []string{}
  48. switch sort { //倒序字段前加-,uploadDate:上架时间 viewTimes:浏览量 downTimes:下载量
  49. case "dSort": //下载量倒叙
  50. sortArr = append(sortArr, "-downTimes")
  51. case "vSort": //浏览量倒叙
  52. sortArr = append(sortArr, "-viewTimes")
  53. default: // "tSort"上传时间倒叙
  54. sortArr = append(sortArr, "-uploadDate")
  55. }
  56. param.Sort = sortArr
  57. resp, err := jyStdDocStdlib.DocQuery(context.Background(), param)
  58. if err != nil {
  59. log.Printf("%s SetUserCollect call error %v\n", userId, err)
  60. return nil, -1, err
  61. }
  62. if resp.Code != 1 {
  63. log.Printf("%s SetUserCollect fail Message %v\n", userId, resp.Msg)
  64. return nil, -1, fmt.Errorf("查询失败")
  65. }
  66. return resp.Docs, resp.Total, nil
  67. }
  68. /*
  69. 获取活动列表
  70. param
  71. userId 用户id
  72. code 活动编号
  73. pageNum 页码
  74. pageSize 每页数量
  75. return
  76. 文库列表、异常
  77. */
  78. func GeActivityList(userId string, code, pageNum, pageSize int64) ([]*stdlib.DocActivity, error) {
  79. resp, err := jyStdDocStdlib.DocActivity(context.Background(), &stdlib.DocActivityReq{
  80. AppId: config.JyDocsAppConfig.AppId,
  81. ActivityId: code,
  82. PageNum: pageNum,
  83. PageSize: pageSize,
  84. UserId: userId,
  85. })
  86. if err != nil {
  87. log.Printf("%s GeActivityList call error %v\n", userId, err)
  88. return nil, err
  89. }
  90. if resp.Code != 1 {
  91. log.Printf("%s GeActivityList fail Message %v\n", userId, resp.Msg)
  92. return nil, fmt.Errorf("获取列表失败")
  93. }
  94. return resp.Docs, nil
  95. }
  96. /*
  97. 获取文库详情
  98. param
  99. userId 用户id
  100. docId 文库id
  101. return
  102. DocInfo 文库详情
  103. error 异常
  104. */
  105. type returnDetail struct {
  106. *stdlib.DocInfo
  107. DocMemberPrice int64 `json:"docMemberPrice"`
  108. DocMemberDiscount int64 `json:"docMemberDiscount"`
  109. ImgId string `json:"imgId"`
  110. }
  111. func GetDocDetail(userId, docId string, isBuyDetail bool) (*returnDetail, bool, bool, error) {
  112. resp, err := jyStdDocStdlib.DocGetCheck(context.Background(), &stdlib.DocGetCheckReq{
  113. AppId: config.JyDocsAppConfig.AppId,
  114. UserId: userId,
  115. DocId: docId,
  116. IsBuyDetail: isBuyDetail,
  117. })
  118. if err != nil {
  119. log.Printf("%s GetDocDetail call error %v\n", userId, err)
  120. return nil, false, false, err
  121. }
  122. if resp.Code != 1 {
  123. log.Printf("%s GetDocDetail fail Message %v\n", userId, resp.Msg)
  124. return nil, false, false, fmt.Errorf("获取内容失败")
  125. }
  126. if resp.DocDeail == nil {
  127. return nil, false, false, fmt.Errorf("查询文档异常")
  128. }
  129. detail := resp.DocDeail
  130. imgId := detail.PreviewImgId
  131. if detail.Source == public.SourceDd {
  132. priceConfig := config.JyDocsAppConfig.Price[detail.Source][detail.ProductType]
  133. // 豆丁的预览图和价格需要重新计算
  134. detail.Price = priceConfig.Rate*detail.Price + priceConfig.Base
  135. detail.PreviewImgId = fmt.Sprintf(config.JyDocsAppConfig.DoudingImg, detail.PreviewImgId)
  136. } else {
  137. detail.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, detail.PreviewImgId)
  138. }
  139. // 计算会员价
  140. rs := returnDetail{
  141. DocInfo: detail,
  142. DocMemberDiscount: config.JyDocsAppConfig.DocMember.Discount,
  143. ImgId: imgId,
  144. }
  145. // 会员免费
  146. if detail.ProductType == public.ProductTypeMemberFree {
  147. rs.DocMemberPrice = 0
  148. }
  149. // 精品 8折
  150. if detail.ProductType == public.ProductTypePremium {
  151. rs.DocMemberPrice = detail.Price * config.JyDocsAppConfig.DocMember.Discount / 10
  152. }
  153. return &rs, resp.IsBuy, resp.IsCollect, nil
  154. }
  155. /*
  156. 文库浏览次数、下载次数统计
  157. param
  158. userId 用户id
  159. docId 文库id
  160. sign 1增加下载次数 2增加浏览次数 3评分
  161. */
  162. const Down int32 = 1
  163. const View int32 = 2
  164. func DocStatistics(userId, docId string, sign int32) {
  165. resp, err := jyStdDocStdlib.DocStatistics(context.Background(), &stdlib.DocStatisticsReq{
  166. AppId: config.JyDocsAppConfig.AppId,
  167. DocId: docId,
  168. DocStatisticsType: sign,
  169. })
  170. if err != nil {
  171. log.Printf("%s DocStatistics call error %v\n", userId, err)
  172. } else if resp != nil && !resp.State {
  173. log.Printf("%s DocStatistics fail Message %v\n", userId, resp)
  174. }
  175. }
  176. func GetIndexTags() ([]string, error) {
  177. resp, err := jyStdDocStdlib.DocIndexTag(context.Background(), &stdlib.DocIndexTagReq{})
  178. if err != nil {
  179. log.Printf("GetIndexTags call error %v\n", err)
  180. return nil, err
  181. }
  182. if resp.Code != 1 {
  183. log.Printf("GetIndexTags fail Message %v\n", resp.Msg)
  184. return nil, fmt.Errorf("获取内容失败")
  185. }
  186. if resp.Tags == nil {
  187. return nil, fmt.Errorf("查询异常")
  188. }
  189. return resp.Tags, nil
  190. }