stdDoc.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. userInfo := public.GetUserBaseInfo(stdDoc.Session())
  174. userId := userInfo.UserId
  175. rData, errMsg := func() (interface{}, error) {
  176. docId := stdDoc.GetString("docId")
  177. if docId == "" {
  178. return nil, fmt.Errorf("参数异常")
  179. }
  180. detail, isBuy, _, err := rpc.GetDocDetail(userId, docId, false)
  181. if err != nil {
  182. return nil, err
  183. }
  184. if !isBuy {
  185. return nil, fmt.Errorf("请先兑换文档")
  186. }
  187. fileId := detail.OssPdfId
  188. if sign == "Down" {
  189. fileId = detail.OssDocId
  190. if b, _ := redis.Exists(public.RedisCode, fmt.Sprintf("file_upload_ing_%s", fileId)); b {
  191. return nil, fmt.Errorf("文档正在上传中,请稍后再试")
  192. }
  193. if detail.OssDocId == "" {
  194. // 下载接口
  195. _, err := rpc.PartDocDownload(docId, userInfo.MgoUserId, userInfo.Phone, userInfo.PositionId)
  196. if err != nil {
  197. log.Println("GetDoc PartDocDownload 获取失败")
  198. }
  199. }
  200. }
  201. domain := config.JyDocsAppConfig.OssBucket.Std
  202. if detail.Source == public.SourceDd {
  203. domain = config.JyDocsAppConfig.OssBucket.Docin
  204. }
  205. url, err := rpc.GetFileContext(userId, fileId, domain)
  206. if err != nil {
  207. return nil, err
  208. }
  209. if strings.HasPrefix(url, "http://") {
  210. url = strings.Replace(url, "http://", "https://", 1)
  211. }
  212. return url, nil
  213. }()
  214. if errMsg != nil {
  215. log.Printf("%s StdDoc content err:%s\n", userId, errMsg.Error())
  216. }
  217. stdDoc.ServeJson(NewResult(rData, errMsg))
  218. }
  219. func (stdDoc *StdDoc) TopList() {
  220. userId := common.ObjToString(stdDoc.GetSession("userId"))
  221. rData, errMsg := func() (interface{}, error) {
  222. num, _ := stdDoc.GetInt("num") //返回数量
  223. sign := stdDoc.GetString("sign") //类别
  224. reqSort := ""
  225. if num > 50 {
  226. num = 50
  227. }
  228. if sign == "hot" {
  229. reqSort = "dSort"
  230. } else if sign == "new" {
  231. reqSort = "tSort"
  232. } else {
  233. return nil, fmt.Errorf("未知请求")
  234. }
  235. topKey := fmt.Sprintf("jydoc_indexCache_%s_%d", reqSort, num)
  236. listCache := redis.Get("other", topKey)
  237. if listCache != nil {
  238. return listCache, nil
  239. }
  240. log.Println("flush", topKey)
  241. list, _, err := rpc.GetDocQuery(userId, "", "", 1, num, reqSort, 0, 0)
  242. if err != nil {
  243. return nil, err
  244. }
  245. if len(list) > 0 { //存入redis缓存
  246. for _, v := range list {
  247. if v.PreviewImgId != "" && !strings.HasPrefix(v.PreviewImgId, "http") {
  248. v.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, v.PreviewImgId)
  249. }
  250. }
  251. redis.Put("other", topKey, list, 60*5)
  252. }
  253. return list, nil
  254. }()
  255. if errMsg != nil {
  256. log.Printf("%s StdDoc topList err:%s\n", userId, errMsg.Error())
  257. }
  258. stdDoc.ServeJson(NewResult(rData, errMsg))
  259. }
  260. func (stdDoc *StdDoc) ActivityList() {
  261. userId := common.ObjToString(stdDoc.GetSession("userId"))
  262. rData, errMsg := func() (interface{}, error) {
  263. code, _ := stdDoc.GetInt("code")
  264. pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始
  265. pageSizeReq, _ := stdDoc.GetInt("size") //每页数量
  266. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, 20*10)
  267. if err != nil {
  268. return nil, err
  269. }
  270. //存入redis缓存
  271. list, err := rpc.GeActivityList(userId, code, pageNum, pageSize)
  272. if err != nil {
  273. return nil, err
  274. }
  275. if list != nil && len(list) > 0 {
  276. for i := 0; i < len(list); i++ {
  277. list[i].DocImg = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, list[i].DocImg)
  278. }
  279. }
  280. return list, nil
  281. }()
  282. if errMsg != nil {
  283. log.Printf("%s StdDoc activityList err:%s\n", userId, errMsg.Error())
  284. }
  285. stdDoc.ServeJson(NewResult(rData, errMsg))
  286. }