You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.2 KiB

  1. package api
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "main/controller/servers"
  6. "main/model/dbModel"
  7. "main/model/modelInterface"
  8. "strings"
  9. )
  10. // @Tags FileUploadAndDownload
  11. // @Summary 上传文件示例
  12. // @Security ApiKeyAuth
  13. // @accept multipart/form-data
  14. // @Produce application/json
  15. // @Param file formData file true "上传文件示例"
  16. // @Success 200 {string} json "{"success":true,"data":{},"msg":"上传成功"}"
  17. // @Router /fileUploadAndDownload/upload [post]
  18. func UploadFile(c *gin.Context) {
  19. _, header, err := c.Request.FormFile("file")
  20. if err != nil {
  21. servers.ReportFormat(c, false, fmt.Sprintf("上传文件失败,%v", err), gin.H{})
  22. } else {
  23. //文件上传后拿到文件路径
  24. err, filePath := servers.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH)
  25. if err != nil {
  26. servers.ReportFormat(c, false, fmt.Sprintf("接收返回值失败,%v", err), gin.H{})
  27. } else {
  28. //修改数据库后得到修改后的user并且返回供前端使用
  29. var file dbModel.FileUploadAndDownload
  30. file.Url = filePath
  31. file.Name = header.Filename
  32. s := strings.Split(file.Name, ".")
  33. file.Tag = s[len(s)-1]
  34. err := file.Upload()
  35. if err != nil {
  36. servers.ReportFormat(c, false, fmt.Sprintf("修改数据库链接失败,%v", err), gin.H{})
  37. } else {
  38. servers.ReportFormat(c, true, "上传成功", gin.H{"file": file})
  39. }
  40. }
  41. }
  42. }
  43. // @Tags FileUploadAndDownload
  44. // @Summary 分页文件列表
  45. // @Security ApiKeyAuth
  46. // @accept application/json
  47. // @Produce application/json
  48. // @Param data body modelInterface.PageInfo true "分页获取文件户列表"
  49. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  50. // @Router /fileUploadAndDownload/getFileList [post]
  51. func GetFileList(c *gin.Context) {
  52. var pageInfo modelInterface.PageInfo
  53. _ = c.BindJSON(&pageInfo)
  54. err, list, total := new(dbModel.FileUploadAndDownload).GetInfoList(pageInfo)
  55. if err != nil {
  56. servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
  57. } else {
  58. servers.ReportFormat(c, true, "获取数据成功", gin.H{
  59. "list": list,
  60. "total": total,
  61. "page": pageInfo.Page,
  62. "pageSize": pageInfo.PageSize,
  63. })
  64. }
  65. }