stdDoc.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.DocSummary = ""
  112. v.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, v.PreviewImgId)
  113. returnList = append(returnList, v)
  114. }
  115. return returnList, nil
  116. }()
  117. if errMsg != nil {
  118. log.Printf("%s StdDoc detail err:%s\n", userId, errMsg.Error())
  119. }
  120. stdDoc.ServeJson(NewResult(rData, errMsg))
  121. }
  122. func (stdDoc *StdDoc) GetDoc(sign string) {
  123. userId := common.ObjToString(stdDoc.GetSession("userId"))
  124. rData, errMsg := func() (interface{}, error) {
  125. docId := stdDoc.GetString("docId")
  126. if docId == "" {
  127. return nil, fmt.Errorf("参数异常")
  128. }
  129. detail, isBuy, _, err := rpc.GetDocDetail(userId, docId)
  130. if err != nil {
  131. return nil, err
  132. }
  133. if !isBuy {
  134. return nil, fmt.Errorf("请先兑换文档")
  135. }
  136. fileId := detail.OssPdfId
  137. if sign == "Down" {
  138. fileId = detail.OssDocId
  139. go rpc.DocStatistics(userId, docId, rpc.Down) //统计下载次数
  140. }
  141. url, err := rpc.GetFileContext(userId, fileId)
  142. if err != nil {
  143. return nil, err
  144. }
  145. if strings.HasPrefix(url, "http://") {
  146. url = strings.Replace(url, "http://", "https://", 1)
  147. }
  148. return url, nil
  149. }()
  150. if errMsg != nil {
  151. log.Printf("%s StdDoc content err:%s\n", userId, errMsg.Error())
  152. }
  153. stdDoc.ServeJson(NewResult(rData, errMsg))
  154. }
  155. func (stdDoc *StdDoc) TopList() {
  156. userId := common.ObjToString(stdDoc.GetSession("userId"))
  157. rData, errMsg := func() (interface{}, error) {
  158. num, _ := stdDoc.GetInt("num") //返回数量
  159. sign := stdDoc.GetString("sign") //类别
  160. reqSort := ""
  161. if num > 50 {
  162. num = 50
  163. }
  164. if sign == "hot" {
  165. reqSort = "dSort"
  166. } else if sign == "new" {
  167. reqSort = "tSort"
  168. } else {
  169. return nil, fmt.Errorf("未知请求")
  170. }
  171. //存入redis缓存
  172. list, _, err := rpc.GetDocQuery(userId, "", "", 1, num, reqSort)
  173. if err != nil {
  174. return nil, err
  175. }
  176. return list, nil
  177. }()
  178. if errMsg != nil {
  179. log.Printf("%s StdDoc topList err:%s\n", userId, errMsg.Error())
  180. }
  181. stdDoc.ServeJson(NewResult(rData, errMsg))
  182. }
  183. func (stdDoc *StdDoc) ActivityList() {
  184. userId := common.ObjToString(stdDoc.GetSession("userId"))
  185. rData, errMsg := func() (interface{}, error) {
  186. code, _ := stdDoc.GetInt("code")
  187. pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始
  188. pageSizeReq, _ := stdDoc.GetInt("size") //每页数量
  189. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, 20*10)
  190. if err != nil {
  191. return nil, err
  192. }
  193. //存入redis缓存
  194. list, err := rpc.GeActivityList(userId, code, pageNum, pageSize)
  195. if err != nil {
  196. return nil, err
  197. }
  198. if list != nil && len(list) > 0 {
  199. for i := 0; i < len(list); i++ {
  200. list[i].DocImg = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, list[i].DocImg)
  201. }
  202. }
  203. return list, nil
  204. }()
  205. if errMsg != nil {
  206. log.Printf("%s StdDoc activityList err:%s\n", userId, errMsg.Error())
  207. }
  208. stdDoc.ServeJson(NewResult(rData, errMsg))
  209. }