annex_api.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * @description: 附件相关的接口api
  3. * @Author: LanJianRong
  4. * @Date: 2020-12-07 10:26:49
  5. * @FilePath: \construction_management\web\api\annex_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. )
  15. type AnnexApi struct {
  16. //框架-web应用上下文环境
  17. Ctx iris.Context
  18. // 需要用的service
  19. ServiceAnnex services.AnnexService
  20. }
  21. // @Summary 获取附件列表
  22. // @Tags 附件
  23. // @Description 获得附件列表
  24. // @Accept json
  25. // @Produce json
  26. // @Security ApiKeyAuth
  27. // @Param dataType path int true "附件类型"
  28. // @Param dataId path string true "源数据id"
  29. // @Success 200 {object} viewmodels.AnnexList "{code:0成功,data:viewmodels.AnnexList,msg:}"
  30. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  31. // @Router /api/file [get]
  32. func (c *AnnexApi) Get() {
  33. // 1.规则验证
  34. annexData, err := c.ServiceAnnex.ValidRule(c.Ctx)
  35. if err != nil {
  36. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  37. return
  38. }
  39. dataId, err := utils.GetDecryptId(annexData.DataId)
  40. dataType, err := strconv.Atoi(annexData.DataType)
  41. // data = c.ServiceAnnex.Get(dataType, dataId)
  42. // dataType, err := strconv.Atoi(annexData.DataType)
  43. data := c.ServiceAnnex.Get(dataType, dataId)
  44. c.Ctx.JSON(iris.Map{
  45. "code": 0,
  46. "msg": "请求成功",
  47. "data": data,
  48. })
  49. }
  50. // @Summary 提交文件记录
  51. // @Tags 附件
  52. // @Description 提交文件记录
  53. // @Accept json
  54. // @Produce json
  55. // @Security ApiKeyAuth
  56. // @Param fileList body array true "附件数组"
  57. // @Param dataType body int true "类型" eg:"1"
  58. // @Param dataId body string true "数据id"
  59. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  60. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  61. // @Router /api/file [post]
  62. func (c *AnnexApi) Post() {
  63. // 1.规则验证
  64. annexData, err := c.ServiceAnnex.ValidCreate(c.Ctx)
  65. if err != nil {
  66. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  67. return
  68. }
  69. dataId, err := utils.GetDecryptId(annexData.DataId)
  70. if err != nil {
  71. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  72. return
  73. }
  74. uid, err := utils.GetProjectAccountId(c.Ctx)
  75. if err != nil {
  76. c.Ctx.JSON(iris.Map{"code": -1, "msg": "未登录或账号失效,请重新登录"})
  77. return
  78. }
  79. err = c.ServiceAnnex.Create(uid, annexData.DataType, dataId, annexData.FileList)
  80. if err != nil {
  81. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  82. return
  83. }
  84. c.Ctx.JSON(iris.Map{
  85. "code": 0,
  86. "msg": "请求成功",
  87. })
  88. }
  89. // @Summary 删除附件
  90. // @Tags 附件
  91. // @Description 删除附件
  92. // @Accept json
  93. // @Produce json
  94. // @Security ApiKeyAuth
  95. // @Param id body string true "数据id"
  96. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  97. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  98. // @Router /api/file [delete]
  99. func (c *AnnexApi) Delete() {
  100. // 1.规则验证
  101. queryId := c.Ctx.URLParam("id")
  102. if queryId == "" {
  103. c.Ctx.JSON(iris.Map{"code": -1, "msg": "id不能为空"})
  104. return
  105. }
  106. id, err := utils.GetDecryptId(queryId)
  107. if err != nil {
  108. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  109. return
  110. }
  111. err = c.ServiceAnnex.Delete(id)
  112. if err != nil {
  113. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  114. return
  115. }
  116. c.Ctx.JSON(iris.Map{
  117. "code": 0,
  118. "msg": "请求成功",
  119. })
  120. }