kefu/controller/article.go

264 lines
6.0 KiB
Go

package controller
import (
"github.com/gin-gonic/gin"
"kefu/models"
"kefu/tools"
"kefu/types"
"strconv"
)
func PostArticleCate(c *gin.Context) {
kefuName, _ := c.Get("kefu_name")
entIdStr, _ := c.Get("ent_id")
entId, _ := entIdStr.(string)
catName := c.PostForm("name")
if catName == "" {
c.JSON(200, gin.H{
"code": types.ApiCode.FAILED,
"msg": types.ApiCode.GetMessage(types.ApiCode.INVALID),
})
return
}
catId := c.PostForm("id")
if catId != "" {
cate := &models.ArticleCate{
CatName: catName,
}
cate.SaveArticleCate("ent_id = ? and id = ? ", entIdStr, catId)
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
})
} else {
cateModel := &models.ArticleCate{
CatName: catName,
UserId: kefuName.(string),
IsTop: 0,
EntId: entId,
}
cateModel.AddArticleCate()
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
})
}
}
func PostArticle(c *gin.Context) {
kefuName, _ := c.Get("kefu_name")
entIdStr, _ := c.Get("ent_id")
entId, _ := entIdStr.(string)
title := c.PostForm("title")
content := c.PostForm("content")
catId := c.PostForm("cat_id")
id := c.PostForm("id")
apiUrl := c.PostForm("api_url")
searchType := c.PostForm("search_type")
if title == "" || content == "" || catId == "" || searchType == "" {
c.JSON(200, gin.H{
"code": types.ApiCode.FAILED,
"msg": types.ApiCode.GetMessage(types.ApiCode.INVALID),
})
return
}
catIdInt, _ := strconv.Atoi(catId)
//编辑文章
if id != "" {
articleModel := &models.Article{
Title: title,
Content: content,
CatId: uint(catIdInt),
UserId: kefuName.(string),
EntId: entId,
ApiUrl: apiUrl,
SearchType: tools.Str2Uint(searchType),
}
articleModel.SaveArticle("ent_id = ? and id = ?", entId, id)
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
})
return
}
articleModel := &models.Article{
Title: title,
Content: content,
CatId: uint(catIdInt),
UserId: kefuName.(string),
EntId: entId,
ApiUrl: apiUrl,
SearchType: tools.Str2Uint(searchType),
}
articleModel.AddArticle()
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
})
}
func DelArticle(c *gin.Context) {
entIdStr, _ := c.Get("ent_id")
articleId := c.Query("id")
if articleId == "" {
c.JSON(200, gin.H{
"code": types.ApiCode.FAILED,
"msg": types.ApiCode.GetMessage(types.ApiCode.INVALID),
})
return
}
models.DelArticles("ent_id = ? and id = ? ", entIdStr, articleId)
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
})
}
func DelArticleCate(c *gin.Context) {
entIdStr, _ := c.Get("ent_id")
catId := c.Query("id")
if catId == "" {
c.JSON(200, gin.H{
"code": types.ApiCode.FAILED,
"msg": types.ApiCode.GetMessage(types.ApiCode.INVALID),
})
return
}
models.DelArticleCate("ent_id = ? and id = ? ", entIdStr, catId)
models.DelArticles("ent_id = ? and cat_id = ? ", entIdStr, catId)
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
})
}
func GetTopQuestion(c *gin.Context) {
entIdStr := c.Query("ent_id")
systemHotQaBussinesId := models.FindConfig("SystemHotQaBussinesId")
if systemHotQaBussinesId != "" {
entIdStr = systemHotQaBussinesId
}
cates := models.FindArticleCates("ent_id = ? and is_top = 1 ", entIdStr)
catResult := make(map[string][]string)
result := make([]string, 0)
if len(cates) != 0 {
for _, cate := range cates {
articles := models.FindArticleList(1, 10, "", "cat_id = ? ", cate.Id)
catResult[cate.CatName] = make([]string, 0)
for _, article := range articles {
catResult[cate.CatName] = append(catResult[cate.CatName], article.Title)
result = append(result, article.Title)
}
}
}
hotQuestion := ""
config := models.FindEntConfig(entIdStr, "VisitorQaKeywords")
if config.ConfValue != "" {
hotQuestion = config.ConfValue
}
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": gin.H{
"catResult": catResult,
"hotQuestion": hotQuestion,
"questionList": result,
},
})
}
// 搜索问题
func GetSearchQuestion(c *gin.Context) {
content := c.Query("content")
entId := c.Query("ent_id")
var articles []models.Article
if content == "" {
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": articles,
})
return
}
articles = models.FindArticleList(1, 10, "score desc", "ent_id= ? and title like ?", entId, "%"+content+"%")
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": articles,
})
}
func SetArticleCateTop(c *gin.Context) {
entIdStr, _ := c.Get("ent_id")
catId := c.Query("id")
isTop := c.Query("is_top")
if catId == "" {
c.JSON(200, gin.H{
"code": types.ApiCode.FAILED,
"msg": types.ApiCode.GetMessage(types.ApiCode.INVALID),
})
return
}
top, _ := strconv.Atoi(isTop)
cate := &models.ArticleCate{
IsTop: uint(top),
}
cate.SaveArticleCate("ent_id = ? and id = ? ", entIdStr, catId)
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
})
}
func GetArticleCates(c *gin.Context) {
entId, _ := c.Get("ent_id")
list := models.FindArticleCatesByEnt(entId)
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": list,
})
}
func GetArticleList(c *gin.Context) {
catId := c.Query("cat_id")
entId, _ := c.Get("ent_id")
roleId, _ := c.Get("role_id")
kefuName, _ := c.Get("kefu_name")
page, _ := strconv.Atoi(c.Query("page"))
if page <= 0 {
page = 1
}
pagesize, _ := strconv.Atoi(c.Query("pagesize"))
if pagesize <= 0 || pagesize > 10000 {
pagesize = 10
}
search := "ent_id = ? "
args := []interface{}{
entId,
}
//坐席权限
if roleId.(float64) == 3 {
search += "and user_id = ? "
args = append(args, kefuName)
}
if catId != "" {
search += "and cat_id = ? "
args = append(args, catId)
}
//排序相关
prop := c.Query("prop")
order := c.DefaultQuery("order", "desc")
orderBy := ""
if prop != "" {
orderBy = prop + " " + order
}
count := models.CountArticleList(search, args...)
list := models.FindArticleList(uint(page), uint(pagesize), orderBy, search, args...)
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": gin.H{
"list": list,
"count": count,
"pagesize": uint(pagesize),
"page": page,
},
})
}