stdDoc.go 6.9 KB

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