uploadImage.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package controllers
  2. import (
  3. "cc-officialweb/models"
  4. "cc-officialweb/service"
  5. "cc-officialweb/unity"
  6. "cc-officialweb/utils"
  7. "encoding/json"
  8. beego "github.com/beego/beego/v2/server/web"
  9. "github.com/go-playground/validator/v10"
  10. "github.com/google/uuid"
  11. "strconv"
  12. "strings"
  13. )
  14. type UploadImageController struct {
  15. beego.Controller
  16. }
  17. type JSON struct {
  18. Code int
  19. Msg any
  20. Data any
  21. }
  22. type JSONS struct {
  23. Current int
  24. Size int64
  25. Data any
  26. }
  27. // Post 上传图片
  28. func (up *UploadImageController) Post() {
  29. file, header, err := up.GetFile("file")
  30. types := up.GetString("types")
  31. if err != nil {
  32. up.Data["json"] = &JSON{Code: 101, Msg: "上传失败"}
  33. up.ServeJSON()
  34. return
  35. }
  36. defer file.Close()
  37. index := strings.Split(header.Filename, ".")[1]
  38. if index != "jpg" && index != "png" && index != "jpeg" {
  39. up.Data["json"] = &JSON{Code: 102, Msg: "文件类型错误"}
  40. up.ServeJSON()
  41. return
  42. }
  43. fileName := uuid.NewString() + "." + index
  44. if err := up.SaveToFile("file", "./static/upload/"+fileName); err != nil {
  45. up.Data["json"] = &JSON{Code: 101, Msg: "上传失败"}
  46. up.ServeJSON()
  47. return
  48. }
  49. var resourse models.Resource
  50. resourse.Name = fileName
  51. resourse.Url = "/image/" + fileName
  52. resourse.Type = types
  53. tx := utils.DB.Create(&resourse)
  54. if tx.RowsAffected > 0 {
  55. up.Data["json"] = &JSON{Code: 200, Msg: "上传成功", Data: resourse.Url}
  56. up.ServeJSON()
  57. return
  58. }
  59. }
  60. // UpdateResource 修改图片
  61. func (up *UploadImageController) UpdateResource() {
  62. getString := up.GetString("id")
  63. atoi, _ := strconv.Atoi(getString)
  64. file, header, err := up.GetFile("file")
  65. types := up.GetString("types")
  66. if err != nil {
  67. up.Data["json"] = &JSON{Code: 101, Msg: "上传失败"}
  68. up.ServeJSON()
  69. return
  70. }
  71. defer file.Close()
  72. index := strings.Split(header.Filename, ".")[1]
  73. if index != "jpg" && index != "png" && index != "jpeg" {
  74. up.Data["json"] = &JSON{Code: 102, Msg: "文件类型错误"}
  75. up.ServeJSON()
  76. return
  77. }
  78. fileName := uuid.NewString() + "." + index
  79. if err := up.SaveToFile("file", "./static/upload/"+fileName); err != nil {
  80. up.Data["json"] = &JSON{Code: 101, Msg: "上传失败"}
  81. up.ServeJSON()
  82. return
  83. }
  84. var resource models.Resource
  85. resource.ID = uint(atoi)
  86. resource.Name = fileName
  87. resource.Url = "/image/" + fileName
  88. resource.Type = types
  89. resources := service.UpdateResource(resource)
  90. if len(resources.Url) == 0 {
  91. up.Data["json"] = &JSON{Code: 101, Msg: "修改失败"}
  92. up.ServeJSON()
  93. return
  94. } else {
  95. up.Data["json"] = &JSON{Code: 200, Msg: "修改成功", Data: resource.Url}
  96. up.ServeJSON()
  97. }
  98. }
  99. // DeleteResourceById 删除图片
  100. func (up *UploadImageController) DeleteResourceById() {
  101. getString := up.GetString("id")
  102. atoi, _ := strconv.Atoi(getString)
  103. validate := validator.New()
  104. err2 := validate.Var(atoi, "required")
  105. if err2 != nil {
  106. up.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  107. up.ServeJSON()
  108. return
  109. }
  110. id, err := unity.DeleteById(atoi, models.Resource{})
  111. if err != nil {
  112. up.Data["json"] = &JSON{Code: 101, Msg: "删除失败"}
  113. up.ServeJSON()
  114. return
  115. } else {
  116. up.Data["json"] = &JSON{Code: 200, Msg: "删除成功", Data: id}
  117. up.ServeJSON()
  118. }
  119. }
  120. // GetResource 获取所有图片
  121. func (up *UploadImageController) GetResource() {
  122. var page unity.PageParams
  123. err := json.Unmarshal(up.Ctx.Input.RequestBody, &page)
  124. if err != nil {
  125. up.Data["json"] = &JSON{Code: 101, Msg: "参数错误"}
  126. up.ServeJSON()
  127. return
  128. }
  129. result, total, current, err := unity.Paginate(page, models.Resource{})
  130. if err != nil {
  131. up.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
  132. up.ServeJSON()
  133. return
  134. } else {
  135. up.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{
  136. Current: current,
  137. Size: total,
  138. Data: result,
  139. }}
  140. up.ServeJSON()
  141. return
  142. }
  143. }
  144. func (up *UploadImageController) GetResourceById() {
  145. getString := up.GetString("id")
  146. id, _ := strconv.Atoi(getString)
  147. validate := validator.New()
  148. err2 := validate.Var(id, "required")
  149. if err2 != nil {
  150. up.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  151. up.ServeJSON()
  152. return
  153. }
  154. result, err := unity.GetById(id, &models.Resource{})
  155. if err != nil {
  156. up.Data["json"] = &JSON{Code: 101, Msg: "获取失败"}
  157. up.ServeJSON()
  158. return
  159. } else {
  160. up.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: result}
  161. up.ServeJSON()
  162. return
  163. }
  164. }