package v1 import ( "github.com/Anderson-Lu/gofasion/gofasion" "github.com/Unknwon/com" "github.com/astaxie/beego/validation" "github.com/gin-gonic/gin" "../../../pkg/app" "../../../pkg/e" "../../../pkg/setting" "../../../pkg/util" "../../../service/menu_service" "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["lists"] = 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) }