811 lines
20 KiB
Go
811 lines
20 KiB
Go
package controllers
|
||
|
||
import (
|
||
"encoding/json"
|
||
"server/models"
|
||
"server/services"
|
||
"strconv"
|
||
"time"
|
||
|
||
"github.com/beego/beego/v2/server/web"
|
||
)
|
||
|
||
// ArticlesController 文章管理控制器
|
||
type ArticlesController struct {
|
||
web.Controller
|
||
}
|
||
|
||
// ListArticles 获取文章列表
|
||
// 支持的状态值:
|
||
// 0=草稿, 1=待审核, 2=已发布, 3=隐藏
|
||
// 不传status参数或传空值时返回所有状态的文章
|
||
func (c *ArticlesController) ListArticles() {
|
||
// 获取查询参数
|
||
page, _ := c.GetInt("page", 1)
|
||
pageSize, _ := c.GetInt("pageSize", 10)
|
||
keyword := c.GetString("keyword")
|
||
cateStr := c.GetString("cate")
|
||
statusStr := c.GetString("status")
|
||
|
||
// 从JWT上下文中获取租户ID和用户ID
|
||
tenantId, _ := c.GetInt("tenantId", 0)
|
||
userId, _ := c.GetInt("userId", 0)
|
||
|
||
var cate int
|
||
var status int8 = -1 // -1表示不筛选状态,显示所有文章
|
||
|
||
if cateStr != "" {
|
||
cate, _ = strconv.Atoi(cateStr)
|
||
}
|
||
|
||
if statusStr != "" {
|
||
statusInt, _ := strconv.Atoi(statusStr)
|
||
status = int8(statusInt)
|
||
}
|
||
|
||
// 调用服务层获取文章列表
|
||
articles, total, err := services.GetArticlesList(tenantId, userId, page, pageSize, keyword, cate, status)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "获取文章列表失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 格式化返回数据
|
||
articleList := make([]map[string]interface{}, 0)
|
||
for _, article := range articles {
|
||
articleList = append(articleList, map[string]interface{}{
|
||
"id": article.Id,
|
||
"title": article.Title,
|
||
"cate": article.Cate,
|
||
"image": article.Image,
|
||
"desc": article.Desc,
|
||
"author": article.Author,
|
||
"content": article.Content,
|
||
"publisher": article.Publisher,
|
||
"publishdate": article.Publishdate,
|
||
"sort": article.Sort,
|
||
"status": article.Status,
|
||
"views": article.Views,
|
||
"likes": article.Likes,
|
||
"is_trans": article.IsTrans,
|
||
"transurl": article.Transurl,
|
||
"push": article.Push,
|
||
"create_time": article.CreateTime,
|
||
"update_time": article.UpdateTime,
|
||
})
|
||
}
|
||
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "获取文章列表成功",
|
||
"data": map[string]interface{}{
|
||
"list": articleList,
|
||
"total": total,
|
||
"page": page,
|
||
"pageSize": pageSize,
|
||
},
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// GetArticle 获取文章详情
|
||
func (c *ArticlesController) GetArticle() {
|
||
// 从URL获取文章ID
|
||
articleId, err := c.GetInt(":id")
|
||
if err != nil || articleId <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "无效的文章ID",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 调用服务层获取文章详情
|
||
article, err := services.GetArticleById(articleId)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "获取文章详情失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
if article == nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "文章不存在",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 增加浏览量
|
||
services.IncrementArticleViews(articleId)
|
||
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "获取文章详情成功",
|
||
"data": map[string]interface{}{
|
||
"id": article.Id,
|
||
"title": article.Title,
|
||
"cate": article.Cate,
|
||
"image": article.Image,
|
||
"desc": article.Desc,
|
||
"author": article.Author,
|
||
"content": article.Content,
|
||
"publisher": article.Publisher,
|
||
"publishdate": article.Publishdate,
|
||
"sort": article.Sort,
|
||
"status": article.Status,
|
||
"views": article.Views + 1, // 返回增加后的浏览量
|
||
"likes": article.Likes,
|
||
"is_trans": article.IsTrans,
|
||
"transurl": article.Transurl,
|
||
"push": article.Push,
|
||
"create_time": article.CreateTime,
|
||
"update_time": article.UpdateTime,
|
||
},
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// CreateArticle 创建文章
|
||
func (c *ArticlesController) CreateArticle() {
|
||
// 定义接收文章数据的结构体
|
||
var articleData struct {
|
||
Title string `json:"title"`
|
||
Cate int `json:"cate"`
|
||
Image string `json:"image"`
|
||
Desc string `json:"desc"`
|
||
Author string `json:"author"`
|
||
Content string `json:"content"`
|
||
Publisher int `json:"publisher"`
|
||
Publishdate string `json:"publishdate"`
|
||
Sort int `json:"sort"`
|
||
Status int8 `json:"status"`
|
||
IsTrans string `json:"is_trans"`
|
||
Transurl string `json:"transurl"`
|
||
Push string `json:"push"`
|
||
}
|
||
|
||
// 解析请求体JSON数据
|
||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &articleData)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "请求参数格式错误: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 校验必要参数
|
||
if articleData.Title == "" {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "文章标题不能为空",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
if articleData.Content == "" {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "文章内容不能为空",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 构建文章对象
|
||
var article models.Articles
|
||
article.Title = articleData.Title
|
||
article.Cate = articleData.Cate
|
||
article.Image = articleData.Image
|
||
article.Desc = articleData.Desc
|
||
article.Author = articleData.Author
|
||
article.Content = articleData.Content
|
||
article.Publisher = articleData.Publisher
|
||
|
||
// 处理发布时间
|
||
if articleData.Publishdate != "" {
|
||
if publishTime, err := time.Parse("2006-01-02 15:04:05", articleData.Publishdate); err == nil {
|
||
article.Publishdate = &publishTime
|
||
} else if publishTime, err := time.Parse("2006-01-02", articleData.Publishdate); err == nil {
|
||
article.Publishdate = &publishTime
|
||
}
|
||
}
|
||
|
||
article.Sort = articleData.Sort
|
||
article.Status = articleData.Status
|
||
article.IsTrans = articleData.IsTrans
|
||
article.Transurl = articleData.Transurl
|
||
article.Push = articleData.Push
|
||
article.CreateTime = time.Now()
|
||
now := time.Now()
|
||
article.UpdateTime = &now
|
||
|
||
// 调用服务层创建文章
|
||
newArticle, err := services.CreateArticle(&article)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "创建文章失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
} else {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "创建文章成功",
|
||
"data": map[string]interface{}{
|
||
"id": newArticle.Id,
|
||
},
|
||
}
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// UpdateArticle 更新文章
|
||
func (c *ArticlesController) UpdateArticle() {
|
||
// 从URL获取文章ID
|
||
articleId, err := c.GetInt(":id")
|
||
if err != nil || articleId <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "无效的文章ID",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 定义接收更新数据的结构体
|
||
var articleData struct {
|
||
Title string `json:"title"`
|
||
Cate int `json:"cate"`
|
||
Image string `json:"image"`
|
||
Desc string `json:"desc"`
|
||
Author string `json:"author"`
|
||
Content string `json:"content"`
|
||
Publisher int `json:"publisher"`
|
||
Publishdate string `json:"publishdate"`
|
||
Sort int `json:"sort"`
|
||
Status int8 `json:"status"`
|
||
IsTrans string `json:"is_trans"`
|
||
Transurl string `json:"transurl"`
|
||
Push string `json:"push"`
|
||
}
|
||
|
||
// 解析请求体JSON数据
|
||
err = json.Unmarshal(c.Ctx.Input.RequestBody, &articleData)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "请求参数格式错误: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 校验必要参数
|
||
if articleData.Title == "" {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "文章标题不能为空",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
if articleData.Content == "" {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "文章内容不能为空",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 构建文章对象
|
||
var article models.Articles
|
||
article.Id = articleId
|
||
article.Title = articleData.Title
|
||
article.Cate = articleData.Cate
|
||
article.Image = articleData.Image
|
||
article.Desc = articleData.Desc
|
||
article.Author = articleData.Author
|
||
article.Content = articleData.Content
|
||
article.Publisher = articleData.Publisher
|
||
|
||
// 处理发布时间
|
||
if articleData.Publishdate != "" {
|
||
if publishTime, err := time.Parse("2006-01-02 15:04:05", articleData.Publishdate); err == nil {
|
||
article.Publishdate = &publishTime
|
||
} else if publishTime, err := time.Parse("2006-01-02", articleData.Publishdate); err == nil {
|
||
article.Publishdate = &publishTime
|
||
}
|
||
}
|
||
|
||
article.Sort = articleData.Sort
|
||
article.Status = articleData.Status
|
||
article.IsTrans = articleData.IsTrans
|
||
article.Transurl = articleData.Transurl
|
||
article.Push = articleData.Push
|
||
now := time.Now()
|
||
article.UpdateTime = &now
|
||
|
||
// 调用服务层更新文章
|
||
err = services.UpdateArticle(&article)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "更新文章失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
} else {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "更新文章成功",
|
||
"data": nil,
|
||
}
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// DeleteArticle 删除文章
|
||
func (c *ArticlesController) DeleteArticle() {
|
||
// 从URL获取文章ID
|
||
articleId, err := c.GetInt(":id")
|
||
if err != nil || articleId <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "无效的文章ID",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 调用服务层删除文章
|
||
err = services.DeleteArticle(articleId)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "删除文章失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
} else {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "删除文章成功",
|
||
"data": nil,
|
||
}
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// UpdateArticleStatus 更新文章状态
|
||
func (c *ArticlesController) UpdateArticleStatus() {
|
||
// 从URL获取文章ID
|
||
articleId, err := c.GetInt(":id")
|
||
if err != nil || articleId <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "无效的文章ID",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 定义接收状态数据的结构体
|
||
var statusData struct {
|
||
Status int8 `json:"status"`
|
||
}
|
||
|
||
// 解析请求体JSON数据
|
||
err = json.Unmarshal(c.Ctx.Input.RequestBody, &statusData)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "请求参数格式错误: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 调用服务层更新文章状态
|
||
err = services.UpdateArticleStatus(articleId, statusData.Status)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "更新文章状态失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
} else {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "更新文章状态成功",
|
||
"data": nil,
|
||
}
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// ListCategories 获取分类列表
|
||
func (c *ArticlesController) ListCategories() {
|
||
|
||
// 获取查询参数
|
||
tenantId, _ := c.GetInt("tenantId")
|
||
page, _ := c.GetInt("page", 1)
|
||
pageSize, _ := c.GetInt("pageSize", 20)
|
||
if pageSizeStr := c.GetString("size"); pageSizeStr != "" {
|
||
if size, err := strconv.Atoi(pageSizeStr); err == nil && size > 0 {
|
||
pageSize = size
|
||
}
|
||
}
|
||
keyword := c.GetString("keyword")
|
||
statusStr := c.GetString("status")
|
||
|
||
// 状态过滤:默认为启用状态(1),只有明确传递status参数时才按指定状态过滤
|
||
var status *int8
|
||
if statusStr != "" {
|
||
// 如果明确传递了status参数,按指定值过滤
|
||
if s, err := strconv.Atoi(statusStr); err == nil {
|
||
statusVal := int8(s)
|
||
status = &statusVal
|
||
}
|
||
} else {
|
||
// 如果没有传递status参数,默认只显示启用状态
|
||
statusVal := int8(1)
|
||
status = &statusVal
|
||
}
|
||
|
||
// 调用服务层获取分类列表
|
||
categories, total, err := services.GetArticleCategories(tenantId, page, pageSize, keyword, status)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "获取分类列表失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 格式化返回数据
|
||
categoryList := make([]map[string]interface{}, 0)
|
||
for _, category := range categories {
|
||
categoryList = append(categoryList, map[string]interface{}{
|
||
"id": category.Id,
|
||
"label": category.DictLabel,
|
||
"value": category.DictValue,
|
||
"status": category.Status,
|
||
"sort": category.Sort,
|
||
"color": category.Color,
|
||
"icon": category.Icon,
|
||
"remark": category.Remark,
|
||
"create_time": category.CreateTime,
|
||
"update_time": category.UpdateTime,
|
||
})
|
||
}
|
||
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "ok",
|
||
"data": map[string]interface{}{
|
||
"list": categoryList,
|
||
"total": total,
|
||
},
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// GetCategory 获取分类详情
|
||
func (c *ArticlesController) GetCategory() {
|
||
// 从URL获取分类ID
|
||
categoryId, err := c.GetInt(":id")
|
||
if err != nil || categoryId <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "无效的分类ID",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 调用服务层获取分类详情
|
||
category, err := services.GetArticleCategoryById(categoryId)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "获取分类详情失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
if category == nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "分类不存在",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "获取分类详情成功",
|
||
"data": map[string]interface{}{
|
||
"id": category.Id,
|
||
"label": category.DictLabel,
|
||
"value": category.DictValue,
|
||
"status": category.Status,
|
||
"sort": category.Sort,
|
||
"color": category.Color,
|
||
"icon": category.Icon,
|
||
"remark": category.Remark,
|
||
"create_time": category.CreateTime,
|
||
"update_time": category.UpdateTime,
|
||
},
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// CreateCategory 创建分类
|
||
func (c *ArticlesController) CreateCategory() {
|
||
// 定义接收分类数据的结构体
|
||
var categoryData struct {
|
||
Label string `json:"label"`
|
||
Value string `json:"value"`
|
||
Status int8 `json:"status"`
|
||
Sort int `json:"sort"`
|
||
Color string `json:"color"`
|
||
Icon string `json:"icon"`
|
||
Remark string `json:"remark"`
|
||
}
|
||
|
||
// 解析请求体JSON数据
|
||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &categoryData)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "请求参数格式错误: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 校验必要参数
|
||
if categoryData.Label == "" {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "分类名称不能为空",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
if categoryData.Value == "" {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "分类值不能为空",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 从URL参数或JWT获取租户ID,优先使用URL参数
|
||
tenantId, _ := c.GetInt("tenant_id", 0)
|
||
if tenantId == 0 {
|
||
tenantId, _ = c.GetInt("tenantId", 0)
|
||
}
|
||
|
||
// 从JWT上下文中获取用户ID
|
||
userId, _ := c.GetInt("userId", 0)
|
||
|
||
// 调用服务层创建分类
|
||
itemId, err := services.CreateArticleCategory(tenantId, userId, categoryData.Label, categoryData.Value, categoryData.Status, categoryData.Sort, categoryData.Color, categoryData.Icon, categoryData.Remark)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "创建分类失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
} else {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "创建分类成功",
|
||
"data": map[string]interface{}{
|
||
"id": itemId,
|
||
},
|
||
}
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// UpdateCategory 更新分类
|
||
func (c *ArticlesController) UpdateCategory() {
|
||
// 从URL获取分类ID
|
||
categoryId, err := c.GetInt(":id")
|
||
if err != nil || categoryId <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "无效的分类ID",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 定义接收更新数据的结构体
|
||
var categoryData struct {
|
||
Label string `json:"label"`
|
||
Value string `json:"value"`
|
||
Status int8 `json:"status"`
|
||
Sort int `json:"sort"`
|
||
Color string `json:"color"`
|
||
Icon string `json:"icon"`
|
||
Remark string `json:"remark"`
|
||
}
|
||
|
||
// 解析请求体JSON数据
|
||
err = json.Unmarshal(c.Ctx.Input.RequestBody, &categoryData)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "请求参数格式错误: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 校验必要参数
|
||
if categoryData.Label == "" {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "分类名称不能为空",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
if categoryData.Value == "" {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "分类值不能为空",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 从JWT上下文中获取用户ID
|
||
userId, _ := c.GetInt("userId", 0)
|
||
|
||
// 调用服务层更新分类
|
||
err = services.UpdateArticleCategory(categoryId, userId, categoryData.Label, categoryData.Value, categoryData.Status, categoryData.Sort, categoryData.Color, categoryData.Icon, categoryData.Remark)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "更新分类失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
} else {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "更新分类成功",
|
||
"data": nil,
|
||
}
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// DeleteCategory 删除分类
|
||
func (c *ArticlesController) DeleteCategory() {
|
||
// 从URL获取分类ID
|
||
categoryId, err := c.GetInt(":id")
|
||
if err != nil || categoryId <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "无效的分类ID",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 调用服务层删除分类
|
||
err = services.DeleteArticleCategory(categoryId)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "删除分类失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
} else {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "删除分类成功",
|
||
"data": nil,
|
||
}
|
||
}
|
||
c.ServeJSON()
|
||
}
|
||
|
||
// UpdateCategoryStatus 更新分类状态
|
||
func (c *ArticlesController) UpdateCategoryStatus() {
|
||
// 从URL获取分类ID
|
||
categoryId, err := c.GetInt(":id")
|
||
if err != nil || categoryId <= 0 {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "无效的分类ID",
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 定义接收状态数据的结构体
|
||
var statusData struct {
|
||
Status int8 `json:"status"`
|
||
}
|
||
|
||
// 解析请求体JSON数据
|
||
err = json.Unmarshal(c.Ctx.Input.RequestBody, &statusData)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "请求参数格式错误: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
c.ServeJSON()
|
||
return
|
||
}
|
||
|
||
// 调用服务层更新分类状态
|
||
err = services.UpdateArticleCategoryStatus(categoryId, statusData.Status)
|
||
if err != nil {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 1,
|
||
"message": "更新分类状态失败: " + err.Error(),
|
||
"data": nil,
|
||
}
|
||
} else {
|
||
c.Data["json"] = map[string]interface{}{
|
||
"code": 0,
|
||
"message": "更新分类状态成功",
|
||
"data": nil,
|
||
}
|
||
}
|
||
c.ServeJSON()
|
||
}
|