help_doc.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package controller
  2. import (
  3. "encoding/json"
  4. "eta/eta_docs/controller/resp"
  5. "eta/eta_docs/global"
  6. "eta/eta_docs/models/crm"
  7. "eta/eta_docs/models/request"
  8. "eta/eta_docs/services"
  9. "eta/eta_docs/utils"
  10. "github.com/gin-gonic/gin"
  11. "github.com/go-playground/validator/v10"
  12. "html"
  13. )
  14. // HelpDocController 帮助文档
  15. type HelpDocController struct{}
  16. // ClassifyList
  17. // @Description 帮助文档分类列表
  18. // @Success 200 {string} string "获取成功"
  19. // @Router /help_doc/classify/list [get]
  20. func (a *HelpDocController) ClassifyList(c *gin.Context) {
  21. var req request.HelpDocClassifyListReq
  22. err := c.Bind(&req)
  23. if err != nil {
  24. errs, ok := err.(validator.ValidationErrors)
  25. if !ok {
  26. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  27. return
  28. }
  29. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  30. return
  31. }
  32. etaOb := new(crm.EtaBusiness)
  33. var pars []interface{}
  34. pars = append(pars, req.BusCode)
  35. etaBusiness, e := etaOb.GetItemByCondition("code_encrypt=?", pars, "")
  36. if e != nil {
  37. resp.FailMsg("获取失败", "获取帮助文档列表失败, Err: "+e.Error(), c)
  38. return
  39. }
  40. nodeAll := make([]*crm.HelpDocClassifyItems, 0)
  41. if etaBusiness != nil {
  42. // 获取列表数据
  43. docOb := new(crm.HelpDocClassify)
  44. var pars []interface{}
  45. pars = append(pars, etaBusiness.EtaBusinessId)
  46. rootList, e := docOb.GetItemsByCondition("parent_id=0 AND FIND_IN_SET(?,visible_business_ids) > 0 ", pars, "sort asc,classify_id asc")
  47. if e != nil {
  48. resp.FailMsg("获取失败", "获取帮助文档列表失败, Err: "+e.Error(), c)
  49. return
  50. }
  51. classifyAll, e := docOb.GetItemsByCondition("FIND_IN_SET(?,visible_business_ids) > 0", pars, "sort asc,classify_id asc")
  52. if e != nil {
  53. resp.FailMsg("获取失败", "获取帮助文档列表失败, Err: "+e.Error(), c)
  54. return
  55. }
  56. classifies := make([]*crm.HelpDocClassifyItems, 0)
  57. for _, v := range classifyAll {
  58. child := make([]*crm.HelpDocClassifyItems, 0)
  59. item := crm.HelpDocClassifyItems{
  60. ClassifyId: v.ClassifyId,
  61. ClassifyName: v.ClassifyName,
  62. ParentId: v.ParentId,
  63. Level: v.Level,
  64. Sort: v.Sort,
  65. SysUserId: v.SysUserId,
  66. SysUserRealName: v.SysUserRealName,
  67. VisibleBusinessIds: v.VisibleBusinessIds,
  68. Children: child,
  69. }
  70. classifies = append(classifies, &item)
  71. }
  72. roots := make([]*crm.HelpDocClassifyItems, 0)
  73. for _, v := range rootList {
  74. child := make([]*crm.HelpDocClassifyItems, 0)
  75. item := crm.HelpDocClassifyItems{
  76. ClassifyId: v.ClassifyId,
  77. ClassifyName: v.ClassifyName,
  78. ParentId: v.ParentId,
  79. Level: v.Level,
  80. Sort: v.Sort,
  81. SysUserId: v.SysUserId,
  82. SysUserRealName: v.SysUserRealName,
  83. VisibleBusinessIds: v.VisibleBusinessIds,
  84. Children: child,
  85. }
  86. roots = append(roots, &item)
  87. }
  88. for k := range roots {
  89. rootNode := roots[k]
  90. services.HelpDocClassifyItemsMakeTree(classifies, rootNode)
  91. nodeAll = append(nodeAll, rootNode)
  92. }
  93. }
  94. resp.OkData("获取成功", nodeAll, c)
  95. }
  96. // Detail
  97. // @Description 文章详情
  98. // @Success 200 {string} string "获取成功"
  99. // @Router /help_doc/detail [get]
  100. func (a *HelpDocController) Detail(c *gin.Context) {
  101. var req request.HelpDocClassifyDetailReq
  102. err := c.Bind(&req)
  103. if err != nil {
  104. errs, ok := err.(validator.ValidationErrors)
  105. if !ok {
  106. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  107. return
  108. }
  109. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  110. return
  111. }
  112. // 获取列表数据
  113. docOb := new(crm.HelpDoc)
  114. var pars []interface{}
  115. pars = append(pars, req.ClassifyId)
  116. item, e := docOb.GetItemByCondition("classify_id=?", pars, "")
  117. if e != nil {
  118. if e == utils.ErrNoRow {
  119. resp.SpecificFail("获取失败", "文章不存在, Err: "+e.Error(), c)
  120. return
  121. }
  122. resp.FailMsg("获取失败", "获取帮助文档列表失败, Err: "+e.Error(), c)
  123. return
  124. }
  125. if item.Status != 2 {
  126. resp.SpecificFail("获取失败", "文章未发布 ", c)
  127. return
  128. }
  129. item.Content = html.UnescapeString(item.Content)
  130. var anchor []crm.AnchorList
  131. if item.Anchor != "" {
  132. err = json.Unmarshal([]byte(item.Anchor), &anchor)
  133. if err != nil {
  134. resp.FailMsg("解析失败", "解析锚点失败, Err: "+e.Error(), c)
  135. return
  136. }
  137. }
  138. var recommend []crm.RecommendList
  139. if item.Recommend != "" {
  140. err = json.Unmarshal([]byte(item.Recommend), &recommend)
  141. if err != nil {
  142. resp.FailMsg("解析失败", "解析推荐链接失败, Err: "+e.Error(), c)
  143. return
  144. }
  145. }
  146. respItem := crm.HelpDocResp{
  147. Id: item.Id,
  148. ClassifyId: item.ClassifyId,
  149. Title: item.Title,
  150. Author: item.Author,
  151. CreateTime: item.CreateTime.Format(utils.FormatDateTime),
  152. ModifyTime: item.ModifyTime.Format(utils.FormatDateTime),
  153. Status: item.Status,
  154. PublishTime: item.PublishTime.Format(utils.FormatDateTime),
  155. Content: item.Content,
  156. AdminId: item.AdminId,
  157. AdminRealName: item.AdminRealName,
  158. Anchor: anchor,
  159. Recommend: recommend,
  160. }
  161. if item.PublishTime.IsZero() {
  162. respItem.PublishTime = ""
  163. }
  164. resp.OkData("获取成功", respItem, c)
  165. }