123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- package system
- import (
- "errors"
- "fmt"
- "go-nc/configs/global"
- "go-nc/internal/utils"
- "go-nc/model"
- "github.com/gin-gonic/gin"
- "github.com/jinzhu/copier"
- )
- // 获取角色
- func GetRole(c *gin.Context) {
- type RoleInfo struct {
- model.Sys_role
- Children []SysMenuType `json:"children"`
- Users []uint `json:"users"`
- }
- var data []RoleInfo
- var roleList []model.Sys_role
- global.App.DB.Find(&roleList)
- copier.Copy(&data, &roleList)
- // 清洗数据
- for i, v := range data {
- // 查询菜单关系
- var rMenuList []model.Sys_role_menu
- global.App.DB.Where("role_id = ?", v.Id).Find(&rMenuList)
- if len(rMenuList) != 0 {
- // 清洗出菜单ID
- var menuIds []uint
- for _, v := range rMenuList {
- menuIds = append(menuIds, v.MenuId)
- }
- // 查询菜单
- var menuList []model.Sys_menu
- global.App.DB.Where("id in ?", menuIds).Find(&menuList)
- for i := range menuList {
- menuList[i].Icon = ""
- }
- var router []SysMenuType
- copier.Copy(&router, &menuList)
- // 清洗数据称tree
- tree := BuildTree(router)
- data[i].Children = tree
- }
- // 查询用户
- var userList []model.Sys_user_role
- global.App.DB.Where("role_id = ?", v.Id).Find(&userList)
- if len(userList) == 0 {
- data[i].Users = []uint{}
- } else {
- var users []uint
- for _, v := range userList {
- users = append(users, v.UserId)
- }
- data[i].Users = users
- }
- }
- c.Set("res_data", data)
- }
- // 新增角色
- func AddRole(c *gin.Context) {
- type Param struct {
- Name string `json:"name" validate:"required"`
- Menu []uint `json:"checkedKeys" validate:"required"`
- Description string `json:"description" validate:"required"`
- }
- var param Param
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- // 查询角色
- var role model.Sys_role
- role.Name = param.Name
- role.Description = param.Description
- // 事物操作
- tx := global.App.DB.Begin()
- if err := tx.Create(&role).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- // 新增角色菜单
- var roleMenuList []model.Sys_role_menu
- for _, v := range param.Menu {
- roleMenuList = append(roleMenuList, model.Sys_role_menu{
- RoleId: role.Id,
- MenuId: v,
- })
- }
- if err := tx.Create(&roleMenuList).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- tx.Commit()
- c.Set("res_data", "新增成功!")
- }
- // 获取角色下的用户列表
- func GetRoleUser(c *gin.Context) {
- type Param struct {
- Id uint `json:"id" validate:"required"`
- Name string `json:"name"`
- }
- var param Param
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- // 查询角色
- var roleList []model.Sys_user_role
- if err := global.App.DB.Where("role_id = ?", param.Id).Find(&roleList).Error; err != nil {
- c.Error(err)
- return
- }
- // userIds
- var userIds []uint
- for _, v := range roleList {
- userIds = append(userIds, v.UserId)
- }
- fmt.Println(roleList)
- // 查询用户id && name 模糊查询
- var userList []model.Sys_user
- if err := global.App.DB.Where("id in ?", userIds).Find(&userList).Error; err != nil {
- c.Error(err)
- return
- }
- for i := range userList {
- userList[i].Password = ""
- }
- c.Set("res_data", userList)
- }
- // 修改角色
- func UpdateRole(c *gin.Context) {
- type Param struct {
- Id uint `json:"id" validate:"required"`
- Description string `json:"description" validate:"required"`
- MenuId []uint `json:"checkedKeys" validate:"required"`
- }
- var param Param
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- // 更新角色: 事物操作
- tx := global.App.DB.Begin()
- // 更新角色
- role := model.Sys_role{}
- role.Id = param.Id
- role.Description = param.Description
- if err := tx.Updates(&role).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- // 更新角色菜单
- // 删除角色菜单
- if err := tx.Where("role_id = ?", param.Id).Unscoped().Delete(&model.Sys_role_menu{}).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- // 新增角色菜单
- var roleMenu []model.Sys_role_menu
- for _, v := range param.MenuId {
- roleMenu = append(roleMenu, model.Sys_role_menu{
- RoleId: param.Id,
- MenuId: v,
- })
- }
- if err := tx.Create(&roleMenu).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- tx.Commit()
- c.Set("res_data", "修改成功!")
- }
- // 解绑角色
- func UnbindRole(c *gin.Context) {
- type Param struct {
- Id uint `json:"id" validate:"required"`
- UserId uint `json:"userId" validate:"required"`
- }
- var param Param
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- // 删除角色
- role := model.Sys_user_role{}
- role.RoleId = param.Id
- role.UserId = param.UserId
- if err := global.App.DB.Unscoped().Delete(&role).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", "解绑成功!")
- }
- // 删除角色
- func DeleteRole(c *gin.Context) {
- type Param struct {
- Id uint `json:"id" validate:"required"`
- }
- var param Param
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- if param.Id == 1 {
- c.Error(errors.New("管理员角色不可删除!"))
- return
- }
- if param.Id == 2 {
- c.Error(errors.New("客户角色不可删除!"))
- return
- }
- // 角色下有用户不让删除
- var roleList []model.Sys_user_role
- if err := global.App.DB.Where("role_id = ?", param.Id).Find(&roleList).Error; err != nil {
- c.Error(err)
- return
- }
- if len(roleList) > 0 {
- c.Error(errors.New("请先解除该角色下所有用户!"))
- return
- }
- // 事物操作
- tx := global.App.DB.Begin()
- // 根据角色ID删除角色菜单数据RoleId的所有数据
- if err := tx.Where("role_id = ?", param.Id).Unscoped().Delete(&model.Sys_role_menu{}).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- // 删除角色
- if err := tx.Unscoped().Delete(&model.Sys_role{}, param.Id).Error; err != nil {
- c.Error(err)
- return
- }
- tx.Commit()
- c.Set("res_data", "删除成功!")
- }
- // 用户状态修改
- func ChangeUserStatus(c *gin.Context) {
- type Param struct {
- Id uint `json:"id" validate:"required"`
- State string `json:"state" validate:"required"`
- }
- var param Param
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- // 更新用户
- user := model.Sys_user{}
- user.Id = param.Id
- user.State = param.State
- if err := global.App.DB.Updates(&user).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", "修改成功!")
- }
|