123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- package controller
- import (
- "Medical_OAuth/common/actions"
- "Medical_OAuth/common/global"
- "github.com/gin-gonic/gin"
- "net/http"
- "Medical_OAuth/app/admin/model"
- "Medical_OAuth/app/admin/service"
- "Medical_OAuth/app/admin/service/dto"
- "gogs.baozhida.cn/zoie/OAuth-core/api"
- "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
- _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
- "gogs.baozhida.cn/zoie/OAuth-core/sdk"
- "gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
- "github.com/gin-gonic/gin/binding"
- )
- type SysRoleController struct {
- api.Api
- }
- // GetPage 获取角色列表
- // @Summary 角色列表数据
- // @Description 获取角色列表
- // @Tags 角色
- // @Param roleName query string false "角色名称"
- // @Param pageSize query int false "页条数"
- // @Param pageIndex query int false "页码"
- // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysRole}} "{"code": 200, "data": [...]}"
- // @Router /api/role [get]
- // @Security Bearer
- func (e SysRoleController) GetPage(c *gin.Context) {
- s := service.SysRole{}
- req := dto.SysRoleGetPageReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, binding.Form).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- list := make([]model.SysRole, 0)
- var count int64
- //数据权限检查
- p := actions.GetPermissionFromContext(c)
- err = s.GetPage(&req, &list, &count, p)
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
- }
- // Get 通过id获取角色
- // @Summary 通过id获取角色
- // @Description 通过id获取角色
- // @Tags 角色
- // @Param id path string true "角色Id"
- // @Success 200 {object} response.Response{data=model.SysRole} "{"code": 200, "data": [...]}"
- // @Router /api/role/{id} [get]
- // @Security Bearer
- func (e SysRoleController) Get(c *gin.Context) {
- s := service.SysRole{}
- req := dto.SysRoleGetReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, nil).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- var object model.SysRole
- //数据权限检查
- p := actions.GetPermissionFromContext(c)
- err = s.Get(&req, &object, p)
- if err != nil {
- e.Logger.Error(err)
- e.Error(http.StatusUnprocessableEntity, err, err.Error())
- return
- }
- e.OK(object, "查询成功")
- }
- // Insert 创建角色
- // @Summary 创建角色
- // @Description 创建角色
- // @Tags 角色
- // @Accept application/json
- // @Product application/json
- // @Param data body dto.SysRoleInsertReq true "data"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/role [post]
- // @Security Bearer
- func (e SysRoleController) Insert(c *gin.Context) {
- s := service.SysRole{}
- req := dto.SysRoleInsertReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, binding.JSON).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- // 设置创建人
- req.CreateBy = user.GetUserId(c)
- err = s.Insert(&req)
- if err != nil {
- e.Error(500, err, err.Error())
- return
- }
- _, err = global.LoadPolicy(c)
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- e.OK(req.GetId(), "创建成功")
- }
- // Update 修改用户角色
- // @Summary 修改用户角色
- // @Description 修改用户角色
- // @Tags 角色
- // @Accept application/json
- // @Product application/json
- // @Param id path string true "角色Id"
- // @Param data body dto.SysRoleUpdateReq true "body"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/role/{id} [put]
- // @Security Bearer
- func (e SysRoleController) Update(c *gin.Context) {
- s := service.SysRole{}
- req := dto.SysRoleUpdateReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, nil, binding.JSON).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- req.SetUpdateBy(user.GetUserId(c))
- //数据权限检查
- p := actions.GetPermissionFromContext(c)
- err = s.Update(&req, p)
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- _, err = global.LoadPolicy(c)
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- e.OK(req.GetId(), "更新成功")
- }
- // Delete 删除用户角色
- // @Summary 删除用户角色
- // @Description 删除用户角色
- // @Tags 角色
- // @Accept application/json
- // @Product application/json
- // @Param data body dto.SysRoleDeleteReq true "body"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/role [delete]
- // @Security Bearer
- func (e SysRoleController) Delete(c *gin.Context) {
- s := new(service.SysRole)
- req := dto.SysRoleDeleteReq{}
- userSvc := service.SysUser{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, binding.JSON).
- MakeService(&s.Service).
- MakeService(&userSvc.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- userReq := dto.SysUserGetCountReq{RoleIds: []int{req.Id}}
- var count int64
- err = userSvc.GetCount(&userReq, &count)
- if err != nil {
- e.Error(500, err, err.Error())
- return
- }
- if count > 0 {
- e.Error(500, err, "有用户关联,禁止删除!")
- return
- }
- //数据权限检查
- p := actions.GetPermissionFromContext(c)
- //cb := sdk.Runtime.GetCasbinKey(c.Request.Host)
- cb := sdk.Runtime.GetCasbinKey(config.ApplicationConfig.Host)
- // fixme 删除角色与路由,角色与菜单之间的关联
- err = s.Remove(&req, p, cb)
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- e.OK(req.GetId(), "删除成功")
- }
- // UpdateRoleApi 修改角色路由权限
- // @Summary 修改角色路由权限
- // @Description 修改角色路由权限
- // @Tags 角色
- // @Accept application/json
- // @Product application/json
- // @Param id path string true "角色Id"
- // @Param data body dto.SysRoleUpdateRoleApiReq true "body"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/role-api/{id} [put]
- // @Security Bearer
- func (e SysRoleController) UpdateRoleApi(c *gin.Context) {
- s := service.SysRole{}
- req := dto.SysRoleUpdateRoleApiReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, nil, binding.JSON).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- req.SetUpdateBy(user.GetUserId(c))
- cb := sdk.Runtime.GetCasbinKey(config.ApplicationConfig.Host)
- err = s.UpdateRoleApi(&req, cb)
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- _, err = global.LoadPolicy(c)
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, "更新失败,"+err.Error())
- return
- }
- e.OK(req.GetId(), "更新角色路由权限成功")
- }
- // GetRoleApi 获取角色路由权限
- // @Summary 获取角色路由权限
- // @Description 获取角色路由权限
- // @Tags 角色
- // @Accept application/json
- // @Product application/json
- // @Param id path string true "角色Id"
- // @Param service_id query int true "服务id"
- // @Success 200 {object} response.Response{data=dto.SysRoleGetRoleApiListResp} "{"code": 200, "data": [...]}"
- // @Router /api/role-api/{id} [get]
- // @Security Bearer
- func (e SysRoleController) GetRoleApi(c *gin.Context) {
- s := service.SysRole{}
- req := dto.SysRoleGetRoleApiListReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, nil, binding.Form).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- var apiList []model.ServApi
- var apiIds []int
- err = s.GetRoleApiList(&req, &apiIds, &apiList)
- if err != nil {
- e.Error(500, err, err.Error())
- return
- }
- e.OK(dto.SysRoleGetRoleApiListResp{
- ApiList: apiList,
- ApiIds: apiIds,
- }, "查询成功")
- }
- // UpdateRoleMenu 修改角色菜单权限
- // @Summary 修改角色菜单权限
- // @Description 修改角色菜单权限
- // @Tags 角色
- // @Accept application/json
- // @Product application/json
- // @Param id path string true "角色Id"
- // @Param data body dto.SysRoleUpdateRoleMenuReq true "body"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/role-menu/{id} [put]
- // @Security Bearer
- func (e SysRoleController) UpdateRoleMenu(c *gin.Context) {
- s := service.SysRole{}
- req := dto.SysRoleUpdateRoleMenuReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, nil, binding.JSON).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- req.SetUpdateBy(user.GetUserId(c))
- err = s.UpdateRoleMenu(&req)
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- e.OK(req.GetId(), "更新角色路由权限成功")
- }
- // GetRoleMenu 修改角色菜单权限
- // @Summary 修改角色菜单权限
- // @Description 修改角色菜单权限
- // @Tags 角色
- // @Accept application/json
- // @Product application/json
- // @Param id path string true "角色Id"
- // @Success 200 {object} response.Response{data=dto.SysRoleGetRoleMenuListResp} "{"code": 200, "data": [...]}"
- // @Router /api/role-menu/{id} [get]
- // @Security Bearer
- func (e SysRoleController) GetRoleMenu(c *gin.Context) {
- req := dto.SysRoleGetRoleMenuListReq{}
- m := service.SysMenu{}
- r := service.SysRole{}
- err := e.MakeContext(c).
- MakeOrm().
- MakeService(&m.Service).
- MakeService(&r.Service).
- Bind(&req, nil).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- result, err := m.SetLabel()
- if err != nil {
- e.Error(500, err, err.Error())
- return
- }
- menuIds := make([]int, 0)
- if req.Id != 0 {
- menuIds, err = r.GetRoleMenuId(req.Id)
- if err != nil {
- e.Error(500, err, "")
- return
- }
- }
- e.OK(gin.H{
- "menus": result,
- "checkedKeys": menuIds,
- }, "获取成功")
- }
|