123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- package v1
- import (
- "github.com/Anderson-Lu/gofasion/gofasion"
- "github.com/astaxie/beego/validation"
- "github.com/gin-gonic/gin"
- "github.com/kptyun/KPTCOMM/pkg/app"
- "github.com/kptyun/KPTCOMM/pkg/e"
- "github.com/kptyun/KPTCOMM/pkg/setting"
- "github.com/kptyun/KPTCOMM/pkg/util"
- //"github.com/kptyun/KPTCOMM/service/menu_service"
- "github.com/unknwon/com"
- "io/ioutil"
- "net/http"
- )
- // @Summary 获取单个菜单
- // @Tags menu
- // @Accept json
- // @Produce json
- // @Param id path string true "id"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Router /authdata/menus/:id [GET]
- func GetMenu(c *gin.Context) {
- appG := app.Gin{C: c}
- id := com.StrTo(c.Param("id")).MustInt()
- valid := validation.Validation{}
- valid.Min(id, 1, "id").Message("ID必须大于0")
- if valid.HasErrors() {
- app.MarkErrors(valid.Errors)
- appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
- return
- }
- menuService := menu_service.Menu{ID: id}
- exists, err := menuService.ExistByID()
- if err != nil {
- appG.Response(http.StatusInternalServerError, e.ERROR_NOT_EXIST, nil)
- return
- }
- if !exists {
- appG.Response(http.StatusOK, e.ERROR_NOT_EXIST, nil)
- return
- }
- article, err := menuService.Get()
- if err != nil {
- appG.Response(http.StatusInternalServerError, e.ERROR_NOT_EXIST, nil)
- return
- }
- appG.Response(http.StatusOK, e.SUCCESS, article)
- }
- // @Summary 获取所有菜单
- // @Tags menu
- // @Accept json
- // @Produce json
- // @Param Ton query string true "Ton"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Router /authdata/menus [GET]
- func GetMenus(c *gin.Context) {
- appG := app.Gin{C: c}
- valid := validation.Validation{}
- if valid.HasErrors() {
- app.MarkErrors(valid.Errors)
- appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
- return
- }
- menuService := menu_service.Menu{
- PageNum: util.GetPage(c),
- PageSize: setting.AppSetting.PageSize,
- }
- total, err := menuService.Count()
- if err != nil {
- appG.Response(http.StatusInternalServerError, e.ERROR_COUNT_FAIL, nil)
- return
- }
- articles, err := menuService.GetAll()
- if err != nil {
- appG.Response(http.StatusInternalServerError, e.ERROR_GET_S_FAIL, nil)
- return
- }
- data := make(map[string]interface{})
- data["rows"] = articles
- data["total"] = total
- appG.Response(http.StatusOK, e.SUCCESS, data)
- }
- // @Summary 增加菜单
- // @Tags menu
- // @Accept json
- // @Produce json
- // @Param name query string true "name"
- // @Param path query string true "path"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Router /authdata/menus [POST]
- func AddMenu(c *gin.Context) {
- var (
- appG = app.Gin{C: c}
- )
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- name := fsion.Get("name").ValueStr()
- path := fsion.Get("path").ValueStr()
- method := fsion.Get("method").ValueStr()
- valid := validation.Validation{}
- valid.MaxSize(name, 100, "name").Message("最长为100字符")
- valid.MaxSize(path, 100, "path").Message("最长为100字符")
- valid.MaxSize(method, 100, "method").Message("最长为100字符")
- if valid.HasErrors() {
- app.MarkErrors(valid.Errors)
- appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
- return
- }
- menuService := menu_service.Menu{
- Name: name,
- Path: path,
- Method: method,
- }
- if err := menuService.Add(); err != nil {
- appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
- return
- }
- appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
- // @Summary 更新菜单
- // @Tags menu
- // @Accept json
- // @Produce json
- // @Param id path string true "id"
- // @Param name query string true "name"
- // @Param path query string true "path"
- // @Param method query string true "method"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Router /authdata/menus/:id [PUT]
- func EditMenu(c *gin.Context) {
- var (
- appG = app.Gin{C: c}
- )
- id := com.StrTo(c.Param("id")).MustInt()
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- name := fsion.Get("name").ValueStr()
- path := fsion.Get("path").ValueStr()
- method := fsion.Get("method").ValueStr()
- valid := validation.Validation{}
- valid.MaxSize(name, 100, "name").Message("最长为100字符")
- valid.MaxSize(path, 100, "path").Message("最长为100字符")
- valid.MaxSize(method, 100, "method").Message("最长为100字符")
- valid.Min(id, 1, "id").Message("ID必须大于0")
- if valid.HasErrors() {
- app.MarkErrors(valid.Errors)
- appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
- return
- }
- menuService := menu_service.Menu{
- Name: name,
- Path: path,
- Method: method,
- }
- exists, err := menuService.ExistByID()
- if err != nil {
- appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_FAIL, nil)
- return
- }
- if !exists {
- appG.Response(http.StatusOK, e.ERROR_EXIST_FAIL, nil)
- return
- }
- err = menuService.Edit()
- if err != nil {
- appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_FAIL, nil)
- return
- }
- appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
- // @Summary 删除菜单
- // @Tags menu
- // @Accept json
- // @Produce json
- // @Param id path string true "id"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Router /authdata/menus/:id [DELETE]
- func DeleteMenu(c *gin.Context) {
- appG := app.Gin{C: c}
- valid := validation.Validation{}
- id := com.StrTo(c.Param("id")).MustInt()
- valid.Min(id, 1, "id").Message("ID必须大于0")
- if valid.HasErrors() {
- app.MarkErrors(valid.Errors)
- appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
- return
- }
- menuService := menu_service.Menu{ID: id}
- exists, err := menuService.ExistByID()
- if err != nil {
- appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_FAIL, nil)
- return
- }
- if !exists {
- appG.Response(http.StatusOK, e.ERROR_EXIST_FAIL, nil)
- return
- }
- err = menuService.Delete()
- if err != nil {
- appG.Response(http.StatusInternalServerError, e.ERROR_DELETE_FAIL, nil)
- return
- }
- appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
|