FileManagement.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package controllers
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/models/Account"
  6. FileManagementModel "ColdVerify_server/models/FileManagement"
  7. "ColdVerify_server/models/System"
  8. "math"
  9. "strings"
  10. beego "github.com/beego/beego/v2/server/web"
  11. )
  12. type FileManagementController struct {
  13. beego.Controller
  14. }
  15. func (c *FileManagementController) List() {
  16. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  17. if !User_is {
  18. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  19. c.ServeJSON()
  20. return
  21. }
  22. page, _ := c.GetInt("page")
  23. if page < 1 {
  24. page = 1
  25. }
  26. pageSize, _ := c.GetInt("page_z")
  27. if pageSize < 1 {
  28. pageSize = conf.Page_size
  29. }
  30. fileName := strings.TrimSpace(c.GetString("file_name"))
  31. list, cnt := FileManagementModel.ReadFileManagementList(fileName, page, pageSize)
  32. pageCount := math.Ceil(float64(cnt) / float64(pageSize))
  33. var response lib.R_JSONS
  34. response.List = list
  35. response.Page = page
  36. response.Page_size = int(pageCount)
  37. response.Pages = lib.Func_page(int64(page), int64(pageCount))
  38. response.Num = int(cnt)
  39. System.Add_UserLogs_T(User_r.T_uuid, "文件管理", "查询", map[string]interface{}{
  40. "file_name": fileName,
  41. "page": page,
  42. "page_z": pageSize,
  43. })
  44. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: response}
  45. c.ServeJSON()
  46. }
  47. func (c *FileManagementController) Get() {
  48. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  49. if !User_is {
  50. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  51. c.ServeJSON()
  52. return
  53. }
  54. id, err := c.GetInt("Id")
  55. if err != nil || id <= 0 {
  56. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  57. c.ServeJSON()
  58. return
  59. }
  60. file, ok := FileManagementModel.ReadFileManagementById(id)
  61. if !ok {
  62. c.Data["json"] = lib.JSONS{Code: 202, Msg: "未找到文件信息!"}
  63. c.ServeJSON()
  64. return
  65. }
  66. System.Add_UserLogs_T(User_r.T_uuid, "文件管理", "详情", map[string]interface{}{
  67. "id": id,
  68. })
  69. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: FileManagementModel.ToFileManagementR(file)}
  70. c.ServeJSON()
  71. }
  72. func (c *FileManagementController) Add() {
  73. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  74. if !User_is {
  75. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  76. c.ServeJSON()
  77. return
  78. }
  79. fileName := strings.TrimSpace(c.GetString("file_name"))
  80. filePath := strings.TrimSpace(c.GetString("file_path"))
  81. if len(fileName) == 0 || len(filePath) == 0 {
  82. c.Data["json"] = lib.JSONS{Code: 202, Msg: "文件名称和文件路径不能为空!"}
  83. c.ServeJSON()
  84. return
  85. }
  86. file := FileManagementModel.FileManagement{
  87. FileName: fileName,
  88. FilePath: filePath,
  89. }
  90. id, ok := FileManagementModel.AddFileManagement(file)
  91. if !ok {
  92. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  93. c.ServeJSON()
  94. return
  95. }
  96. System.Add_UserLogs_T(User_r.T_uuid, "文件管理", "添加", file)
  97. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  98. c.ServeJSON()
  99. }
  100. func (c *FileManagementController) Up() {
  101. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  102. if !User_is {
  103. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  104. c.ServeJSON()
  105. return
  106. }
  107. id, err := c.GetInt("Id")
  108. if err != nil || id <= 0 {
  109. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  110. c.ServeJSON()
  111. return
  112. }
  113. file, ok := FileManagementModel.ReadFileManagementById(id)
  114. if !ok {
  115. c.Data["json"] = lib.JSONS{Code: 202, Msg: "未找到文件信息!"}
  116. c.ServeJSON()
  117. return
  118. }
  119. fileName := strings.TrimSpace(c.GetString("file_name"))
  120. filePath := strings.TrimSpace(c.GetString("file_path"))
  121. updateCols := make([]string, 0, 2)
  122. if len(fileName) > 0 {
  123. file.FileName = fileName
  124. updateCols = append(updateCols, "FileName")
  125. }
  126. if len(filePath) > 0 {
  127. file.FilePath = filePath
  128. updateCols = append(updateCols, "FilePath")
  129. }
  130. if len(updateCols) == 0 {
  131. c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有可更新的内容!"}
  132. c.ServeJSON()
  133. return
  134. }
  135. if !FileManagementModel.UpdateFileManagement(file, updateCols...) {
  136. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  137. c.ServeJSON()
  138. return
  139. }
  140. System.Add_UserLogs_T(User_r.T_uuid, "文件管理", "修改", file)
  141. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  142. c.ServeJSON()
  143. }
  144. func (c *FileManagementController) Del() {
  145. User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  146. if !User_is {
  147. c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
  148. c.ServeJSON()
  149. return
  150. }
  151. id, err := c.GetInt("Id")
  152. if err != nil || id <= 0 {
  153. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
  154. c.ServeJSON()
  155. return
  156. }
  157. file, ok := FileManagementModel.ReadFileManagementById(id)
  158. if !ok {
  159. c.Data["json"] = lib.JSONS{Code: 202, Msg: "未找到文件信息!"}
  160. c.ServeJSON()
  161. return
  162. }
  163. if !FileManagementModel.DeleteFileManagement(file) {
  164. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  165. c.ServeJSON()
  166. return
  167. }
  168. System.Add_UserLogs_T(User_r.T_uuid, "文件管理", "删除", file)
  169. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  170. c.ServeJSON()
  171. }