stdDoc.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. "fmt"
  7. "jy-docs/config"
  8. "jy-docs/public"
  9. "jy-docs/rpc"
  10. "log"
  11. "strings"
  12. )
  13. type StdDoc struct {
  14. *xweb.Action
  15. search xweb.Mapper `xweb:"/search"` //检索文库
  16. indexTag xweb.Mapper `xweb:"/indexTag"` //首页搜索标签
  17. detail xweb.Mapper `xweb:"/detail"` //文库详情
  18. recommend xweb.Mapper `xweb:"/detail/recommend"` //相关推荐
  19. getDoc xweb.Mapper `xweb:"/get(Show|Down)"` //文库在线查看 or 下载
  20. topList xweb.Mapper `xweb:"/topList"` //最新文档&热门下载
  21. activityList xweb.Mapper `xweb:"/activityList"` //活动文库(精品推荐、兑换榜)
  22. }
  23. func (stdDoc *StdDoc) Search() {
  24. userId := common.ObjToString(stdDoc.GetSession("userId"))
  25. rData, errMsg := func() (interface{}, error) {
  26. keyWord := strings.TrimSpace(stdDoc.GetString("keyWord")) //关键词
  27. tag := stdDoc.GetString("tag") //标签
  28. sort := stdDoc.GetString("sort") //排序 tSort dSort vSort
  29. pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始
  30. pageSizeReq, _ := stdDoc.GetInt("size") //每页数量
  31. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, config.JyDocsAppConfig.SearchNumLimit)
  32. if err != nil {
  33. return nil, err
  34. }
  35. if keyWord == "" {
  36. return nil, fmt.Errorf("检索内容不能为空")
  37. }
  38. if tag == "全部" {
  39. tag = ""
  40. }
  41. list, total, err := rpc.GetDocQuery(userId, keyWord, tag, pageNum, pageSize, sort)
  42. if err != nil {
  43. return nil, err
  44. }
  45. if total > config.JyDocsAppConfig.SearchNumLimit {
  46. total = config.JyDocsAppConfig.SearchNumLimit
  47. }
  48. return map[string]interface{}{
  49. "total": total,
  50. "list": list,
  51. }, nil
  52. }()
  53. if errMsg != nil {
  54. log.Printf("%s StdDoc search err:%s\n", userId, errMsg.Error())
  55. }
  56. stdDoc.ServeJson(NewResult(rData, errMsg))
  57. }
  58. func (stdDoc *StdDoc) IndexTag() {
  59. stdDoc.ServeJson(NewResult(config.JyDocsAppConfig.IndexSearchTag, nil))
  60. }
  61. func (stdDoc *StdDoc) Detail() {
  62. userId := common.ObjToString(stdDoc.GetSession("userId"))
  63. rData, errMsg := func() (interface{}, error) {
  64. docId := stdDoc.GetString("docId")
  65. from := stdDoc.GetString("from")
  66. if docId == "" {
  67. return nil, fmt.Errorf("参数异常")
  68. }
  69. if from != "" { //分享赚积分
  70. go public.OpenShareJydoc(from, userId, docId)
  71. }
  72. detail, isBuy, IsCollect, err := rpc.GetDocDetail(userId, docId)
  73. if err != nil {
  74. return nil, err
  75. }
  76. //ossId清除
  77. detail.OssPdfId = ""
  78. detail.OssDocId = ""
  79. detail.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, detail.PreviewImgId)
  80. go rpc.DocStatistics(userId, docId, rpc.View) //统计下载次数
  81. return map[string]interface{}{
  82. "status": common.If(isBuy, 1, 0),
  83. "collect": common.If(IsCollect, 1, 0),
  84. "detail": detail,
  85. }, nil
  86. }()
  87. if errMsg != nil {
  88. log.Printf("%s StdDoc detail err:%s\n", userId, errMsg.Error())
  89. }
  90. stdDoc.ServeJson(NewResult(rData, errMsg))
  91. }
  92. func (stdDoc *StdDoc) Recommend() {
  93. userId := common.ObjToString(stdDoc.GetSession("userId"))
  94. rData, errMsg := func() (interface{}, error) {
  95. docId := stdDoc.GetString("docId")
  96. docTag := stdDoc.GetString("docTag")
  97. num, _ := stdDoc.GetInt("num")
  98. num = public.PageRange(num, 1, 10)
  99. if strings.Index(docTag, ",") > -1 {
  100. docTag = strings.Split(docTag, ",")[0]
  101. }
  102. list, _, err := rpc.GetDocQuery(userId, "", docTag, 1, num+1, "dSort")
  103. if err != nil {
  104. return nil, err
  105. }
  106. returnList := []interface{}{}
  107. for _, v := range list {
  108. if docId == v.DocId || len(returnList) >= common.IntAll(num) {
  109. continue
  110. }
  111. v.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, v.PreviewImgId)
  112. returnList = append(returnList, v)
  113. }
  114. return returnList, nil
  115. }()
  116. if errMsg != nil {
  117. log.Printf("%s StdDoc detail err:%s\n", userId, errMsg.Error())
  118. }
  119. stdDoc.ServeJson(NewResult(rData, errMsg))
  120. }
  121. func (stdDoc *StdDoc) GetDoc(sign string) {
  122. userId := common.ObjToString(stdDoc.GetSession("userId"))
  123. rData, errMsg := func() (interface{}, error) {
  124. docId := stdDoc.GetString("docId")
  125. if docId == "" {
  126. return nil, fmt.Errorf("参数异常")
  127. }
  128. detail, isBuy, _, err := rpc.GetDocDetail(userId, docId)
  129. if err != nil {
  130. return nil, err
  131. }
  132. if !isBuy {
  133. return nil, fmt.Errorf("请先兑换文档")
  134. }
  135. fileId := detail.OssPdfId
  136. if sign == "Down" {
  137. fileId = detail.OssDocId
  138. go rpc.DocStatistics(userId, docId, rpc.Down) //统计下载次数
  139. }
  140. url, err := rpc.GetFileContext(userId, fileId)
  141. if err != nil {
  142. return nil, err
  143. }
  144. if strings.HasPrefix(url, "http://") {
  145. url = strings.Replace(url, "http://", "https://", 1)
  146. }
  147. return url, nil
  148. }()
  149. if errMsg != nil {
  150. log.Printf("%s StdDoc content err:%s\n", userId, errMsg.Error())
  151. }
  152. stdDoc.ServeJson(NewResult(rData, errMsg))
  153. }
  154. func (stdDoc *StdDoc) TopList() {
  155. userId := common.ObjToString(stdDoc.GetSession("userId"))
  156. rData, errMsg := func() (interface{}, error) {
  157. num, _ := stdDoc.GetInt("num") //返回数量
  158. sign := stdDoc.GetString("sign") //类别
  159. reqSort := ""
  160. if num > 50 {
  161. num = 50
  162. }
  163. if sign == "hot" {
  164. reqSort = "dSort"
  165. } else if sign == "new" {
  166. reqSort = "tSort"
  167. } else {
  168. return nil, fmt.Errorf("未知请求")
  169. }
  170. //存入redis缓存
  171. list, _, err := rpc.GetDocQuery(userId, "", "", 1, num, reqSort)
  172. if err != nil {
  173. return nil, err
  174. }
  175. return list, nil
  176. }()
  177. if errMsg != nil {
  178. log.Printf("%s StdDoc topList err:%s\n", userId, errMsg.Error())
  179. }
  180. stdDoc.ServeJson(NewResult(rData, errMsg))
  181. }
  182. func (stdDoc *StdDoc) ActivityList() {
  183. userId := common.ObjToString(stdDoc.GetSession("userId"))
  184. rData, errMsg := func() (interface{}, error) {
  185. code, _ := stdDoc.GetInt("code")
  186. pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始
  187. pageSizeReq, _ := stdDoc.GetInt("size") //每页数量
  188. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, 20*10)
  189. if err != nil {
  190. return nil, err
  191. }
  192. //存入redis缓存
  193. list, err := rpc.GeActivityList(userId, code, pageNum, pageSize)
  194. if err != nil {
  195. return nil, err
  196. }
  197. if list != nil && len(list) > 0 {
  198. for i := 0; i < len(list); i++ {
  199. list[i].DocImg = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, list[i].DocImg)
  200. }
  201. }
  202. return list, nil
  203. }()
  204. if errMsg != nil {
  205. log.Printf("%s StdDoc activityList err:%s\n", userId, errMsg.Error())
  206. }
  207. stdDoc.ServeJson(NewResult(rData, errMsg))
  208. }