config.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package system
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/gin-gonic/gin/binding"
  6. "device-manage/app/admin/models"
  7. "device-manage/tools"
  8. "device-manage/tools/app"
  9. "device-manage/tools/app/msg"
  10. )
  11. // @Summary 配置列表数据
  12. // @Description 获取JSON
  13. // @Tags 配置
  14. // @Param configKey query string false "configKey"
  15. // @Param configName query string false "configName"
  16. // @Param configType query string false "configType"
  17. // @Param pageSize query int false "页条数"
  18. // @Param pageIndex query int false "页码"
  19. // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
  20. // @Router /api/v1/configList [get]
  21. // @Security Bearer
  22. func GetConfigList(c *gin.Context) {
  23. var data models.SysConfig
  24. var err error
  25. var pageSize = 10
  26. var pageIndex = 1
  27. if size := c.Request.FormValue("pageSize"); size != "" {
  28. pageSize, err = tools.StringToInt(size)
  29. }
  30. if index := c.Request.FormValue("pageIndex"); index != "" {
  31. pageIndex, err = tools.StringToInt(index)
  32. }
  33. data.ConfigKey = c.Request.FormValue("configKey")
  34. data.ConfigName = c.Request.FormValue("configName")
  35. data.ConfigType = c.Request.FormValue("configType")
  36. data.DataScope = tools.GetUserIdStr(c)
  37. result, count, err := data.GetPage(pageSize, pageIndex)
  38. tools.HasError(err, "", -1)
  39. var mp = make(map[string]interface{}, 3)
  40. mp["list"] = result
  41. mp["count"] = count
  42. mp["pageIndex"] = pageIndex
  43. mp["pageSize"] = pageSize
  44. var res app.Response
  45. res.Data = mp
  46. c.JSON(http.StatusOK, res.ReturnOK())
  47. }
  48. // @Summary 获取配置
  49. // @Description 获取JSON
  50. // @Tags 配置
  51. // @Param configId path int true "配置编码"
  52. // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
  53. // @Router /api/v1/config/{configId} [get]
  54. // @Security Bearer
  55. func GetConfig(c *gin.Context) {
  56. var Config models.SysConfig
  57. Config.ConfigId, _ = tools.StringToInt(c.Param("configId"))
  58. result, err := Config.Get()
  59. tools.HasError(err, "抱歉未找到相关信息", -1)
  60. var res app.Response
  61. res.Data = result
  62. c.JSON(http.StatusOK, res.ReturnOK())
  63. }
  64. // @Summary 获取配置
  65. // @Description 获取JSON
  66. // @Tags 配置
  67. // @Param configKey path int true "configKey"
  68. // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
  69. // @Router /api/v1/configKey/{configKey} [get]
  70. // @Security Bearer
  71. func GetConfigByConfigKey(c *gin.Context) {
  72. var Config models.SysConfig
  73. Config.ConfigKey = c.Param("configKey")
  74. result, err := Config.Get()
  75. tools.HasError(err, "抱歉未找到相关信息", -1)
  76. app.OK(c, result, result.ConfigValue)
  77. }
  78. // @Summary 添加配置
  79. // @Description 获取JSON
  80. // @Tags 配置
  81. // @Accept application/json
  82. // @Product application/json
  83. // @Param data body models.SysConfig true "data"
  84. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  85. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  86. // @Router /api/v1/dict/data [post]
  87. // @Security Bearer
  88. func InsertConfig(c *gin.Context) {
  89. var data models.SysConfig
  90. err := c.BindWith(&data, binding.JSON)
  91. data.CreateBy = tools.GetUserIdStr(c)
  92. tools.HasError(err, "", 500)
  93. result, err := data.Create()
  94. tools.HasError(err, "", -1)
  95. app.OK(c, result, "")
  96. }
  97. // @Summary 修改配置
  98. // @Description 获取JSON
  99. // @Tags 配置
  100. // @Accept application/json
  101. // @Product application/json
  102. // @Param data body models.SysConfig true "body"
  103. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  104. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  105. // @Router /api/v1/config [put]
  106. // @Security Bearer
  107. func UpdateConfig(c *gin.Context) {
  108. var data models.SysConfig
  109. err := c.BindWith(&data, binding.JSON)
  110. tools.HasError(err, "数据解析失败", -1)
  111. data.UpdateBy = tools.GetUserIdStr(c)
  112. result, err := data.Update(data.ConfigId)
  113. tools.HasError(err, "", -1)
  114. app.OK(c, result, "")
  115. }
  116. // @Summary 删除配置
  117. // @Description 删除数据
  118. // @Tags 配置
  119. // @Param configId path int true "configId"
  120. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  121. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  122. // @Router /api/v1/config/{configId} [delete]
  123. func DeleteConfig(c *gin.Context) {
  124. var data models.SysConfig
  125. data.UpdateBy = tools.GetUserIdStr(c)
  126. IDS := tools.IdsStrToIdsIntGroup("configId", c)
  127. result, err := data.BatchDelete(IDS)
  128. tools.HasError(err, "修改失败", 500)
  129. app.OK(c, result, msg.DeletedSuccess)
  130. }