432 lines
9.2 KiB
Go
432 lines
9.2 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
"server/models"
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
// FileController 处理文件相关请求
|
|
type FileController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
// GetAllFiles 获取所有文件信息
|
|
func (c *FileController) GetAllFiles() {
|
|
files, err := models.GetAllFiles()
|
|
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "获取文件列表失败",
|
|
"error": err.Error(),
|
|
}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": true,
|
|
"message": "获取文件列表成功",
|
|
"data": files,
|
|
}
|
|
}
|
|
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// GetFileById 根据ID获取文件信息
|
|
func (c *FileController) GetFileById() {
|
|
idStr := c.Ctx.Input.Param(":id")
|
|
id, err := strconv.ParseInt(idStr, 10, 64)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "参数错误",
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
file, err := models.GetFileById(id)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "文件不存在",
|
|
}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": true,
|
|
"message": "获取文件成功",
|
|
"data": file,
|
|
}
|
|
}
|
|
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// GetFilesByTenant 根据租户ID获取文件信息
|
|
func (c *FileController) GetFilesByTenant() {
|
|
tenantID := c.GetString("tenant_id")
|
|
if tenantID == "" {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "租户ID不能为空",
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
files, err := models.GetFilesByTenant(tenantID)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "获取文件列表失败",
|
|
"error": err.Error(),
|
|
}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": true,
|
|
"message": "获取文件列表成功",
|
|
"data": files,
|
|
}
|
|
}
|
|
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// GetFilesByCategory 根据分类获取文件信息
|
|
func (c *FileController) GetFilesByCategory() {
|
|
category := c.GetString("category")
|
|
if category == "" {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "分类不能为空",
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
files, err := models.GetFilesByCategory(category)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "获取文件列表失败",
|
|
"error": err.Error(),
|
|
}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": true,
|
|
"message": "获取文件列表成功",
|
|
"data": files,
|
|
}
|
|
}
|
|
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// GetFilesByStatus 根据状态获取文件信息
|
|
func (c *FileController) GetFilesByStatus() {
|
|
statusStr := c.GetString("status")
|
|
status, err := strconv.Atoi(statusStr)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "状态参数错误",
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
files, err := models.GetFilesByStatus(int8(status))
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "获取文件列表失败",
|
|
"error": err.Error(),
|
|
}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": true,
|
|
"message": "获取文件列表成功",
|
|
"data": files,
|
|
}
|
|
}
|
|
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// CreateFile 创建新文件信息
|
|
func (c *FileController) CreateFile() {
|
|
var file models.FileInfo
|
|
|
|
// 解析请求体
|
|
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &file); err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "请求参数错误",
|
|
"error": err.Error(),
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
// 从JWT中间件获取用户信息
|
|
if userID, ok := c.Ctx.Input.GetData("userId").(int); ok && userID > 0 {
|
|
file.UserID = userID
|
|
}
|
|
if username, ok := c.Ctx.Input.GetData("username").(string); ok && username != "" {
|
|
file.UploadBy = username
|
|
}
|
|
|
|
// 验证必填字段
|
|
if file.TenantID == "" || file.FileName == "" || file.OriginalName == "" ||
|
|
file.FilePath == "" || file.FileType == "" || file.FileExt == "" ||
|
|
file.Category == "" || file.UploadBy == "" {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "必填字段不能为空",
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
// 添加文件信息
|
|
if _, err := models.AddFile(&file); err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "创建文件失败",
|
|
"error": err.Error(),
|
|
}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": true,
|
|
"message": "创建文件成功",
|
|
"data": file,
|
|
}
|
|
}
|
|
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// UpdateFile 更新文件信息
|
|
func (c *FileController) UpdateFile() {
|
|
idStr := c.Ctx.Input.Param(":id")
|
|
id, err := strconv.ParseInt(idStr, 10, 64)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "参数错误",
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
var file models.FileInfo
|
|
file.ID = id
|
|
|
|
// 解析请求体
|
|
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &file); err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "请求参数错误",
|
|
"error": err.Error(),
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
// 更新文件信息
|
|
if err := models.UpdateFile(&file); err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "更新文件失败",
|
|
"error": err.Error(),
|
|
}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": true,
|
|
"message": "更新文件成功",
|
|
"data": file,
|
|
}
|
|
}
|
|
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// DeleteFile 删除文件信息(软删除)
|
|
func (c *FileController) DeleteFile() {
|
|
idStr := c.Ctx.Input.Param(":id")
|
|
id, err := strconv.ParseInt(idStr, 10, 64)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "参数错误",
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
if err := models.DeleteFile(id); err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "删除文件失败",
|
|
"error": err.Error(),
|
|
}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": true,
|
|
"message": "删除文件成功",
|
|
}
|
|
}
|
|
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// HardDeleteFile 硬删除文件信息
|
|
func (c *FileController) HardDeleteFile() {
|
|
idStr := c.Ctx.Input.Param(":id")
|
|
id, err := strconv.ParseInt(idStr, 10, 64)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "参数错误",
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
if err := models.HardDeleteFile(id); err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "删除文件失败",
|
|
"error": err.Error(),
|
|
}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": true,
|
|
"message": "删除文件成功",
|
|
}
|
|
}
|
|
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// GetFileStatistics 获取文件统计信息
|
|
func (c *FileController) GetFileStatistics() {
|
|
tenantID := c.GetString("tenant_id")
|
|
if tenantID == "" {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "租户ID不能为空",
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
stats, err := models.GetFileStatistics(tenantID)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "获取统计信息失败",
|
|
"error": err.Error(),
|
|
}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": true,
|
|
"message": "获取统计信息成功",
|
|
"data": stats,
|
|
}
|
|
}
|
|
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// SearchFiles 搜索文件
|
|
func (c *FileController) SearchFiles() {
|
|
keyword := c.GetString("keyword")
|
|
tenantID := c.GetString("tenant_id")
|
|
|
|
if keyword == "" {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "搜索关键词不能为空",
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
if tenantID == "" {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "租户ID不能为空",
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
files, err := models.SearchFiles(keyword, tenantID)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "搜索文件失败",
|
|
"error": err.Error(),
|
|
}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": true,
|
|
"message": "搜索文件成功",
|
|
"data": files,
|
|
}
|
|
}
|
|
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// GetMyFiles 获取当前用户的文件列表
|
|
func (c *FileController) GetMyFiles() {
|
|
// 从JWT中间件获取用户信息
|
|
userID, ok := c.Ctx.Input.GetData("userId").(int)
|
|
if !ok || userID <= 0 {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "用户未登录或登录已过期",
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
files, err := models.GetFilesByUserID(userID)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "获取文件列表失败: " + err.Error(),
|
|
}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": true,
|
|
"message": "获取成功",
|
|
"data": files,
|
|
}
|
|
}
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// GetFilesPublic 不需要认证的获取文件信息接口
|
|
func (c *FileController) GetFilesPublic() {
|
|
files, err := models.GetAllFiles()
|
|
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": false,
|
|
"message": "获取文件列表失败",
|
|
"error": err.Error(),
|
|
}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"success": true,
|
|
"message": "获取文件列表成功",
|
|
"data": files,
|
|
}
|
|
}
|
|
|
|
c.ServeJSON()
|
|
} |