stdDocRpc.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "log"
  10. )
  11. // 标准库RPC接口
  12. var jyStdDocStdlib stdlib.Stdlib
  13. func init() {
  14. jyStdDocStdlib = stdlib.NewStdlib(zrpc.MustNewClient(zrpc.RpcClientConf{
  15. Etcd: discov.EtcdConf{
  16. Key: config.JyDocsAppConfig.RpcServers.StdDoc.Key,
  17. Hosts: config.JyDocsAppConfig.RpcServers.StdDoc.Address,
  18. },
  19. }))
  20. }
  21. /*
  22. 检索文库
  23. param
  24. userId 用户id
  25. keyWord 关键词
  26. tag 分类
  27. pageNum 页码
  28. pageSize 每页数量
  29. tSort 时间排序
  30. dSort 下载排序
  31. vSort 浏览量排序
  32. */
  33. func GetDocQuery(userId, keyWord, tag string, pageNum, pageSize int64, sort string, productType, docFileType int64) ([]*stdlib.Doc, int64, error) {
  34. param := &stdlib.DocQueryRequest{
  35. AppId: config.JyDocsAppConfig.AppId,
  36. KeyWord: keyWord,
  37. PageSize: pageSize,
  38. PageNum: pageNum,
  39. ProductType: productType,
  40. DocFileType: docFileType,
  41. }
  42. if tag != "" {
  43. param.DocTag = []string{tag}
  44. }
  45. sortArr := []string{}
  46. switch sort { //倒序字段前加-,uploadDate:上架时间 viewTimes:浏览量 downTimes:下载量
  47. case "dSort": //下载量倒叙
  48. sortArr = append(sortArr, "-downTimes")
  49. case "vSort": //浏览量倒叙
  50. sortArr = append(sortArr, "-viewTimes")
  51. default: // "tSort"上传时间倒叙
  52. sortArr = append(sortArr, "-uploadDate")
  53. }
  54. param.Sort = sortArr
  55. resp, err := jyStdDocStdlib.DocQuery(context.Background(), param)
  56. if err != nil {
  57. log.Printf("%s SetUserCollect call error %v\n", userId, err)
  58. return nil, -1, err
  59. }
  60. if resp.Code != 1 {
  61. log.Printf("%s SetUserCollect fail Message %v\n", userId, resp.Msg)
  62. return nil, -1, fmt.Errorf("查询失败")
  63. }
  64. return resp.Docs, resp.Total, nil
  65. }
  66. /*
  67. 获取活动列表
  68. param
  69. userId 用户id
  70. code 活动编号
  71. pageNum 页码
  72. pageSize 每页数量
  73. return
  74. 文库列表、异常
  75. */
  76. func GeActivityList(userId string, code, pageNum, pageSize int64) ([]*stdlib.DocActivity, error) {
  77. resp, err := jyStdDocStdlib.DocActivity(context.Background(), &stdlib.DocActivityReq{
  78. AppId: config.JyDocsAppConfig.AppId,
  79. ActivityId: code,
  80. PageNum: pageNum,
  81. PageSize: pageSize,
  82. UserId: userId,
  83. })
  84. if err != nil {
  85. log.Printf("%s GeActivityList call error %v\n", userId, err)
  86. return nil, err
  87. }
  88. if resp.Code != 1 {
  89. log.Printf("%s GeActivityList fail Message %v\n", userId, resp.Msg)
  90. return nil, fmt.Errorf("获取列表失败")
  91. }
  92. return resp.Docs, nil
  93. }
  94. /*
  95. 获取文库详情
  96. param
  97. userId 用户id
  98. docId 文库id
  99. return
  100. DocInfo 文库详情
  101. error 异常
  102. */
  103. func GetDocDetail(userId, docId string) (*stdlib.DocInfo, bool, bool, error) {
  104. resp, err := jyStdDocStdlib.DocGetCheck(context.Background(), &stdlib.DocGetCheckReq{
  105. AppId: config.JyDocsAppConfig.AppId,
  106. UserId: userId,
  107. DocId: docId,
  108. })
  109. if err != nil {
  110. log.Printf("%s GetDocDetail call error %v\n", userId, err)
  111. return nil, false, false, err
  112. }
  113. if resp.Code != 1 {
  114. log.Printf("%s GetDocDetail fail Message %v\n", userId, resp.Msg)
  115. return nil, false, false, fmt.Errorf("获取内容失败")
  116. }
  117. if resp.DocDeail == nil {
  118. return nil, false, false, fmt.Errorf("查询文档异常")
  119. }
  120. return resp.DocDeail, resp.IsBuy, resp.IsCollect, nil
  121. }
  122. /*
  123. 文库浏览次数、下载次数统计
  124. param
  125. userId 用户id
  126. docId 文库id
  127. sign 1增加下载次数 2增加浏览次数 3评分
  128. */
  129. const Down int32 = 1
  130. const View int32 = 2
  131. func DocStatistics(userId, docId string, sign int32) {
  132. resp, err := jyStdDocStdlib.DocStatistics(context.Background(), &stdlib.DocStatisticsReq{
  133. AppId: config.JyDocsAppConfig.AppId,
  134. DocId: docId,
  135. DocStatisticsType: sign,
  136. })
  137. if err != nil {
  138. log.Printf("%s DocStatistics call error %v\n", userId, err)
  139. }
  140. if !resp.State {
  141. log.Printf("%s DocStatistics fail Message %v\n", userId, resp)
  142. }
  143. }
  144. func GetIndexTags() ([]string, error) {
  145. resp, err := jyStdDocStdlib.DocIndexTag(context.Background(), &stdlib.DocIndexTagReq{})
  146. if err != nil {
  147. log.Printf("GetIndexTags call error %v\n", err)
  148. return nil, err
  149. }
  150. if resp.Code != 1 {
  151. log.Printf("GetIndexTags fail Message %v\n", resp.Msg)
  152. return nil, fmt.Errorf("获取内容失败")
  153. }
  154. if resp.Tags == nil {
  155. return nil, fmt.Errorf("查询异常")
  156. }
  157. return resp.Tags, nil
  158. }