stdDoc.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. }
  141. url, err := rpc.GetFileContext(userId, fileId)
  142. if err != nil {
  143. return nil, err
  144. }
  145. if strings.HasPrefix(url, "http://") {
  146. url = strings.Replace(url, "http://", "https://", 1)
  147. }
  148. return url, nil
  149. }()
  150. if errMsg != nil {
  151. log.Printf("%s StdDoc content err:%s\n", userId, errMsg.Error())
  152. }
  153. stdDoc.ServeJson(NewResult(rData, errMsg))
  154. }
  155. func (stdDoc *StdDoc) TopList() {
  156. userId := common.ObjToString(stdDoc.GetSession("userId"))
  157. rData, errMsg := func() (interface{}, error) {
  158. num, _ := stdDoc.GetInt("num") //返回数量
  159. sign := stdDoc.GetString("sign") //类别
  160. reqSort := ""
  161. if num > 50 {
  162. num = 50
  163. }
  164. if sign == "hot" {
  165. reqSort = "dSort"
  166. } else if sign == "new" {
  167. reqSort = "tSort"
  168. } else {
  169. return nil, fmt.Errorf("未知请求")
  170. }
  171. topKey := fmt.Sprintf("jydoc_indexCache_%s_%d", reqSort, num)
  172. listCache := redis.Get("other", topKey)
  173. if listCache != nil {
  174. return listCache, nil
  175. }
  176. log.Println("flush", topKey)
  177. list, _, err := rpc.GetDocQuery(userId, "", "", 1, num, reqSort)
  178. if err != nil {
  179. return nil, err
  180. }
  181. if len(list) > 0 { //存入redis缓存
  182. for _, v := range list {
  183. if v.PreviewImgId != "" && !strings.HasPrefix(v.PreviewImgId, "http") {
  184. v.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, v.PreviewImgId)
  185. }
  186. }
  187. redis.Put("other", topKey, list, 60*5)
  188. }
  189. return list, nil
  190. }()
  191. if errMsg != nil {
  192. log.Printf("%s StdDoc topList err:%s\n", userId, errMsg.Error())
  193. }
  194. stdDoc.ServeJson(NewResult(rData, errMsg))
  195. }
  196. func (stdDoc *StdDoc) ActivityList() {
  197. userId := common.ObjToString(stdDoc.GetSession("userId"))
  198. rData, errMsg := func() (interface{}, error) {
  199. code, _ := stdDoc.GetInt("code")
  200. pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始
  201. pageSizeReq, _ := stdDoc.GetInt("size") //每页数量
  202. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, 20*10)
  203. if err != nil {
  204. return nil, err
  205. }
  206. //存入redis缓存
  207. list, err := rpc.GeActivityList(userId, code, pageNum, pageSize)
  208. if err != nil {
  209. return nil, err
  210. }
  211. if list != nil && len(list) > 0 {
  212. for i := 0; i < len(list); i++ {
  213. list[i].DocImg = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, list[i].DocImg)
  214. }
  215. }
  216. return list, nil
  217. }()
  218. if errMsg != nil {
  219. log.Printf("%s StdDoc activityList err:%s\n", userId, errMsg.Error())
  220. }
  221. stdDoc.ServeJson(NewResult(rData, errMsg))
  222. }