123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- /*
- * @description:文件夹相关-管理员可访问
- * @Author: CP
- * @Date: 2020-09-17 17:50:50
- * @FilePath: \construction_management\web\api\tree_api.go
- */
- package api
- import (
- "fmt"
- "strconv"
- "github.com/kataras/iris/v12"
- "go.mod/services"
- "go.mod/web/utils"
- "go.mod/web/viewmodels"
- )
- type TreeApi struct {
- //框架-web应用上下文环境
- Ctx iris.Context
- // 需要用的service
- ServiceTree services.TreeService
- }
- // @Summary 获得目录和数据
- // @Tags 目录相关-管理员
- // @Description 获得目录和数据
- // @Accept json
- // @Produce json
- // @Security ApiKeyAuth
- // @Success 200 {object} viewmodels.Tree "{code:0成功,data:viewmodels.Tree,msg:}"
- // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
- // @Router /api/tree [get]
- func (c *TreeApi) Get() {
- // 获得项目ID
- projectIdInt, err := utils.GetProjectId(c.Ctx)
- if err != nil {
- c.Ctx.JSON(iris.Map{
- "code": -1,
- "msg": err,
- })
- return
- }
- // 获得层级文件夹
- FolderData := c.ServiceTree.GetAllProject(projectIdInt)
- c.Ctx.JSON(iris.Map{
- "code": 0,
- "msg": "",
- "data": FolderData,
- })
- }
- // @Summary 新增目录
- // @Tags 目录相关-管理员
- // @Description 新增目录
- // @Accept json
- // @Produce json
- // @Security ApiKeyAuth
- // @Param id body string true "目录ID"
- // @Param depth body int true "目录深度 顶级目录(-1)其他级目录(0)"
- // @Param name body string true "目录名称"
- // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
- // @Router /api/tree/create [post]
- func (c *TreeApi) PostCreate() {
- ErrMsg := ""
- // 验证内容
- FolderData, err := c.ServiceTree.ValidRule(c.Ctx)
- if err != nil {
- ErrMsg = utils.FormValidError(err)
- c.Ctx.JSON(iris.Map{
- "code": -1,
- "msg": ErrMsg,
- })
- return
- } else {
- // 获得项目ID
- projectIdInt, err := utils.GetProjectId(c.Ctx)
- if err != nil {
- c.Ctx.JSON(iris.Map{
- "code": -1,
- "msg": fmt.Sprintf("%s", err),
- })
- return
- }
- FolderData.ProjectId = strconv.Itoa(projectIdInt)
- // 新增文件夹
- err = c.ServiceTree.Create(FolderData)
- if err != nil {
- c.Ctx.JSON(iris.Map{
- "code": 3,
- "msg": fmt.Sprintf("%s", err),
- })
- return
- }
- c.Ctx.JSON(iris.Map{
- "code": 0,
- "msg": "新增成功",
- })
- }
- }
- // @Summary 重命名
- // @Tags 目录相关-管理员
- // @Description 重命名-文件夹或者标段
- // @Accept json
- // @Produce json
- // @Security ApiKeyAuth
- // @Param id body string true "treeId"
- // @Param name body string true "重命名的名称"
- // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
- // @Router /api/tree/rename [post]
- func (c *TreeApi) PostRename() {
- ErrMsg := ""
- // 验证内容
- FolderData, err := c.ServiceTree.ValidRule(c.Ctx)
- if err != nil {
- ErrMsg = utils.FormValidError(err)
- c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
- return
- } else {
- // 重命名
- projectId, err := utils.GetProjectId(c.Ctx)
- if err != nil {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
- return
- }
- err = c.ServiceTree.Rename(FolderData, projectId)
- if err != nil {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
- return
- }
- c.Ctx.JSON(iris.Map{
- "code": 0,
- "msg": "重命名成功",
- })
- }
- }
- // @Summary 删除目录
- // @Tags 目录相关-管理员
- // @Description 删除目录下的目录以及其他内容
- // @Accept json
- // @Produce json
- // @Security ApiKeyAuth
- // @Param id body string true "目录ID"
- // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
- // @Router /api/tree [delete]
- func (c *TreeApi) Delete() {
- // 获得需要删除数据
- // api/tree/{id:int min(1)}
- //id := c.Ctx.Params().Get("id")
- //id := c.Ctx.URLParam("id")
- // 获得该目录下所有的目录和标段
- // treeData, err := c.ServiceTree.GetFolderAndBid(id)
- // if err != nil {
- // c.Ctx.JSON(iris.Map{
- // "code": -1,
- // "msg": fmt.Sprintf("%s", err),
- // })
- // return
- // }
- // fmt.Println(treeData)
- // 添加锁
- idString := c.Ctx.URLParam("id")
- if idString == "" {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": "目录不存在"})
- return
- }
- id, err := utils.GetDecryptId(idString)
- if err != nil {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
- return
- }
- projectId, err := utils.GetProjectId(c.Ctx)
- if err != nil {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
- return
- }
- // 删除目录以及标段
- err = c.ServiceTree.DeleteFolderAndBid(id, projectId)
- if err != nil {
- c.Ctx.JSON(iris.Map{
- "code": -1,
- "msg": fmt.Sprintf("%s", err),
- })
- return
- }
- c.Ctx.JSON(iris.Map{
- "code": 0,
- "msg": "删除成功",
- })
- }
- // @Summary 移动文件夹
- // @Tags 目录相关-管理员
- // @Description 移动文件夹
- // @Accept json
- // @Produce json
- // @Security ApiKeyAuth
- // @Param id body string true "目录ID"
- // @Param moveId body string true "被放置的目录ID"
- // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
- // @Router /api/tree/move [post]
- func (c *TreeApi) PostMove() {
- // 获得目录ID和移动目录ID--TODO 判断
- Tree := viewmodels.Tree{}
- err := c.Ctx.ReadJSON(&Tree)
- if err != nil {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
- return
- }
- id, err := utils.GetDecryptId(Tree.Id)
- if err != nil {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
- return
- }
- moveId, err := utils.GetDecryptId(Tree.TargetFolderId)
- if err != nil {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
- return
- }
- projectId, err := utils.GetProjectId(c.Ctx)
- if err != nil {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
- return
- }
- // 移动目录
- err = c.ServiceTree.Move(id, moveId, projectId)
- if err != nil {
- c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
- return
- }
- c.Ctx.JSON(iris.Map{
- "code": 0,
- "msg": "移动成功",
- })
- }
|