c_menu.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package controllers
  2. import (
  3. "github.com/gogf/gf/v2/net/ghttp"
  4. "yx-dataset-server/app/bll"
  5. "yx-dataset-server/app/errors"
  6. "yx-dataset-server/app/schema"
  7. "yx-dataset-server/library/gplus"
  8. )
  9. type Menu struct {
  10. MenuBll bll.IMenu
  11. }
  12. func NewMenu(bMenu bll.IMenu) *Menu {
  13. return &Menu{
  14. MenuBll: bMenu,
  15. }
  16. }
  17. // Query 查询数据
  18. func (a *Menu) Query(r *ghttp.Request) {
  19. switch r.Get("q").String() {
  20. case "page":
  21. a.QueryPage(r)
  22. case "list":
  23. a.QueryList(r)
  24. case "tree":
  25. a.QueryTree(r)
  26. default:
  27. ResError(r, errors.ErrUnknownQuery)
  28. }
  29. }
  30. // QueryPage 查询分页数据
  31. // @Tags
  32. // @Summary 查询分页数据
  33. // @Param Authorization header string false "Bearer 用户令牌"
  34. // @Param current query int true "分页索引" 1
  35. // @Param pageSize query int true "分页大小" 10
  36. // @Success 200 []schema.Menu "查询结果:{list:列表数据,pagination:{current:页索引,pageSize:页大小,total:总数量}}"
  37. // @Failure 400 schema.HTTPError "{error:{code:0,message:未知的查询类型}}"
  38. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  39. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  40. // @Router GET /web/v1/menu?q=page
  41. func (a *Menu) QueryPage(r *ghttp.Request) {
  42. ctx := gplus.NewContext(r)
  43. var params schema.MenuQueryParam
  44. result, err := a.MenuBll.Query(ctx, params, schema.MenuQueryOptions{
  45. PageParam: &schema.PaginationParam{
  46. PageIndex: gplus.GetPageIndex(r),
  47. PageSize: gplus.GetPageSize(r),
  48. },
  49. })
  50. if err != nil {
  51. ResError(r, err)
  52. }
  53. ResPage(r, result.Data, result.PageResult)
  54. }
  55. // QueryList 查询列表数据
  56. // @Tags
  57. // @Summary 查询列表数据
  58. // @Param Authorization header string false "Bearer 用户令牌"
  59. // @Success 200 []schema.Menu "查询结果:{list:列表数据}"
  60. // @Failure 400 schema.HTTPError "{error:{code:0,message:未知的查询类型}}"
  61. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  62. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  63. // @Router GET /web/v1/menu?q=list
  64. func (a *Menu) QueryList(r *ghttp.Request) {
  65. ctx := gplus.NewContext(r)
  66. var params schema.MenuQueryParam
  67. result, err := a.MenuBll.Query(ctx, params)
  68. if err != nil {
  69. gplus.ResError(r, err)
  70. }
  71. ResSuccess(r, result.Data)
  72. }
  73. // @Router GET /web/v1/menus?q=tree
  74. func (a *Menu) QueryTree(r *ghttp.Request) {
  75. ctx := gplus.NewContext(r)
  76. var params schema.MenuQueryParam
  77. result, err := a.MenuBll.QueryTree(ctx, params)
  78. if err != nil {
  79. gplus.ResError(r, err)
  80. }
  81. ResSuccess(r, result)
  82. }
  83. // Get 获取数据
  84. // @Summary 获取数据
  85. // @Param id path string true "记录ID"
  86. // @Success 200 schema.Menu
  87. // @Failure 400 schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
  88. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  89. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  90. // @Router /web/v1/menu/{id}
  91. func (a *Menu) Get(r *ghttp.Request) {
  92. ctx := gplus.NewContext(r)
  93. result, err := a.MenuBll.Get(ctx, r.Get("id").String())
  94. if err != nil {
  95. ResError(r, err)
  96. }
  97. ResSuccess(r, result)
  98. }
  99. // Create 创建数据
  100. // @Tags API-Menu
  101. // @Summary 创建数据
  102. // @Param body schema.Menu true "提交的数据"
  103. // @Success 200 schema.Menu
  104. // @Failure 400 schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
  105. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  106. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  107. // @Router /web/v1/menu/ [post]
  108. func (a *Menu) Create(r *ghttp.Request) {
  109. var data schema.Menu
  110. if err := gplus.ParseJson(r, &data); err != nil {
  111. ResError(r, err)
  112. }
  113. ctx := gplus.NewContext(r)
  114. err := a.MenuBll.Create(ctx, data)
  115. if err != nil {
  116. ResError(r, err)
  117. }
  118. ResSuccess(r, nil)
  119. }
  120. // Update 更新数据
  121. // @Summary 更新数据
  122. // @Param body schema.Menu true "更新数据"
  123. // @Success 200 schema.Menu
  124. // @Failure 400 schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
  125. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  126. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  127. // @Router /web/v1/menu/{id}
  128. func (a *Menu) Update(r *ghttp.Request) {
  129. var data schema.Menu
  130. if err := gplus.ParseJson(r, &data); err != nil {
  131. ResError(r, err)
  132. }
  133. ctx := gplus.NewContext(r)
  134. err := a.MenuBll.Update(ctx, r.Get("id").String(), data)
  135. if err != nil {
  136. ResError(r, err)
  137. }
  138. ResSuccess(r, nil)
  139. }
  140. // Delete 删除数据
  141. // @Summary 删除数据
  142. // @Param id path string true "记录ID"
  143. // @Success 200 schema.HTTPStatus "{status:OK}"
  144. // @Failure 400 schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
  145. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  146. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  147. // @Router /web/v1/menu/{id}
  148. func (a *Menu) Delete(r *ghttp.Request) {
  149. ctx := gplus.NewContext(r)
  150. err := a.MenuBll.Delete(ctx, r.Get("id").String())
  151. if err != nil {
  152. ResError(r, err)
  153. }
  154. ResSuccess(r, nil)
  155. }
  156. func (a *Menu) UpdateSequence(r *ghttp.Request) {
  157. var data schema.Menu
  158. if err := gplus.ParseJson(r, &data); err != nil {
  159. gplus.ResError(r, err)
  160. }
  161. ctx := gplus.NewContext(r)
  162. err := a.MenuBll.UpdateSequence(ctx, r.Get("id").String(), data.Sequence)
  163. if err != nil {
  164. gplus.ResError(r, err)
  165. }
  166. ResSuccess(r, nil)
  167. }