menu.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package v1
  2. /*
  3. import (
  4. "io/ioutil"
  5. "net/http"
  6. "tmr-watch/pkg/app"
  7. "tmr-watch/pkg/e"
  8. "tmr-watch/conf/setting"
  9. "tmr-watch/pkg/util"
  10. "tmr-watch/service/menu_service"
  11. "github.com/Anderson-Lu/gofasion/gofasion"
  12. "github.com/Unknwon/com"
  13. "github.com/astaxie/beego/validation"
  14. "github.com/gin-gonic/gin"
  15. )
  16. // @Summary 获取单个菜单
  17. // @Tags menu
  18. // @Accept json
  19. // @Produce json
  20. // @Param id path string true "id"
  21. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  22. // @Router /authdata/menus/:id [GET]
  23. func GetMenu(c *gin.Context) {
  24. appG := app.Gin{C: c}
  25. id := com.StrTo(c.Param("id")).MustInt()
  26. valid := validation.Validation{}
  27. valid.Min(id, 1, "id").Message("ID必须大于0")
  28. if valid.HasErrors() {
  29. app.MarkErrors(valid.Errors)
  30. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  31. return
  32. }
  33. menuService := menu_service.Menu{ID: id}
  34. exists, err := menuService.ExistByID()
  35. if err != nil {
  36. appG.Response(http.StatusInternalServerError, e.ERROR_NOT_EXIST, nil)
  37. return
  38. }
  39. if !exists {
  40. appG.Response(http.StatusOK, e.ERROR_NOT_EXIST, nil)
  41. return
  42. }
  43. article, err := menuService.Get()
  44. if err != nil {
  45. appG.Response(http.StatusInternalServerError, e.ERROR_NOT_EXIST, nil)
  46. return
  47. }
  48. appG.Response(http.StatusOK, e.SUCCESS, article)
  49. }
  50. // @Summary 获取所有菜单
  51. // @Tags menu
  52. // @Accept json
  53. // @Produce json
  54. // @Param Ton query string true "Ton"
  55. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  56. // @Router /authdata/menus [GET]
  57. func GetMenus(c *gin.Context) {
  58. appG := app.Gin{C: c}
  59. valid := validation.Validation{}
  60. if valid.HasErrors() {
  61. app.MarkErrors(valid.Errors)
  62. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  63. return
  64. }
  65. menuService := menu_service.Menu{
  66. PageNum: util.GetPage(c),
  67. PageSize: setting.AppSetting.PageSize,
  68. }
  69. total, err := menuService.Count()
  70. if err != nil {
  71. appG.Response(http.StatusInternalServerError, e.ERROR_COUNT_FAIL, nil)
  72. return
  73. }
  74. articles, err := menuService.GetAll()
  75. if err != nil {
  76. appG.Response(http.StatusInternalServerError, e.ERROR_GET_S_FAIL, nil)
  77. return
  78. }
  79. data := make(map[string]interface{})
  80. data["lists"] = articles
  81. data["total"] = total
  82. appG.Response(http.StatusOK, e.SUCCESS, data)
  83. }
  84. // @Summary 增加菜单
  85. // @Tags menu
  86. // @Accept json
  87. // @Produce json
  88. // @Param name query string true "name"
  89. // @Param path query string true "path"
  90. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  91. // @Router /authdata/menus [POST]
  92. func AddMenu(c *gin.Context) {
  93. var (
  94. appG = app.Gin{C: c}
  95. )
  96. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  97. fsion := gofasion.NewFasion(string(dataByte))
  98. name := fsion.Get("name").ValueStr()
  99. path := fsion.Get("path").ValueStr()
  100. method := fsion.Get("method").ValueStr()
  101. valid := validation.Validation{}
  102. valid.MaxSize(name, 100, "name").Message("最长为100字符")
  103. valid.MaxSize(path, 100, "path").Message("最长为100字符")
  104. valid.MaxSize(method, 100, "method").Message("最长为100字符")
  105. if valid.HasErrors() {
  106. app.MarkErrors(valid.Errors)
  107. appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
  108. return
  109. }
  110. menuService := menu_service.Menu{
  111. Name: name,
  112. Path: path,
  113. Method: method,
  114. }
  115. if err := menuService.Add(); err != nil {
  116. appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
  117. return
  118. }
  119. appG.Response(http.StatusOK, e.SUCCESS, nil)
  120. }
  121. // @Summary 更新菜单
  122. // @Tags menu
  123. // @Accept json
  124. // @Produce json
  125. // @Param id path string true "id"
  126. // @Param name query string true "name"
  127. // @Param path query string true "path"
  128. // @Param method query string true "method"
  129. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  130. // @Router /authdata/menus/:id [PUT]
  131. func EditMenu(c *gin.Context) {
  132. var (
  133. appG = app.Gin{C: c}
  134. )
  135. id := com.StrTo(c.Param("id")).MustInt()
  136. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  137. fsion := gofasion.NewFasion(string(dataByte))
  138. name := fsion.Get("name").ValueStr()
  139. path := fsion.Get("path").ValueStr()
  140. method := fsion.Get("method").ValueStr()
  141. valid := validation.Validation{}
  142. valid.MaxSize(name, 100, "name").Message("最长为100字符")
  143. valid.MaxSize(path, 100, "path").Message("最长为100字符")
  144. valid.MaxSize(method, 100, "method").Message("最长为100字符")
  145. valid.Min(id, 1, "id").Message("ID必须大于0")
  146. if valid.HasErrors() {
  147. app.MarkErrors(valid.Errors)
  148. appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
  149. return
  150. }
  151. menuService := menu_service.Menu{
  152. Name: name,
  153. Path: path,
  154. Method: method,
  155. }
  156. exists, err := menuService.ExistByID()
  157. if err != nil {
  158. appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_FAIL, nil)
  159. return
  160. }
  161. if !exists {
  162. appG.Response(http.StatusOK, e.ERROR_EXIST_FAIL, nil)
  163. return
  164. }
  165. err = menuService.Edit()
  166. if err != nil {
  167. appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_FAIL, nil)
  168. return
  169. }
  170. appG.Response(http.StatusOK, e.SUCCESS, nil)
  171. }
  172. // @Summary 删除菜单
  173. // @Tags menu
  174. // @Accept json
  175. // @Produce json
  176. // @Param id path string true "id"
  177. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  178. // @Router /authdata/menus/:id [DELETE]
  179. func DeleteMenu(c *gin.Context) {
  180. appG := app.Gin{C: c}
  181. valid := validation.Validation{}
  182. id := com.StrTo(c.Param("id")).MustInt()
  183. valid.Min(id, 1, "id").Message("ID必须大于0")
  184. if valid.HasErrors() {
  185. app.MarkErrors(valid.Errors)
  186. appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
  187. return
  188. }
  189. menuService := menu_service.Menu{ID: id}
  190. exists, err := menuService.ExistByID()
  191. if err != nil {
  192. appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_FAIL, nil)
  193. return
  194. }
  195. if !exists {
  196. appG.Response(http.StatusOK, e.ERROR_EXIST_FAIL, nil)
  197. return
  198. }
  199. err = menuService.Delete()
  200. if err != nil {
  201. appG.Response(http.StatusInternalServerError, e.ERROR_DELETE_FAIL, nil)
  202. return
  203. }
  204. appG.Response(http.StatusOK, e.SUCCESS, nil)
  205. }
  206. */