stdDoc.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/public"
  8. "jy-docs/rpc"
  9. "log"
  10. "strings"
  11. )
  12. type StdDoc struct {
  13. *xweb.Action
  14. search xweb.Mapper `xweb:"/search"` //检索文库
  15. detail xweb.Mapper `xweb:"/detail"` //文库详情
  16. content xweb.Mapper `xweb:"/content"` //文库内容
  17. topList xweb.Mapper `xweb:"/topList"` //最新文档&热门下载
  18. activityList xweb.Mapper `xweb:"/activityList"` //活动文库(精品推荐、兑换榜)
  19. }
  20. func (stdDoc *StdDoc) Search() {
  21. userId := common.ObjToString(stdDoc.GetSession("userId"))
  22. rData, errMsg := func() (interface{}, error) {
  23. keyWord := strings.TrimSpace(stdDoc.GetString("keyWord")) //关键词
  24. tag := stdDoc.GetString("tag") //标签
  25. sort := stdDoc.GetString("sort") //排序 tSort dSort vSort
  26. pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始
  27. pageSizeReq, _ := stdDoc.GetInt("size") //每页数量
  28. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, 20*10)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if keyWord == "" {
  33. return nil, fmt.Errorf("检索内容不能为空")
  34. }
  35. list, total, err := rpc.GetDocQuery(userId, keyWord, tag, pageNum, pageSize, sort)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return map[string]interface{}{
  40. "total": total,
  41. "list": list,
  42. }, nil
  43. }()
  44. if errMsg != nil {
  45. log.Printf("%s StdDoc search err:%s\n", userId, errMsg.Error())
  46. }
  47. stdDoc.ServeJson(NewResult(rData, errMsg))
  48. }
  49. func (stdDoc *StdDoc) Detail() {
  50. userId := common.ObjToString(stdDoc.GetSession("userId"))
  51. rData, errMsg := func() (interface{}, error) {
  52. docId := stdDoc.GetString("docId")
  53. if docId == "" {
  54. return nil, fmt.Errorf("参数异常")
  55. }
  56. detail, isBuy, err := rpc.GetDocDetail(userId, docId)
  57. if err != nil {
  58. return nil, err
  59. }
  60. return map[string]interface{}{
  61. "status": common.If(isBuy, 1, 0),
  62. "detail": detail,
  63. }, nil
  64. }()
  65. if errMsg != nil {
  66. log.Printf("%s StdDoc detail err:%s\n", userId, errMsg.Error())
  67. }
  68. stdDoc.ServeJson(NewResult(rData, errMsg))
  69. }
  70. func (stdDoc *StdDoc) Content() {
  71. userId := common.ObjToString(stdDoc.GetSession("userId"))
  72. rData, errMsg := func() (interface{}, error) {
  73. return nil, nil
  74. }()
  75. if errMsg != nil {
  76. log.Printf("%s StdDoc content err:%s\n", userId, errMsg.Error())
  77. }
  78. stdDoc.ServeJson(NewResult(rData, errMsg))
  79. }
  80. func (stdDoc *StdDoc) TopList() {
  81. userId := common.ObjToString(stdDoc.GetSession("userId"))
  82. rData, errMsg := func() (interface{}, error) {
  83. num, _ := stdDoc.GetInt("num") //返回数量
  84. sign := stdDoc.GetString("sign") //类别
  85. reqSort := ""
  86. if num > 50 {
  87. num = 50
  88. }
  89. if sign == "hot" {
  90. reqSort = "dSort"
  91. } else if sign == "new" {
  92. reqSort = "tSort"
  93. } else {
  94. return nil, fmt.Errorf("未知请求")
  95. }
  96. list, _, err := rpc.GetDocQuery(userId, "", "", 1, num, reqSort)
  97. if err != nil {
  98. return nil, err
  99. }
  100. return list, nil
  101. }()
  102. if errMsg != nil {
  103. log.Printf("%s StdDoc topList err:%s\n", userId, errMsg.Error())
  104. }
  105. stdDoc.ServeJson(NewResult(rData, errMsg))
  106. }
  107. func (stdDoc *StdDoc) ActivityList() {
  108. userId := common.ObjToString(stdDoc.GetSession("userId"))
  109. rData, errMsg := func() (interface{}, error) {
  110. code, _ := stdDoc.GetInt("code")
  111. pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始
  112. pageSizeReq, _ := stdDoc.GetInt("size") //每页数量
  113. pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, 20*10)
  114. if err != nil {
  115. return nil, err
  116. }
  117. list, err := rpc.GeActivityList(userId, code, pageNum, pageSize)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return list, nil
  122. }()
  123. if errMsg != nil {
  124. log.Printf("%s StdDoc activityList err:%s\n", userId, errMsg.Error())
  125. }
  126. stdDoc.ServeJson(NewResult(rData, errMsg))
  127. }