stdDoc.go 5.0 KB

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