stdDoc.go 9.2 KB

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