增加cms模块

This commit is contained in:
2026-01-05 17:38:34 +08:00
parent 3c0f01eb36
commit 09cbd721a0
24 changed files with 3158 additions and 955 deletions
+451
View File
@@ -0,0 +1,451 @@
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()
}
+1
View File
@@ -93,6 +93,7 @@ func (c *AuthController) Login() {
"avatar": user.Avatar,
"nickname": user.Nickname,
"tenant_id": user.TenantId,
"uid": user.Uid,
"role": user.Role, // 角色ID
"type": "user", // 标识是用户登录
}
+4
View File
@@ -29,6 +29,7 @@ func (c *UserController) GetAllUsers() {
for _, user := range users {
userList = append(userList, map[string]interface{}{
"id": user.Id,
"uid": user.Uid,
"username": user.Username,
"email": user.Email,
"avatar": user.Avatar,
@@ -79,6 +80,7 @@ func (c *UserController) GetTenantUsers() {
for _, user := range users {
userList = append(userList, map[string]interface{}{
"id": user.Id,
"uid": user.Uid,
"username": user.Username,
"email": user.Email,
"avatar": user.Avatar,
@@ -203,6 +205,7 @@ func (c *UserController) GetUserInfo() {
"message": "查询成功",
"data": map[string]interface{}{
"id": user.Id,
"uid": user.Uid,
"username": user.Username,
"email": user.Email,
"avatar": user.Avatar,
@@ -297,6 +300,7 @@ func (c *UserController) AddUser() {
"message": "用户添加成功",
"data": map[string]interface{}{
"id": newUser.Id,
"uid": newUser.Uid,
"username": newUser.Username,
"email": newUser.Email,
"nickname": newUser.Nickname,