stdDocRpc.go 5.4 KB

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