stdDoc.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. productType, _ := stdDoc.GetInt("productType") //每页数量
  33. docFileType, _ := stdDoc.GetInt("docFileType") //每页数量
  34. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, config.JyDocsAppConfig.SearchNumLimit)
  35. if err != nil {
  36. return nil, err
  37. }
  38. if keyWord == "" && tag == "" && productType == 0 {
  39. return nil, fmt.Errorf("检索内容不能为空")
  40. }
  41. if tag == "全部" {
  42. tag = ""
  43. }
  44. if keyWord == "" && (pageNum > 1 || pageSize > 50 || docFileType != 0) { // / 搜索时关键词不能为空 热门推荐 会员免费 精选推荐时关键词可以为空所以再判断一下页数
  45. return nil, fmt.Errorf("参数有误")
  46. }
  47. // 关键词为空说明不是搜索过来的
  48. topKey := fmt.Sprintf("jydoc_searchCache_%s_%d_%s_%d_%d", tag, productType, sort, pageNum, pageSize)
  49. if keyWord == "" {
  50. listCache := redis.Get("other", topKey)
  51. if listCache != nil {
  52. return listCache, nil
  53. }
  54. }
  55. list, total, err := rpc.GetDocQuery(userId, keyWord, tag, pageNum, pageSize, sort, productType, docFileType)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if total > config.JyDocsAppConfig.SearchNumLimit {
  60. total = config.JyDocsAppConfig.SearchNumLimit
  61. }
  62. returnList := []interface{}{}
  63. for _, v := range list {
  64. //
  65. if v.Source == public.SourceDd {
  66. v.PreviewImgId = fmt.Sprintf(config.JyDocsAppConfig.DoudingImg, v.DocId)
  67. } else {
  68. v.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, v.PreviewImgId)
  69. }
  70. returnList = append(returnList, v)
  71. }
  72. // 存缓存
  73. if keyWord == "" && len(returnList) > 0 {
  74. redis.Put("other", topKey, list, 60*60*2)
  75. }
  76. return map[string]interface{}{
  77. "total": total,
  78. "list": list,
  79. }, nil
  80. }()
  81. if errMsg != nil {
  82. log.Printf("%s StdDoc search err:%s\n", userId, errMsg.Error())
  83. }
  84. stdDoc.ServeJson(NewResult(rData, errMsg))
  85. }
  86. func (stdDoc *StdDoc) IndexTag() {
  87. tags, err := rpc.GetIndexTags()
  88. stdDoc.ServeJson(NewResult(tags, err))
  89. }
  90. func (stdDoc *StdDoc) Detail() {
  91. userId := common.ObjToString(stdDoc.GetSession("userId"))
  92. rData, errMsg := func() (interface{}, error) {
  93. docId := stdDoc.GetString("docId")
  94. from := stdDoc.GetString("from")
  95. if docId == "" {
  96. return nil, fmt.Errorf("参数异常")
  97. }
  98. if from != "" { //分享赚积分
  99. go public.OpenShareJydoc(from, userId, docId)
  100. }
  101. detail, isBuy, IsCollect, err := rpc.GetDocDetail(userId, docId)
  102. if err != nil {
  103. return nil, err
  104. }
  105. //ossId清除
  106. detail.OssPdfId = ""
  107. detail.OssDocId = ""
  108. detail.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, detail.PreviewImgId)
  109. go rpc.DocStatistics(userId, docId, rpc.View) //统计下载次数
  110. return map[string]interface{}{
  111. "status": common.If(isBuy, 1, 0),
  112. "collect": common.If(IsCollect, 1, 0),
  113. "detail": detail,
  114. }, nil
  115. }()
  116. if errMsg != nil {
  117. log.Printf("%s StdDoc detail err:%s\n", userId, errMsg.Error())
  118. }
  119. stdDoc.ServeJson(NewResult(rData, errMsg))
  120. }
  121. func (stdDoc *StdDoc) Recommend() {
  122. userId := common.ObjToString(stdDoc.GetSession("userId"))
  123. rData, errMsg := func() (interface{}, error) {
  124. docId := stdDoc.GetString("docId")
  125. docTag := stdDoc.GetString("docTag")
  126. num, _ := stdDoc.GetInt("num")
  127. num = public.PageRange(num, 1, 10)
  128. if strings.Index(docTag, ",") > -1 {
  129. docTag = strings.Split(docTag, ",")[0]
  130. }
  131. list, _, err := rpc.GetDocQuery(userId, "", docTag, 1, num+1, "dSort", 0, 0)
  132. if err != nil {
  133. return nil, err
  134. }
  135. returnList := []interface{}{}
  136. for _, v := range list {
  137. if docId == v.DocId || len(returnList) >= common.IntAll(num) {
  138. continue
  139. }
  140. v.DocSummary = ""
  141. v.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, v.PreviewImgId)
  142. returnList = append(returnList, v)
  143. }
  144. return returnList, nil
  145. }()
  146. if errMsg != nil {
  147. log.Printf("%s StdDoc detail err:%s\n", userId, errMsg.Error())
  148. }
  149. stdDoc.ServeJson(NewResult(rData, errMsg))
  150. }
  151. func (stdDoc *StdDoc) GetDoc(sign string) {
  152. userId := common.ObjToString(stdDoc.GetSession("userId"))
  153. rData, errMsg := func() (interface{}, error) {
  154. docId := stdDoc.GetString("docId")
  155. if docId == "" {
  156. return nil, fmt.Errorf("参数异常")
  157. }
  158. detail, isBuy, _, err := rpc.GetDocDetail(userId, docId)
  159. if err != nil {
  160. return nil, err
  161. }
  162. if !isBuy {
  163. return nil, fmt.Errorf("请先兑换文档")
  164. }
  165. fileId := detail.OssPdfId
  166. if sign == "Down" {
  167. fileId = detail.OssDocId
  168. }
  169. url, err := rpc.GetFileContext(userId, fileId)
  170. if err != nil {
  171. return nil, err
  172. }
  173. if strings.HasPrefix(url, "http://") {
  174. url = strings.Replace(url, "http://", "https://", 1)
  175. }
  176. return url, nil
  177. }()
  178. if errMsg != nil {
  179. log.Printf("%s StdDoc content err:%s\n", userId, errMsg.Error())
  180. }
  181. stdDoc.ServeJson(NewResult(rData, errMsg))
  182. }
  183. func (stdDoc *StdDoc) TopList() {
  184. userId := common.ObjToString(stdDoc.GetSession("userId"))
  185. rData, errMsg := func() (interface{}, error) {
  186. num, _ := stdDoc.GetInt("num") //返回数量
  187. sign := stdDoc.GetString("sign") //类别
  188. reqSort := ""
  189. if num > 50 {
  190. num = 50
  191. }
  192. if sign == "hot" {
  193. reqSort = "dSort"
  194. } else if sign == "new" {
  195. reqSort = "tSort"
  196. } else {
  197. return nil, fmt.Errorf("未知请求")
  198. }
  199. topKey := fmt.Sprintf("jydoc_indexCache_%s_%d", reqSort, num)
  200. listCache := redis.Get("other", topKey)
  201. if listCache != nil {
  202. return listCache, nil
  203. }
  204. log.Println("flush", topKey)
  205. list, _, err := rpc.GetDocQuery(userId, "", "", 1, num, reqSort, 0, 0)
  206. if err != nil {
  207. return nil, err
  208. }
  209. if len(list) > 0 { //存入redis缓存
  210. for _, v := range list {
  211. if v.PreviewImgId != "" && !strings.HasPrefix(v.PreviewImgId, "http") {
  212. v.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, v.PreviewImgId)
  213. }
  214. }
  215. redis.Put("other", topKey, list, 60*5)
  216. }
  217. return list, nil
  218. }()
  219. if errMsg != nil {
  220. log.Printf("%s StdDoc topList err:%s\n", userId, errMsg.Error())
  221. }
  222. stdDoc.ServeJson(NewResult(rData, errMsg))
  223. }
  224. func (stdDoc *StdDoc) ActivityList() {
  225. userId := common.ObjToString(stdDoc.GetSession("userId"))
  226. rData, errMsg := func() (interface{}, error) {
  227. code, _ := stdDoc.GetInt("code")
  228. pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始
  229. pageSizeReq, _ := stdDoc.GetInt("size") //每页数量
  230. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, 20*10)
  231. if err != nil {
  232. return nil, err
  233. }
  234. //存入redis缓存
  235. list, err := rpc.GeActivityList(userId, code, pageNum, pageSize)
  236. if err != nil {
  237. return nil, err
  238. }
  239. if list != nil && len(list) > 0 {
  240. for i := 0; i < len(list); i++ {
  241. list[i].DocImg = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, list[i].DocImg)
  242. }
  243. }
  244. return list, nil
  245. }()
  246. if errMsg != nil {
  247. log.Printf("%s StdDoc activityList err:%s\n", userId, errMsg.Error())
  248. }
  249. stdDoc.ServeJson(NewResult(rData, errMsg))
  250. }