stdDoc.go 9.3 KB

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