97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
package controller
|
|
|
|
import (
|
|
"cms-api/internal/model"
|
|
"cms-api/internal/service"
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
// 附件控制器
|
|
type cAttachment struct{}
|
|
|
|
var Attachment = &cAttachment{}
|
|
|
|
// List 获取附件列表
|
|
func (c *cAttachment) List(r *ghttp.Request) {
|
|
var req *model.AttachmentListRequest
|
|
if err := r.Parse(&req); err != nil {
|
|
r.Response.WriteJson(&model.Response{Code: 400, Message: "参数解析失败", Data: err.Error()})
|
|
return
|
|
}
|
|
|
|
// 调用附件服务获取列表
|
|
attachmentService := service.Attachment()
|
|
result, err := attachmentService.List(r.Context(), req)
|
|
if err != nil {
|
|
r.Response.WriteJson(&model.Response{Code: 500, Message: "获取附件列表失败", Data: err.Error()})
|
|
return
|
|
}
|
|
|
|
r.Response.WriteJson(&model.Response{Code: 200, Message: "获取成功", Data: result})
|
|
}
|
|
|
|
// Upload 上传附件
|
|
func (c *cAttachment) Upload(r *ghttp.Request) {
|
|
// 获取上传的文件
|
|
file := r.GetUploadFile("file")
|
|
if file == nil {
|
|
r.Response.WriteJson(&model.Response{Code: 400, Message: "请选择要上传的文件"})
|
|
return
|
|
}
|
|
|
|
// 调用附件服务处理上传
|
|
attachmentService := service.Attachment()
|
|
result, err := attachmentService.Upload(r.Context(), file, "uploads")
|
|
if err != nil {
|
|
r.Response.WriteJson(&model.Response{Code: 500, Message: "文件上传失败", Data: err.Error()})
|
|
return
|
|
}
|
|
|
|
r.Response.WriteJson(&model.Response{Code: 200, Message: "上传成功", Data: result})
|
|
}
|
|
|
|
// Delete 删除附件
|
|
func (c *cAttachment) Delete(r *ghttp.Request) {
|
|
id := gconv.Int(r.Get("id"))
|
|
if id <= 0 {
|
|
r.Response.WriteJson(&model.Response{Code: 400, Message: "附件ID无效"})
|
|
return
|
|
}
|
|
|
|
// 调用附件服务删除附件
|
|
attachmentService := service.Attachment()
|
|
err := attachmentService.Delete(r.Context(), id)
|
|
if err != nil {
|
|
r.Response.WriteJson(&model.Response{Code: 500, Message: "删除附件失败", Data: err.Error()})
|
|
return
|
|
}
|
|
|
|
r.Response.WriteJson(&model.Response{Code: 200, Message: "删除附件成功"})
|
|
}
|
|
|
|
// Detail 获取附件详情
|
|
func (c *cAttachment) Detail(r *ghttp.Request) {
|
|
id := gconv.Int(r.Get("id"))
|
|
if id <= 0 {
|
|
r.Response.WriteJson(&model.Response{Code: 400, Message: "附件ID无效"})
|
|
return
|
|
}
|
|
|
|
// 调用附件服务获取详情
|
|
attachmentService := service.Attachment()
|
|
result, err := attachmentService.GetById(r.Context(), id)
|
|
if err != nil {
|
|
r.Response.WriteJson(&model.Response{Code: 500, Message: "获取附件详情失败", Data: err.Error()})
|
|
return
|
|
}
|
|
|
|
if result == nil {
|
|
r.Response.WriteJson(&model.Response{Code: 404, Message: "附件不存在"})
|
|
return
|
|
}
|
|
|
|
r.Response.WriteJson(&model.Response{Code: 200, Message: "获取成功", Data: result})
|
|
}
|