stdDoc.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package servers
  2. import (
  3. . "app.yhyue.com/moapp/jybase/api"
  4. "app.yhyue.com/moapp/jybase/common"
  5. "app.yhyue.com/moapp/jybase/go-xweb/xweb"
  6. "fmt"
  7. "jy-docs/config"
  8. "jy-docs/public"
  9. "jy-docs/rpc"
  10. "log"
  11. "strings"
  12. )
  13. type StdDoc struct {
  14. *xweb.Action
  15. search xweb.Mapper `xweb:"/search"` //检索文库
  16. indexTag xweb.Mapper `xweb:"/indexTag"` //首页搜索标签
  17. detail xweb.Mapper `xweb:"/detail"` //文库详情
  18. getDoc xweb.Mapper `xweb:"/get(Show|Down)"` //文库在线查看 or 下载
  19. topList xweb.Mapper `xweb:"/topList"` //最新文档&热门下载
  20. activityList xweb.Mapper `xweb:"/activityList"` //活动文库(精品推荐、兑换榜)
  21. }
  22. func (stdDoc *StdDoc) Search() {
  23. userId := common.ObjToString(stdDoc.GetSession("userId"))
  24. rData, errMsg := func() (interface{}, error) {
  25. keyWord := strings.TrimSpace(stdDoc.GetString("keyWord")) //关键词
  26. tag := stdDoc.GetString("tag") //标签
  27. sort := stdDoc.GetString("sort") //排序 tSort dSort vSort
  28. pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始
  29. pageSizeReq, _ := stdDoc.GetInt("size") //每页数量
  30. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, config.JyDocsAppConfig.SearchNumLimit)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if keyWord == "" {
  35. return nil, fmt.Errorf("检索内容不能为空")
  36. }
  37. if tag == "全部" {
  38. tag = ""
  39. }
  40. list, total, err := rpc.GetDocQuery(userId, keyWord, tag, pageNum, pageSize, sort)
  41. if err != nil {
  42. return nil, err
  43. }
  44. if total > config.JyDocsAppConfig.SearchNumLimit {
  45. total = config.JyDocsAppConfig.SearchNumLimit
  46. }
  47. return map[string]interface{}{
  48. "total": total,
  49. "list": list,
  50. }, nil
  51. }()
  52. if errMsg != nil {
  53. log.Printf("%s StdDoc search err:%s\n", userId, errMsg.Error())
  54. }
  55. stdDoc.ServeJson(NewResult(rData, errMsg))
  56. }
  57. func (stdDoc *StdDoc) IndexTag() {
  58. stdDoc.ServeJson(NewResult(config.JyDocsAppConfig.IndexSearchTag, nil))
  59. }
  60. func (stdDoc *StdDoc) Detail() {
  61. userId := common.ObjToString(stdDoc.GetSession("userId"))
  62. rData, errMsg := func() (interface{}, error) {
  63. docId := stdDoc.GetString("docId")
  64. from := stdDoc.GetString("from")
  65. if docId == "" {
  66. return nil, fmt.Errorf("参数异常")
  67. }
  68. if from != "" { //分享赚积分
  69. go public.OpenShareJydoc(from, userId, docId)
  70. }
  71. detail, isBuy, IsCollect, err := rpc.GetDocDetail(userId, docId)
  72. if err != nil {
  73. return nil, err
  74. }
  75. //ossId清除
  76. detail.OssPdfId = ""
  77. detail.OssDocId = ""
  78. detail.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, detail.PreviewImgId)
  79. go rpc.DocStatistics(userId, docId, rpc.View) //统计下载次数
  80. return map[string]interface{}{
  81. "status": common.If(isBuy, 1, 0),
  82. "collect": common.If(IsCollect, 1, 0),
  83. "detail": detail,
  84. }, nil
  85. }()
  86. if errMsg != nil {
  87. log.Printf("%s StdDoc detail err:%s\n", userId, errMsg.Error())
  88. }
  89. stdDoc.ServeJson(NewResult(rData, errMsg))
  90. }
  91. func (stdDoc *StdDoc) GetDoc(sign string) {
  92. userId := common.ObjToString(stdDoc.GetSession("userId"))
  93. rData, errMsg := func() (interface{}, error) {
  94. docId := stdDoc.GetString("docId")
  95. if docId == "" {
  96. return nil, fmt.Errorf("参数异常")
  97. }
  98. detail, isBuy, _, err := rpc.GetDocDetail(userId, docId)
  99. if err != nil {
  100. return nil, err
  101. }
  102. if !isBuy {
  103. return nil, fmt.Errorf("请先兑换文档")
  104. }
  105. fileId := detail.OssPdfId
  106. if sign == "Down" {
  107. fileId = detail.OssDocId
  108. go rpc.DocStatistics(userId, docId, rpc.Down) //统计下载次数
  109. }
  110. url, err := rpc.GetFileContext(userId, fileId)
  111. if err != nil {
  112. return nil, err
  113. }
  114. if strings.HasPrefix(url, "http://") {
  115. url = strings.Replace(url, "http://", "https://", 1)
  116. }
  117. return url, nil
  118. }()
  119. if errMsg != nil {
  120. log.Printf("%s StdDoc content err:%s\n", userId, errMsg.Error())
  121. }
  122. stdDoc.ServeJson(NewResult(rData, errMsg))
  123. }
  124. func (stdDoc *StdDoc) TopList() {
  125. userId := common.ObjToString(stdDoc.GetSession("userId"))
  126. rData, errMsg := func() (interface{}, error) {
  127. num, _ := stdDoc.GetInt("num") //返回数量
  128. sign := stdDoc.GetString("sign") //类别
  129. reqSort := ""
  130. if num > 50 {
  131. num = 50
  132. }
  133. if sign == "hot" {
  134. reqSort = "dSort"
  135. } else if sign == "new" {
  136. reqSort = "tSort"
  137. } else {
  138. return nil, fmt.Errorf("未知请求")
  139. }
  140. //存入redis缓存
  141. list, _, err := rpc.GetDocQuery(userId, "", "", 1, num, reqSort)
  142. if err != nil {
  143. return nil, err
  144. }
  145. return list, nil
  146. }()
  147. if errMsg != nil {
  148. log.Printf("%s StdDoc topList err:%s\n", userId, errMsg.Error())
  149. }
  150. stdDoc.ServeJson(NewResult(rData, errMsg))
  151. }
  152. func (stdDoc *StdDoc) ActivityList() {
  153. userId := common.ObjToString(stdDoc.GetSession("userId"))
  154. rData, errMsg := func() (interface{}, error) {
  155. code, _ := stdDoc.GetInt("code")
  156. pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始
  157. pageSizeReq, _ := stdDoc.GetInt("size") //每页数量
  158. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, 20*10)
  159. if err != nil {
  160. return nil, err
  161. }
  162. //存入redis缓存
  163. list, err := rpc.GeActivityList(userId, code, pageNum, pageSize)
  164. if err != nil {
  165. return nil, err
  166. }
  167. if list != nil && len(list) > 0 {
  168. for i := 0; i < len(list); i++ {
  169. list[i].DocImg = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, list[i].DocImg)
  170. }
  171. }
  172. return list, nil
  173. }()
  174. if errMsg != nil {
  175. log.Printf("%s StdDoc activityList err:%s\n", userId, errMsg.Error())
  176. }
  177. stdDoc.ServeJson(NewResult(rData, errMsg))
  178. }