package controller import ( "encoding/json" "eta/eta_docs/controller/resp" "eta/eta_docs/global" "eta/eta_docs/models/crm" "eta/eta_docs/models/request" "eta/eta_docs/services" "eta/eta_docs/utils" "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "html" ) // HelpDocController 帮助文档 type HelpDocController struct{} // ClassifyList // @Description 帮助文档分类列表 // @Success 200 {string} string "获取成功" // @Router /help_doc/classify/list [get] func (a *HelpDocController) ClassifyList(c *gin.Context) { var req request.HelpDocClassifyListReq err := c.Bind(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } etaOb := new(crm.EtaBusiness) var pars []interface{} pars = append(pars, req.BusCode) etaBusiness, e := etaOb.GetItemByCondition("code_encrypt=?", pars, "") if e != nil { resp.FailMsg("获取失败", "获取帮助文档列表失败, Err: "+e.Error(), c) return } nodeAll := make([]*crm.HelpDocClassifyItems, 0) if etaBusiness != nil { // 获取列表数据 docOb := new(crm.HelpDocClassify) var pars []interface{} pars = append(pars, etaBusiness.EtaBusinessId) rootList, e := docOb.GetItemsByCondition("parent_id=0 AND FIND_IN_SET(?,visible_business_ids) > 0 ", pars, "sort asc,classify_id asc") if e != nil { resp.FailMsg("获取失败", "获取帮助文档列表失败, Err: "+e.Error(), c) return } classifyAll, e := docOb.GetItemsByCondition("FIND_IN_SET(?,visible_business_ids) > 0", pars, "sort asc,classify_id asc") if e != nil { resp.FailMsg("获取失败", "获取帮助文档列表失败, Err: "+e.Error(), c) return } classifies := make([]*crm.HelpDocClassifyItems, 0) for _, v := range classifyAll { child := make([]*crm.HelpDocClassifyItems, 0) item := crm.HelpDocClassifyItems{ ClassifyId: v.ClassifyId, ClassifyName: v.ClassifyName, ParentId: v.ParentId, Level: v.Level, Sort: v.Sort, SysUserId: v.SysUserId, SysUserRealName: v.SysUserRealName, VisibleBusinessIds: v.VisibleBusinessIds, Children: child, } classifies = append(classifies, &item) } roots := make([]*crm.HelpDocClassifyItems, 0) for _, v := range rootList { child := make([]*crm.HelpDocClassifyItems, 0) item := crm.HelpDocClassifyItems{ ClassifyId: v.ClassifyId, ClassifyName: v.ClassifyName, ParentId: v.ParentId, Level: v.Level, Sort: v.Sort, SysUserId: v.SysUserId, SysUserRealName: v.SysUserRealName, VisibleBusinessIds: v.VisibleBusinessIds, Children: child, } roots = append(roots, &item) } for k := range roots { rootNode := roots[k] services.HelpDocClassifyItemsMakeTree(classifies, rootNode) nodeAll = append(nodeAll, rootNode) } } resp.OkData("获取成功", nodeAll, c) } // Detail // @Description 文章详情 // @Success 200 {string} string "获取成功" // @Router /help_doc/detail [get] func (a *HelpDocController) Detail(c *gin.Context) { var req request.HelpDocClassifyDetailReq err := c.Bind(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } // 获取列表数据 docOb := new(crm.HelpDoc) var pars []interface{} pars = append(pars, req.ClassifyId) item, e := docOb.GetItemByCondition("classify_id=?", pars, "") if e != nil { if e == utils.ErrNoRow { resp.SpecificFail("获取失败", "文章不存在, Err: "+e.Error(), c) return } resp.FailMsg("获取失败", "获取帮助文档列表失败, Err: "+e.Error(), c) return } if item.Status != 2 { resp.SpecificFail("获取失败", "文章未发布 ", c) return } item.Content = html.UnescapeString(item.Content) var anchor []crm.AnchorList if item.Anchor != "" { err = json.Unmarshal([]byte(item.Anchor), &anchor) if err != nil { resp.FailMsg("解析失败", "解析锚点失败, Err: "+e.Error(), c) return } } var recommend []crm.RecommendList if item.Recommend != "" { err = json.Unmarshal([]byte(item.Recommend), &recommend) if err != nil { resp.FailMsg("解析失败", "解析推荐链接失败, Err: "+e.Error(), c) return } } respItem := crm.HelpDocResp{ Id: item.Id, ClassifyId: item.ClassifyId, Title: item.Title, Author: item.Author, CreateTime: item.CreateTime.Format(utils.FormatDateTime), ModifyTime: item.ModifyTime.Format(utils.FormatDateTime), Status: item.Status, PublishTime: item.PublishTime.Format(utils.FormatDateTime), Content: item.Content, AdminId: item.AdminId, AdminRealName: item.AdminRealName, Anchor: anchor, Recommend: recommend, } if item.PublishTime.IsZero() { respItem.PublishTime = "" } resp.OkData("获取成功", respItem, c) }