stdDoc.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. "app.yhyue.com/moapp/jypkg/common/src/qfw/util/jy"
  8. "fmt"
  9. "jy-docs/config"
  10. "jy-docs/public"
  11. "jy-docs/rpc"
  12. "log"
  13. "strings"
  14. )
  15. type StdDoc struct {
  16. *xweb.Action
  17. search xweb.Mapper `xweb:"/search"` //检索文库
  18. indexTag xweb.Mapper `xweb:"/indexTag"` //首页搜索标签
  19. detail xweb.Mapper `xweb:"/detail"` //文库详情
  20. recommend xweb.Mapper `xweb:"/detail/recommend"` //相关推荐
  21. getDoc xweb.Mapper `xweb:"/get(Show|Down)"` //文库在线查看 or 下载
  22. topList xweb.Mapper `xweb:"/topList"` //最新文档&热门下载
  23. activityList xweb.Mapper `xweb:"/activityList"` //活动文库(精品推荐、兑换榜)
  24. }
  25. func (stdDoc *StdDoc) Search() {
  26. userId := common.ObjToString(stdDoc.GetSession("userId"))
  27. rData, errMsg := func() (interface{}, error) {
  28. keyWord := strings.TrimSpace(stdDoc.GetString("keyWord")) //关键词
  29. tag := stdDoc.GetString("tag") //标签
  30. sort := stdDoc.GetString("sort") //排序 tSort dSort vSort
  31. pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始
  32. pageSizeReq, _ := stdDoc.GetInt("size") //每页数量
  33. productType, _ := stdDoc.GetInt("productType") //每页数量
  34. docFileType, _ := stdDoc.GetInt("docFileType") //每页数量
  35. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, config.JyDocsAppConfig.SearchNumLimit)
  36. if err != nil {
  37. return nil, err
  38. }
  39. if keyWord == "" && tag == "" && productType == 0 {
  40. return nil, fmt.Errorf("检索内容不能为空")
  41. }
  42. if tag == "全部" {
  43. tag = ""
  44. }
  45. if keyWord == "" && (pageNum > 1 || pageSize > 50 || docFileType != 0) { // / 搜索时关键词不能为空 热门推荐 会员免费 精选推荐时关键词可以为空所以再判断一下页数
  46. return nil, fmt.Errorf("参数有误")
  47. }
  48. // 关键词为空说明不是搜索过来的
  49. topKey := fmt.Sprintf("jydoc_searchCache_%s_%d_%s_%d_%d", tag, productType, sort, pageNum, pageSize)
  50. if keyWord == "" {
  51. listCache := redis.Get("other", topKey)
  52. if listCache != nil {
  53. return listCache, nil
  54. }
  55. }
  56. list, total, err := rpc.GetDocQuery(userId, keyWord, tag, pageNum, pageSize, sort, productType, docFileType)
  57. if err != nil {
  58. return nil, err
  59. }
  60. if total > config.JyDocsAppConfig.SearchNumLimit {
  61. total = config.JyDocsAppConfig.SearchNumLimit
  62. }
  63. for i := 0; i < len(list); i++ {
  64. if list[i].Source == public.SourceDd {
  65. list[i].PreviewImgId = fmt.Sprintf(config.JyDocsAppConfig.DoudingImg, list[i].PreviewImgId)
  66. } else {
  67. list[i].PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, list[i].PreviewImgId)
  68. }
  69. }
  70. rs := map[string]interface{}{
  71. "total": total,
  72. "list": list,
  73. }
  74. // 存缓存
  75. if keyWord == "" && len(list) > 0 {
  76. redis.Put("other", topKey, rs, 60*60*2)
  77. }
  78. return rs, nil
  79. }()
  80. if errMsg != nil {
  81. log.Printf("%s StdDoc search err:%s\n", userId, errMsg.Error())
  82. }
  83. stdDoc.ServeJson(NewResult(rData, errMsg))
  84. }
  85. func (stdDoc *StdDoc) IndexTag() {
  86. tags, err := rpc.GetIndexTags()
  87. stdDoc.ServeJson(NewResult(tags, err))
  88. }
  89. func (stdDoc *StdDoc) Detail() {
  90. userId := common.ObjToString(stdDoc.GetSession("userId"))
  91. rData, errMsg := func() (interface{}, error) {
  92. docId := stdDoc.GetString("docId")
  93. from := stdDoc.GetString("from")
  94. if docId == "" {
  95. return nil, fmt.Errorf("参数异常")
  96. }
  97. if from != "" { //分享赚积分
  98. go public.OpenShareJydoc(from, userId, docId)
  99. }
  100. detail, isBuy, IsCollect, err := rpc.GetDocDetail(userId, docId, false)
  101. if err != nil {
  102. return nil, err
  103. }
  104. //ossId清除
  105. detail.OssPdfId = ""
  106. detail.OssDocId = ""
  107. downloadStatus := 0 // 1- 免费用户下载豆丁免费次数用完
  108. // 如果是会员免费文档 并且不是文库会员 并且没有买过 时免费判断判断有没有超过10篇
  109. if !isBuy && userId != "" &&detail.ProductType==public.ProductTypeMemberFree{
  110. mData := jy.GetBigVipUserBaseMsg(stdDoc.Session(), *config.Middleground)
  111. if mData != nil && mData.Data != nil {
  112. if mData.Data.Docs.Status <= 0 {
  113. // 免费用户 判断豆丁免费文档下载次数
  114. count, _, err2 := rpc.TodayCount(userId)
  115. if err2 != nil {
  116. return nil, err2
  117. }
  118. if count >= int64(config.JyDocsAppConfig.DocMember.FreeDocLimit) {
  119. downloadStatus = 1
  120. }
  121. }
  122. }
  123. }
  124. go rpc.DocStatistics(userId, docId, rpc.View) //统计下载次数
  125. return map[string]interface{}{
  126. "status": common.If(isBuy, 1, 0),
  127. "collect": common.If(IsCollect, 1, 0),
  128. "detail": detail,
  129. "downloadStatus": downloadStatus,
  130. }, nil
  131. }()
  132. if errMsg != nil {
  133. log.Printf("%s StdDoc detail err:%s\n", userId, errMsg.Error())
  134. }
  135. stdDoc.ServeJson(NewResult(rData, errMsg))
  136. }
  137. func (stdDoc *StdDoc) Recommend() {
  138. userId := common.ObjToString(stdDoc.GetSession("userId"))
  139. rData, errMsg := func() (interface{}, error) {
  140. docId := stdDoc.GetString("docId")
  141. docTag := stdDoc.GetString("docTag")
  142. num, _ := stdDoc.GetInt("num")
  143. num = public.PageRange(num, 1, 10)
  144. if strings.Index(docTag, ",") > -1 {
  145. docTag = strings.Split(docTag, ",")[0]
  146. }
  147. list, _, err := rpc.GetDocQuery(userId, "", docTag, 1, num+1, "dSort", 0, 0)
  148. if err != nil {
  149. return nil, err
  150. }
  151. returnList := []interface{}{}
  152. for _, v := range list {
  153. if docId == v.DocId || len(returnList) >= common.IntAll(num) {
  154. continue
  155. }
  156. v.DocSummary = ""
  157. if v.Source == public.SourceDd {
  158. v.PreviewImgId = fmt.Sprintf(config.JyDocsAppConfig.DoudingImg, v.PreviewImgId)
  159. } else {
  160. v.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, v.PreviewImgId)
  161. }
  162. returnList = append(returnList, v)
  163. }
  164. return returnList, nil
  165. }()
  166. if errMsg != nil {
  167. log.Printf("%s StdDoc detail err:%s\n", userId, errMsg.Error())
  168. }
  169. stdDoc.ServeJson(NewResult(rData, errMsg))
  170. }
  171. func (stdDoc *StdDoc) GetDoc(sign string) {
  172. userId := common.ObjToString(stdDoc.GetSession("userId"))
  173. rData, errMsg := func() (interface{}, error) {
  174. docId := stdDoc.GetString("docId")
  175. if docId == "" {
  176. return nil, fmt.Errorf("参数异常")
  177. }
  178. detail, isBuy, _, err := rpc.GetDocDetail(userId, docId, false)
  179. if err != nil {
  180. return nil, err
  181. }
  182. if !isBuy {
  183. return nil, fmt.Errorf("请先兑换文档")
  184. }
  185. fileId := detail.OssPdfId
  186. if sign == "Down" {
  187. fileId = detail.OssDocId
  188. if b, _ := redis.Exists(public.RedisCode, fmt.Sprintf("file_upload_ing_%s", fileId)); b {
  189. return nil, fmt.Errorf("文档正在上传中,请稍后再试")
  190. }
  191. }
  192. url, err := rpc.GetFileContext(userId, fileId)
  193. if err != nil {
  194. return nil, err
  195. }
  196. if strings.HasPrefix(url, "http://") {
  197. url = strings.Replace(url, "http://", "https://", 1)
  198. }
  199. return url, nil
  200. }()
  201. if errMsg != nil {
  202. log.Printf("%s StdDoc content err:%s\n", userId, errMsg.Error())
  203. }
  204. stdDoc.ServeJson(NewResult(rData, errMsg))
  205. }
  206. func (stdDoc *StdDoc) TopList() {
  207. userId := common.ObjToString(stdDoc.GetSession("userId"))
  208. rData, errMsg := func() (interface{}, error) {
  209. num, _ := stdDoc.GetInt("num") //返回数量
  210. sign := stdDoc.GetString("sign") //类别
  211. reqSort := ""
  212. if num > 50 {
  213. num = 50
  214. }
  215. if sign == "hot" {
  216. reqSort = "dSort"
  217. } else if sign == "new" {
  218. reqSort = "tSort"
  219. } else {
  220. return nil, fmt.Errorf("未知请求")
  221. }
  222. topKey := fmt.Sprintf("jydoc_indexCache_%s_%d", reqSort, num)
  223. listCache := redis.Get("other", topKey)
  224. if listCache != nil {
  225. return listCache, nil
  226. }
  227. log.Println("flush", topKey)
  228. list, _, err := rpc.GetDocQuery(userId, "", "", 1, num, reqSort, 0, 0)
  229. if err != nil {
  230. return nil, err
  231. }
  232. if len(list) > 0 { //存入redis缓存
  233. for _, v := range list {
  234. if v.PreviewImgId != "" && !strings.HasPrefix(v.PreviewImgId, "http") {
  235. v.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, v.PreviewImgId)
  236. }
  237. }
  238. redis.Put("other", topKey, list, 60*5)
  239. }
  240. return list, nil
  241. }()
  242. if errMsg != nil {
  243. log.Printf("%s StdDoc topList err:%s\n", userId, errMsg.Error())
  244. }
  245. stdDoc.ServeJson(NewResult(rData, errMsg))
  246. }
  247. func (stdDoc *StdDoc) ActivityList() {
  248. userId := common.ObjToString(stdDoc.GetSession("userId"))
  249. rData, errMsg := func() (interface{}, error) {
  250. code, _ := stdDoc.GetInt("code")
  251. pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始
  252. pageSizeReq, _ := stdDoc.GetInt("size") //每页数量
  253. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, 20*10)
  254. if err != nil {
  255. return nil, err
  256. }
  257. //存入redis缓存
  258. list, err := rpc.GeActivityList(userId, code, pageNum, pageSize)
  259. if err != nil {
  260. return nil, err
  261. }
  262. if list != nil && len(list) > 0 {
  263. for i := 0; i < len(list); i++ {
  264. list[i].DocImg = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, list[i].DocImg)
  265. }
  266. }
  267. return list, nil
  268. }()
  269. if errMsg != nil {
  270. log.Printf("%s StdDoc activityList err:%s\n", userId, errMsg.Error())
  271. }
  272. stdDoc.ServeJson(NewResult(rData, errMsg))
  273. }