public.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package controller
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "hongze/hongze_yb/controller/response"
  6. "hongze/hongze_yb/global"
  7. "hongze/hongze_yb/logic"
  8. "hongze/hongze_yb/services"
  9. "hongze/hongze_yb/utils"
  10. "os"
  11. "path"
  12. "time"
  13. )
  14. // GetApplyVarietyList
  15. // @Tags 公共模块
  16. // @Summary 获取所有可以申请的品种权限列表
  17. // @Description 获取所有可以申请的品种权限列表
  18. // @Security ApiKeyAuth
  19. // @securityDefinitions.basic BasicAuth
  20. // @Param Authorization header string true "微信登录后获取到的token"
  21. // @Accept json
  22. // @Product json
  23. // @Success 200 {object} []logic.ApplyVariety "获取成功"
  24. // @failure 400 {string} string "获取失败"
  25. // @Router /public/get_apply_variety_list [get]
  26. func GetApplyVarietyList(c *gin.Context) {
  27. list := logic.GetApplyVarietyList()
  28. response.OkData("获取成功", list, c)
  29. }
  30. // Upload
  31. // @Tags 公共模块
  32. // @Summary 文件上传
  33. // @Description 文件上传
  34. // @Security ApiKeyAuth
  35. // @securityDefinitions.basic BasicAuth
  36. // @Param Authorization header string true "微信登录后获取到的token"
  37. // @Param file formData file false "操作描述"
  38. // @Accept multipart/form-data
  39. // @Product json
  40. // @Success 200 {object} string "上传成功"
  41. // @failure 400 {string} string "上传失败,存储目录创建失败"
  42. // @Router /public/upload [post]
  43. func Upload(c *gin.Context) {
  44. // 单文件
  45. file, err := c.FormFile("file")
  46. fmt.Println("file", file)
  47. if err != nil {
  48. response.FailData("获取资源失败", "获取资源失败,Err:"+err.Error(), c)
  49. return
  50. }
  51. ext := path.Ext(file.Filename)
  52. dateDir := time.Now().Format("20060102")
  53. uploadDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  54. err = os.MkdirAll(uploadDir, 0766)
  55. if err != nil {
  56. response.FailData("存储目录创建失败", "存储目录创建失败,Err:"+err.Error(), c)
  57. return
  58. }
  59. randStr := utils.GetRandStringNoSpecialChar(28)
  60. fileName := randStr + ext
  61. fpath := uploadDir + "/" + fileName
  62. // 上传文件至指定目录
  63. err = c.SaveUploadedFile(file, fpath)
  64. if err != nil {
  65. response.FailData("文件上传失败", "文件上传失败,Err:"+err.Error(), c)
  66. return
  67. }
  68. defer func() {
  69. os.Remove(fpath)
  70. }()
  71. //上传到阿里云
  72. resourceUrl, err := services.UploadAliyun(fileName, fpath)
  73. if err != nil {
  74. response.FailData("文件上传失败", "文件上传失败,Err:"+err.Error(), c)
  75. return
  76. }
  77. response.OkData("文件上传成功", resourceUrl, c)
  78. }