annex_api.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // @Param pageNo path int true "页码" eg:1
  30. // @Param pageSize path int true "页数" eg:15
  31. // @Success 200 {object} viewmodels.AnnexList "{code:0成功,data:viewmodels.AnnexList,msg:}"
  32. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  33. // @Router /api/file [get]
  34. func (c *AnnexApi) Get() {
  35. // 1.规则验证
  36. annexData, err := c.ServiceAnnex.ValidRule(c.Ctx)
  37. if err != nil {
  38. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  39. return
  40. }
  41. dataId, err := utils.GetDecryptId(annexData.DataId)
  42. dataType, err := strconv.Atoi(annexData.DataType)
  43. // data = c.ServiceAnnex.Get(dataType, dataId)
  44. // dataType, err := strconv.Atoi(annexData.DataType)
  45. data, total := c.ServiceAnnex.Get(dataType, dataId, annexData.PageNo, annexData.PageSize)
  46. c.Ctx.JSON(iris.Map{
  47. "code": 0,
  48. "msg": "请求成功",
  49. "data": data,
  50. "total": total,
  51. })
  52. }
  53. // @Summary 提交文件记录
  54. // @Tags 附件
  55. // @Description 提交文件记录
  56. // @Accept json
  57. // @Produce json
  58. // @Security ApiKeyAuth
  59. // @Param fileList body array true "附件数组"
  60. // @Param dataType body int true "类型" eg:"1"
  61. // @Param dataId body string true "数据id"
  62. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  63. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  64. // @Router /api/file [post]
  65. func (c *AnnexApi) Post() {
  66. // 1.规则验证
  67. annexData, err := c.ServiceAnnex.ValidCreate(c.Ctx)
  68. if err != nil {
  69. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  70. return
  71. }
  72. dataId, err := utils.GetDecryptId(annexData.DataId)
  73. if err != nil {
  74. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  75. return
  76. }
  77. uid, err := utils.GetProjectAccountId(c.Ctx)
  78. if err != nil {
  79. c.Ctx.JSON(iris.Map{"code": -1, "msg": "未登录或账号失效,请重新登录"})
  80. return
  81. }
  82. err = c.ServiceAnnex.Create(uid, annexData.DataType, dataId, annexData.FileList)
  83. if err != nil {
  84. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  85. return
  86. }
  87. c.Ctx.JSON(iris.Map{
  88. "code": 0,
  89. "msg": "请求成功",
  90. })
  91. }
  92. // @Summary 删除附件
  93. // @Tags 附件
  94. // @Description 删除附件
  95. // @Accept json
  96. // @Produce json
  97. // @Security ApiKeyAuth
  98. // @Param id body string true "数据id"
  99. // @Success 200 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  100. // @Failure 400 {string} string "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
  101. // @Router /api/file [delete]
  102. func (c *AnnexApi) Delete() {
  103. // 1.规则验证
  104. queryId := c.Ctx.URLParam("id")
  105. if queryId == "" {
  106. c.Ctx.JSON(iris.Map{"code": -1, "msg": "id不能为空"})
  107. return
  108. }
  109. id, err := utils.GetDecryptId(queryId)
  110. if err != nil {
  111. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  112. return
  113. }
  114. err = c.ServiceAnnex.Delete(id)
  115. if err != nil {
  116. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  117. return
  118. }
  119. c.Ctx.JSON(iris.Map{
  120. "code": 0,
  121. "msg": "请求成功",
  122. })
  123. }