package v1 /* import ( "io/ioutil" "net/http" "tmr-watch/pkg/app" "tmr-watch/pkg/e" "tmr-watch/conf/setting" "tmr-watch/pkg/util" "tmr-watch/service/menu_service" "github.com/Anderson-Lu/gofasion/gofasion" "github.com/Unknwon/com" "github.com/astaxie/beego/validation" "github.com/gin-gonic/gin" ) // @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) } */