stdDoc.go 5.4 KB

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