package admin import ( "github.com/druidcaesa/gotool" "strconv" "time" "ulink-admin/frame" "ulink-admin/modules/system/models/model" "ulink-admin/modules/system/models/req" service "ulink-admin/modules/system/service" "ulink-admin/pkg/excels" "ulink-admin/pkg/jwt/admin" "ulink-admin/pkg/page" ) type RoleApi struct { UserService *service.UserService `inject:""` MenuService *service.MenuService `inject:""` RoleService *service.RoleService `inject:""` } // Find 分页查询角色数据 // @Summary 分页查询角色数据接口 // @Description 分页查询角色数据接口 // @Tags 角色相关接口 // @Accept application/json // @Produce application/json // @Param Authorization header string false "Bearer 令牌" // @Param object query req.RoleQuery false "查询参数" // @Security ApiKeyAuth // @Success 200 {object} resp.Response{data=page.Page{list=model.SysRole},msg=string} "分页获取角色列表,返回包括列表,总数,页码,每页数量" // @Router /role/findList [get] func (a RoleApi) Find(c *frame.Context) { query := &req.RoleQuery{} c.ValidteError(c.ShouldBind(query), query) list := make([]model.SysRole, 0) i := a.RoleService.Page(query, &list) c.Ok(page.Page{ List: list, Total: i, Size: query.PageSize, }) } // ListAll 查询操作日志记录所有数据 // @Summary 分页查询角色数据接口 // @Description 分页查询角色数据接口 // @Tags 角色相关接口 // @Accept application/json // @Produce application/json // @Param Authorization header string false "Bearer 令牌" // @Security ApiKeyAuth // @Success 200 {object} resp.Response{data=model.SysRole,msg=string} "分页获取角色列表,返回包括列表,总数,页码,每页数量" // @Router /role/findAll [get] func (a RoleApi) ListAll(c *frame.Context) { list := make([]model.SysRole, 0) a.RoleService.List(&req.RoleQuery{}, &list) c.Ok(list) } // GetRoleId 根据人roleId查询角色数据 // @Summary 角色详情查询接口 // @Description 角色详情查询接口 // @Tags 角色相关接口 // @Accept application/json // @Produce application/json // @Param Authorization header string false "Bearer 令牌" // @Param id path int true "id" id // @Security ApiKeyAuth // @Success 200 {object} resp.Response{data=model.SysRole,msg=string} "返回角色详情查询" // @Router /role [get] func (a RoleApi) GetRoleId(c *frame.Context) { param := c.Param("id") id, err := strconv.ParseInt(param, 10, 64) if err == nil { role := new(model.SysRole) a.RoleService.GetById(id, role) if role == nil { c.Error("角色查询异常'") return } menuList := *a.MenuService.SelectMenuListByRoleId(role.Id) if menuList == nil { c.Error("菜单查询异常") return } //根据角色查询菜单 role.MenuIds = menuList c.Ok(role) } else { c.Error("参数绑定异常") } } // Add 添加角色业务操作 // @Summary 新增角色接口 // @Description 新增角色接口 // @Tags 角色相关接口 // @Accept application/json // @Produce application/json // @Param Authorization header string false "Bearer 令牌" // @Param data body model.SysRole true "角色实体对象" // @Success 200 {object} resp.Response{msg=string} "操作状态" // @Router /role/add [post] func (a RoleApi) Add(c *frame.Context) { role := &model.SysRole{DelFlag: "0"} c.ValidteError(c.ShouldBind(role), role) role.CreateBy = admin.GetUserInfo(c).UserName role.UpdateBy = role.CreateBy a.RoleService.Add(role) } // Edit 修改角色接口 // @Summary 修改角色接口 // @Description 新增角色接口 // @Tags 角色相关接口 // @Accept application/json // @Produce application/json // @Param Authorization header string false "Bearer 令牌" // @Param data body model.SysRole true "角色实体对象" // @Success 200 {object} resp.Response{msg=string} "操作状态" // @Router /role/edit [put] func (a RoleApi) Edit(c *frame.Context) { role := &model.SysRole{} c.ValidteError(c.ShouldBind(role), role) role.UpdateBy = admin.GetUserInfo(c).UserName a.RoleService.Update(role) } // Delete 删除角色 // @Summary 删除角色接口 // @Description 删除角色接口 // @Tags 角色相关接口 // @Accept application/json // @Produce application/json // @Param Authorization header string false "Bearer 令牌" // @Param id path int true "id" id // @Success 200 {object} resp.Response{msg=string} "操作状态" // @Router /role/remove [delete] func (a RoleApi) Delete(c *frame.Context) { param := c.Param("id") id, err := strconv.ParseInt(param, 10, 64) if err != nil { c.Error("参数绑定异常") return } a.RoleService.Delete(id) } // ChangeStatus 状态修改 // ChangeStatus 修改状态 // @Summary 修改状态接口 // @Description 修改状态接口 // @Tags 角色相关接口 // @Accept application/json // @Produce application/json // @Param Authorization header string false "Bearer 令牌" // @Param id query int true "id" id // @Param status query string true "status" status // @Success 200 {object} resp.Response{msg=string} "操作状态" // @Router /role/changeStatus [put] func (a RoleApi) ChangeStatus(c *frame.Context) { var req struct { Id int64 `form:"ids" binding:"required" msg:"id不存在"` //ids } c.ValidteError(c.ShouldBind(&req), req) body := new(model.SysRole) body.Id = req.Id allowed, s := a.RoleService.CheckRoleAllowed(body.Id) if !allowed { c.Error(s) } body.UpdateTime = time.Now() body.UpdateBy = admin.GetUserInfo(c).UserName a.RoleService.UpdateStatus(body) } // AllocatedList 查询已分配角色角色列表 // @Summary 查询已分配角色角色列表 // @Description 查询已分配角色角色列表 // @Tags 角色相关接口 // @Accept application/json // @Produce application/json // @Param Authorization header string false "Bearer 令牌" // @Param object query req.UserQuery false "查询参数" // @Security ApiKeyAuth // @Success 200 {object} resp.Response{data=page.Page{list=model.SysRole},msg=string} "分页获取角色列表,返回包括列表,总数,页码,每页数量" // @Router /role/authUser/allocatedList [get] func (a RoleApi) AllocatedList(c *frame.Context) { query := &req.UserQuery{} c.ValidteError(c.ShouldBind(query), query) list, i := a.UserService.GetAllocatedList(query) c.Ok(page.Page{ List: list, Total: i, }) } // UnallocatedList 查询未分配角色角色列表 // @Summary 查询已分配角色角色列表 // @Description 查询已分配角色角色列表 // @Tags 角色相关接口 // @Accept application/json // @Produce application/json // @Param Authorization header string false "Bearer 令牌" // @Param object query req.UserQuery false "查询参数" // @Security ApiKeyAuth // @Success 200 {object} resp.Response{data=page.Page{list=model.SysRole},msg=string} "分页获取角色列表,返回包括列表,总数,页码,每页数量" // @Router /role/authUser/unallocatedList [get] func (a RoleApi) UnallocatedList(c *frame.Context) { query := &req.UserQuery{} c.ValidteError(c.ShouldBind(query), query) list, i := a.UserService.GetUnallocatedList(query) c.Ok(page.Page{ List: list, Total: i, }) } // CancelAuthUser 取消授权角色 // @Summary 取消授权角色 // @Description 取消授权角色 // @Tags 角色相关接口 // @Accept application/json // @Produce application/json // @Param Authorization header string false "Bearer 令牌" // @Param data body model.SysRole true "角色实体对象" // @Success 200 {object} resp.Response{msg=string} "操作状态" // @Router /role/authUser/cancel [put] func (a RoleApi) CancelAuthUser(c *frame.Context) { roleUser := &model.SysUserRole{} c.ValidteError(c.ShouldBind(roleUser), roleUser) a.RoleService.DeleteAuthUser(roleUser) } // BatchCancelAuthUsers 批量取消角色授权 // CancelAuthUser 批量取消角色授权 // @Summary 批量取消角色授权 // @Description 取消授权角色 // @Tags 角色相关接口 // @Accept application/json // @Produce application/json // @Param Authorization header string false "Bearer 令牌" // @Param data body model.SysRole true "角色实体对象" // @Success 200 {object} resp.Response{msg=string} "操作状态" // @Router /role/authUser/cancelAll [put] func (a RoleApi) BatchCancelAuthUsers(c *frame.Context) { body := &req.UserRoleBody{} c.ValidteError(c.ShouldBind(body), body) a.RoleService.BatchCancelAuthUsers(body) } // UpdateAuthUserAll 批量选择角色授权 // @Summary 批量选择角色授权 // @Description 批量选择角色授权 // @Tags 角色相关接口 // @Accept application/json // @Produce application/json // @Param Authorization header string false "Bearer 令牌" // @Param data body model.SysRole true "角色实体对象" // @Success 200 {object} resp.Response{msg=string} "操作状态" // @Router /role/authUser/selectAll [put] func (a RoleApi) UpdateAuthUserAll(c *frame.Context) { body := &req.UserRoleBody{} c.ValidteError(c.ShouldBind(body), body) a.RoleService.InsertAuthUsers(body) } // Export 导出Excel func (a RoleApi) Export(c *frame.Context) { query := &req.RoleQuery{} items := make([]interface{}, 0) c.ValidteError(c.ShouldBind(query), query) list := make([]model.SysRole, 0) a.RoleService.List(query, list) for _, role := range list { items = append(items, role) } _, file := excels.ExportExcel(items, "角色表") c.Header("Content-Type", "application/octet-stream") c.Header("Content-Disposition", "attachment; filename="+gotool.IdUtils.IdUUIDToRan(false)+".xlsx") c.Header("Content-Transfer-Encoding", "binary") c.Header("FileName", gotool.IdUtils.IdUUIDToRan(false)+".xlsx") file.Write(c.Writer) }