tree_api.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * @description:文件夹相关-管理员可访问
  3. * @Author: CP
  4. * @Date: 2020-09-17 17:50:50
  5. * @FilePath: \construction_management\web\api\tree_api.go
  6. */
  7. package api
  8. import (
  9. "fmt"
  10. "strconv"
  11. "github.com/kataras/iris/v12"
  12. "go.mod/services"
  13. "go.mod/web/utils"
  14. "go.mod/web/viewmodels"
  15. )
  16. type TreeApi struct {
  17. //框架-web应用上下文环境
  18. Ctx iris.Context
  19. // 需要用的service
  20. ServiceTree services.TreeService
  21. }
  22. // @Summary 获得目录和数据
  23. // @Tags 目录相关-管理员
  24. // @Description 获得目录和数据
  25. // @Accept json
  26. // @Produce json
  27. // @Security ApiKeyAuth
  28. // @Success 200 {object} viewmodels.Tree "{code:0成功,data:viewmodels.Tree,msg:}"
  29. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  30. // @Router /api/tree [get]
  31. func (c *TreeApi) Get() {
  32. // 获得项目ID
  33. projectIdInt, err := utils.GetProjectId(c.Ctx)
  34. if err != nil {
  35. c.Ctx.JSON(iris.Map{
  36. "code": -1,
  37. "msg": err,
  38. })
  39. return
  40. }
  41. // 获得层级文件夹
  42. FolderData := c.ServiceTree.GetAllProject(projectIdInt)
  43. c.Ctx.JSON(iris.Map{
  44. "code": 0,
  45. "msg": "",
  46. "data": FolderData,
  47. })
  48. }
  49. // @Summary 新增目录
  50. // @Tags 目录相关-管理员
  51. // @Description 新增目录
  52. // @Accept json
  53. // @Produce json
  54. // @Security ApiKeyAuth
  55. // @Param id body string true "目录ID"
  56. // @Param depth body int true "目录深度 顶级目录(-1)其他级目录(0)"
  57. // @Param name body string true "目录名称"
  58. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  59. // @Router /api/tree/create [post]
  60. func (c *TreeApi) PostCreate() {
  61. ErrMsg := ""
  62. // 验证内容
  63. FolderData, err := c.ServiceTree.ValidRule(c.Ctx)
  64. if err != nil {
  65. ErrMsg = utils.FormValidError(err)
  66. c.Ctx.JSON(iris.Map{
  67. "code": -1,
  68. "msg": ErrMsg,
  69. })
  70. return
  71. } else {
  72. // 获得项目ID
  73. projectIdInt, err := utils.GetProjectId(c.Ctx)
  74. if err != nil {
  75. c.Ctx.JSON(iris.Map{
  76. "code": -1,
  77. "msg": fmt.Sprintf("%s", err),
  78. })
  79. return
  80. }
  81. FolderData.ProjectId = strconv.Itoa(projectIdInt)
  82. // 新增文件夹
  83. err = c.ServiceTree.Create(FolderData)
  84. if err != nil {
  85. c.Ctx.JSON(iris.Map{
  86. "code": 3,
  87. "msg": fmt.Sprintf("%s", err),
  88. })
  89. return
  90. }
  91. c.Ctx.JSON(iris.Map{
  92. "code": 0,
  93. "msg": "新增成功",
  94. })
  95. }
  96. }
  97. // @Summary 重命名
  98. // @Tags 目录相关-管理员
  99. // @Description 重命名-文件夹或者标段
  100. // @Accept json
  101. // @Produce json
  102. // @Security ApiKeyAuth
  103. // @Param id body string true "treeId"
  104. // @Param name body string true "重命名的名称"
  105. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  106. // @Router /api/tree/rename [post]
  107. func (c *TreeApi) PostRename() {
  108. ErrMsg := ""
  109. // 验证内容
  110. FolderData, err := c.ServiceTree.ValidRule(c.Ctx)
  111. if err != nil {
  112. ErrMsg = utils.FormValidError(err)
  113. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  114. return
  115. } else {
  116. // 重命名
  117. projectId, err := utils.GetProjectId(c.Ctx)
  118. if err != nil {
  119. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  120. return
  121. }
  122. err = c.ServiceTree.Rename(FolderData, projectId)
  123. if err != nil {
  124. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  125. return
  126. }
  127. c.Ctx.JSON(iris.Map{
  128. "code": 0,
  129. "msg": "重命名成功",
  130. })
  131. }
  132. }
  133. // @Summary 删除目录
  134. // @Tags 目录相关-管理员
  135. // @Description 删除目录下的目录以及其他内容
  136. // @Accept json
  137. // @Produce json
  138. // @Security ApiKeyAuth
  139. // @Param id body string true "目录ID"
  140. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  141. // @Router /api/tree [delete]
  142. func (c *TreeApi) Delete() {
  143. // 获得需要删除数据
  144. // api/tree/{id:int min(1)}
  145. //id := c.Ctx.Params().Get("id")
  146. //id := c.Ctx.URLParam("id")
  147. // 获得该目录下所有的目录和标段
  148. // treeData, err := c.ServiceTree.GetFolderAndBid(id)
  149. // if err != nil {
  150. // c.Ctx.JSON(iris.Map{
  151. // "code": -1,
  152. // "msg": fmt.Sprintf("%s", err),
  153. // })
  154. // return
  155. // }
  156. // fmt.Println(treeData)
  157. // 添加锁
  158. idString := c.Ctx.URLParam("id")
  159. if idString == "" {
  160. c.Ctx.JSON(iris.Map{"code": -1, "msg": "目录不存在"})
  161. return
  162. }
  163. id, err := utils.GetDecryptId(idString)
  164. if err != nil {
  165. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  166. return
  167. }
  168. projectId, err := utils.GetProjectId(c.Ctx)
  169. if err != nil {
  170. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  171. return
  172. }
  173. // 删除目录以及标段
  174. err = c.ServiceTree.DeleteFolderAndBid(id, projectId)
  175. if err != nil {
  176. c.Ctx.JSON(iris.Map{
  177. "code": -1,
  178. "msg": fmt.Sprintf("%s", err),
  179. })
  180. return
  181. }
  182. c.Ctx.JSON(iris.Map{
  183. "code": 0,
  184. "msg": "删除成功",
  185. })
  186. }
  187. // @Summary 移动文件夹
  188. // @Tags 目录相关-管理员
  189. // @Description 移动文件夹
  190. // @Accept json
  191. // @Produce json
  192. // @Security ApiKeyAuth
  193. // @Param id body string true "目录ID"
  194. // @Param moveId body string true "被放置的目录ID"
  195. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  196. // @Router /api/tree/move [post]
  197. func (c *TreeApi) PostMove() {
  198. // 获得目录ID和移动目录ID--TODO 判断
  199. Tree := viewmodels.Tree{}
  200. err := c.Ctx.ReadJSON(&Tree)
  201. if err != nil {
  202. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  203. return
  204. }
  205. id, err := utils.GetDecryptId(Tree.Id)
  206. if err != nil {
  207. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  208. return
  209. }
  210. moveId, err := utils.GetDecryptId(Tree.TargetFolderId)
  211. if err != nil {
  212. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  213. return
  214. }
  215. projectId, err := utils.GetProjectId(c.Ctx)
  216. if err != nil {
  217. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  218. return
  219. }
  220. // 移动目录
  221. err = c.ServiceTree.Move(id, moveId, projectId)
  222. if err != nil {
  223. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  224. return
  225. }
  226. c.Ctx.JSON(iris.Map{
  227. "code": 0,
  228. "msg": "移动成功",
  229. })
  230. }