stdDoc.go 8.3 KB

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