stdDoc.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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, 20*10)
  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. return map[string]interface{}{
  45. "total": total,
  46. "list": list,
  47. }, nil
  48. }()
  49. if errMsg != nil {
  50. log.Printf("%s StdDoc search err:%s\n", userId, errMsg.Error())
  51. }
  52. stdDoc.ServeJson(NewResult(rData, errMsg))
  53. }
  54. func (stdDoc *StdDoc) IndexTag() {
  55. stdDoc.ServeJson(NewResult(config.JyDocsAppConfig.IndexSearchTag, nil))
  56. }
  57. func (stdDoc *StdDoc) Detail() {
  58. userId := common.ObjToString(stdDoc.GetSession("userId"))
  59. rData, errMsg := func() (interface{}, error) {
  60. docId := stdDoc.GetString("docId")
  61. from := stdDoc.GetString("from")
  62. if docId == "" {
  63. return nil, fmt.Errorf("参数异常")
  64. }
  65. if from != "" { //分享赚积分
  66. go public.OpenShareJydoc(from, userId, docId)
  67. }
  68. detail, isBuy, IsCollect, err := rpc.GetDocDetail(userId, docId)
  69. if err != nil {
  70. return nil, err
  71. }
  72. //ossId清除
  73. detail.OssPdfId = ""
  74. detail.OssDocId = ""
  75. detail.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, detail.PreviewImgId)
  76. return map[string]interface{}{
  77. "status": common.If(isBuy, 1, 0),
  78. "collect": common.If(IsCollect, 1, 0),
  79. "detail": detail,
  80. }, nil
  81. }()
  82. if errMsg != nil {
  83. log.Printf("%s StdDoc detail err:%s\n", userId, errMsg.Error())
  84. }
  85. stdDoc.ServeJson(NewResult(rData, errMsg))
  86. }
  87. func (stdDoc *StdDoc) GetDoc(sign string) {
  88. userId := common.ObjToString(stdDoc.GetSession("userId"))
  89. rData, errMsg := func() (interface{}, error) {
  90. docId := stdDoc.GetString("docId")
  91. if docId == "" {
  92. return nil, fmt.Errorf("参数异常")
  93. }
  94. detail, isBuy, _, err := rpc.GetDocDetail(userId, docId)
  95. if err != nil {
  96. return nil, err
  97. }
  98. if !isBuy {
  99. return nil, fmt.Errorf("请先兑换文档")
  100. }
  101. fileId := detail.OssPdfId
  102. if sign == "Down" {
  103. fileId = detail.OssDocId
  104. }
  105. url, err := rpc.GetFileContext(userId, fileId)
  106. if err != nil {
  107. return nil, err
  108. }
  109. if strings.HasPrefix(url, "http://") {
  110. url = strings.Replace(url, "http://", "https://", 1)
  111. }
  112. return url, nil
  113. }()
  114. if errMsg != nil {
  115. log.Printf("%s StdDoc content err:%s\n", userId, errMsg.Error())
  116. }
  117. stdDoc.ServeJson(NewResult(rData, errMsg))
  118. }
  119. func (stdDoc *StdDoc) TopList() {
  120. userId := common.ObjToString(stdDoc.GetSession("userId"))
  121. rData, errMsg := func() (interface{}, error) {
  122. num, _ := stdDoc.GetInt("num") //返回数量
  123. sign := stdDoc.GetString("sign") //类别
  124. reqSort := ""
  125. if num > 50 {
  126. num = 50
  127. }
  128. if sign == "hot" {
  129. reqSort = "dSort"
  130. } else if sign == "new" {
  131. reqSort = "tSort"
  132. } else {
  133. return nil, fmt.Errorf("未知请求")
  134. }
  135. //存入redis缓存
  136. list, _, err := rpc.GetDocQuery(userId, "", "", 1, num, reqSort)
  137. if err != nil {
  138. return nil, err
  139. }
  140. return list, nil
  141. }()
  142. if errMsg != nil {
  143. log.Printf("%s StdDoc topList err:%s\n", userId, errMsg.Error())
  144. }
  145. stdDoc.ServeJson(NewResult(rData, errMsg))
  146. }
  147. func (stdDoc *StdDoc) ActivityList() {
  148. userId := common.ObjToString(stdDoc.GetSession("userId"))
  149. rData, errMsg := func() (interface{}, error) {
  150. code, _ := stdDoc.GetInt("code")
  151. pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始
  152. pageSizeReq, _ := stdDoc.GetInt("size") //每页数量
  153. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, 20*10)
  154. if err != nil {
  155. return nil, err
  156. }
  157. //存入redis缓存
  158. list, err := rpc.GeActivityList(userId, code, pageNum, pageSize)
  159. if err != nil {
  160. return nil, err
  161. }
  162. if list != nil && len(list) > 0 {
  163. for i := 0; i < len(list); i++ {
  164. list[i].DocImg = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, list[i].DocImg)
  165. }
  166. }
  167. return list, nil
  168. }()
  169. if errMsg != nil {
  170. log.Printf("%s StdDoc activityList err:%s\n", userId, errMsg.Error())
  171. }
  172. stdDoc.ServeJson(NewResult(rData, errMsg))
  173. }