| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 | package v1import (	"github.com/Anderson-Lu/gofasion/gofasion"	"github.com/Unknwon/com"	"github.com/kptyun/KPTCOMM/middleware/inject"	"github.com/kptyun/KPTCOMM/pkg/setting"	"github.com/kptyun/KPTCOMM/pkg/util"	"github.com/kptyun/KPTCOMM/service/role_service"	"io/ioutil"	"net/http"	"github.com/astaxie/beego/validation"	"github.com/gin-gonic/gin"	"../../../pkg/app"	"../../../pkg/e")// @Summary   获取单个角色// @Tags role// @Accept json// @Produce  json// @Param  id  path  string true "id"// @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"// @Router /authdata/roles/:id  [GET]func GetRole(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	}	RoleService := Role_service.Role{ID: id}	exists, err := RoleService.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 := RoleService.Get()	if err != nil {		appG.Response(http.StatusInternalServerError, e.ERROR_NOT_EXIST, nil)		return	}	appG.Response(http.StatusOK, e.SUCCESS, article)}// @Summary   获取所有角色// @Tags role// @Accept json// @Produce  json// @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"// @Router /authdata/roles  [GET]func GetRoles(c *gin.Context) {	appG := app.Gin{C: c}	name := c.Query("name")	valid := validation.Validation{}	if valid.HasErrors() {		app.MarkErrors(valid.Errors)		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)		return	}	RoleService := Role_service.Role{		Name:     name,		PageNum:  util.GetPage(c),		PageSize: setting.AppSetting.PageSize,	}	total, err := RoleService.Count()	if err != nil {		appG.Response(http.StatusInternalServerError, e.ERROR_COUNT_FAIL, nil)		return	}	articles, err := RoleService.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 role// @Accept json// @Produce  json// @Param  name  query  string true "name"// @Param  menu_id  query  string true "menu_id"// @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"// @Router /authdata/roles  [POST]func AddRole(c *gin.Context) {	var (		appG = app.Gin{C: c}	)	dataByte, _ := ioutil.ReadAll(c.Request.Body)	fsion := gofasion.NewFasion(string(dataByte))	name := fsion.Get("username").ValueStr()	menuId := com.StrTo(fsion.Get("menu_id").ValueInt()).MustInt()	valid := validation.Validation{}	valid.MaxSize(name, 100, "path").Message("名称最长为100字符")	if valid.HasErrors() {		app.MarkErrors(valid.Errors)		appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)		return	}	RoleService := Role_service.Role{		Name: name,		Menu: menuId,	}	if id, err := RoleService.Add(); err != nil {		err = inject.Obj.Common.RoleAPI.LoadPolicy(id)		if err != nil {			appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_FAIL, nil)			return		}		appG.Response(http.StatusOK, e.SUCCESS, nil)	} else {		appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)		return	}}// @Summary   更新角色// @Tags role// @Accept json// @Produce  json// @Param  id  path  string true "id"// @Param  name  query  string true "name"// @Param  menu_id  query  string true "menu_id"// @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"// @Router /authdata/roles/:id  [PUT]func EditRole(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("username").ValueStr()	menuId := com.StrTo(fsion.Get("menu_id").ValueInt()).MustInt()	valid := validation.Validation{}	valid.MaxSize(name, 100, "path").Message("名称最长为100字符")	if valid.HasErrors() {		app.MarkErrors(valid.Errors)		appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)		return	}	RoleService := Role_service.Role{		ID:   id,		Name: name,		Menu: menuId,	}	exists, err := RoleService.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 = RoleService.Edit()	if err != nil {		appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_FAIL, nil)		return	}	err = inject.Obj.Common.RoleAPI.LoadPolicy(id)	if err != nil {		appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_FAIL, nil)		return	}	appG.Response(http.StatusOK, e.SUCCESS, nil)}// @Summary   删除角色// @Tags role// @Accept json// @Produce  json// @Param  id  path  string true "id"// @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"// @Router /authdata/roles/:id  [DELETE]func DeleteRole(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	}	RoleService := Role_service.Role{ID: id}	exists, err := RoleService.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	}	role, err := RoleService.Get()	err = RoleService.Delete()	if err != nil {		appG.Response(http.StatusInternalServerError, e.ERROR_DELETE_FAIL, nil)		return	}	inject.Obj.Enforcer.DeleteUser(role.Name)	appG.Response(http.StatusOK, e.SUCCESS, nil)}
 |