menu.go 5.9 KB

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