sys_role.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm_ht/models"
  5. "eta/eta_mini_crm_ht/models/request"
  6. "eta/eta_mini_crm_ht/models/response"
  7. "eta/eta_mini_crm_ht/services"
  8. "eta/eta_mini_crm_ht/utils"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. "time"
  11. "github.com/beego/beego/v2/client/orm"
  12. )
  13. type SysRoleController struct {
  14. BaseAuthController
  15. }
  16. // List
  17. // @Title 系统角色列表
  18. // @Description 系统角色列表
  19. // @Param request body UserLoginReq true "type json string"
  20. // @Success 200 {object} response.SysRoleListResp
  21. // @router /list [get]
  22. func (this *SysRoleController) List() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. pageSize, _ := this.GetInt("PageSize")
  29. currentIndex, _ := this.GetInt("CurrentIndex")
  30. var startSize int
  31. if pageSize <= 0 {
  32. pageSize = utils.PageSize20
  33. }
  34. if currentIndex <= 0 {
  35. currentIndex = 1
  36. }
  37. startSize = utils.StartIndex(currentIndex, pageSize)
  38. total, err := models.GetSysRoleListCount()
  39. if err != nil {
  40. br.Msg = "获取失败"
  41. br.ErrMsg = "获取失败,Err:" + err.Error()
  42. return
  43. }
  44. list, err := models.GetSysRoleList(startSize, pageSize)
  45. if err != nil {
  46. br.Msg = "获取失败"
  47. br.ErrMsg = "获取失败,Err:" + err.Error()
  48. return
  49. }
  50. page := paging.GetPaging(currentIndex, pageSize, total)
  51. resp := new(response.SysRoleListResp)
  52. resp.List = list
  53. resp.Paging = page
  54. br.Ret = 200
  55. br.Success = true
  56. br.Msg = "获取成功"
  57. br.Data = resp
  58. }
  59. // AllList
  60. // @Title 系统所有角色列表
  61. // @Description 系统所有角色列表
  62. // @Success 200 {object} response.SysRoleListResp
  63. // @router /allList [get]
  64. func (this *SysRoleController) AllList() {
  65. br := new(models.BaseResponse).Init()
  66. defer func() {
  67. this.Data["json"] = br
  68. this.ServeJSON()
  69. }()
  70. list, err := models.GetSysRoleListNoPage()
  71. if err != nil {
  72. br.Msg = "获取失败"
  73. br.ErrMsg = "获取失败,Err:" + err.Error()
  74. return
  75. }
  76. resp := new(response.SysRoleListResp)
  77. resp.List = list
  78. br.Ret = 200
  79. br.Success = true
  80. br.Msg = "获取成功"
  81. br.Data = resp
  82. }
  83. // @Title 删除角色
  84. // @Description 删除角色接口
  85. // @Param request body system.SysRoleDeleteReq true "type json string"
  86. // @Success 200 删除成功
  87. // @router /delete [post]
  88. func (this *SysRoleController) Delete() {
  89. br := new(models.BaseResponse).Init()
  90. defer func() {
  91. this.Data["json"] = br
  92. this.ServeJSON()
  93. }()
  94. var req request.SysRoleDeleteReq
  95. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  96. if err != nil {
  97. br.Msg = "参数解析异常!"
  98. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  99. return
  100. }
  101. if req.SysRoleId <= 0 {
  102. br.Msg = "参数错误"
  103. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  104. return
  105. }
  106. role, err := models.GetSysRoleById(req.SysRoleId)
  107. if err != nil {
  108. if err.Error() == utils.ErrNoRow() {
  109. br.Msg = "角色不存在, 请刷新页面"
  110. return
  111. }
  112. br.Msg = "删除失败"
  113. br.ErrMsg = "获取角色信息失败, Err: " + err.Error()
  114. return
  115. }
  116. if role.SysRoleName == "admin" {
  117. br.Msg = "admin角色不可删除"
  118. return
  119. }
  120. count, err := models.GetSysUserCountByRoleId(req.SysRoleId)
  121. if err != nil {
  122. br.Msg = "删除失败"
  123. br.ErrMsg = "获取用户数量失败,Err: " + err.Error()
  124. return
  125. }
  126. if count > 0 {
  127. br.Msg = "角色绑定用户,不可删除"
  128. return
  129. }
  130. err = models.DeleteSysRoleById(req.SysRoleId)
  131. if err != nil {
  132. br.Msg = "删除失败"
  133. br.Msg = "删除角色失败,系统错误,Err: " + err.Error()
  134. return
  135. }
  136. br.Msg = "删除成功"
  137. br.Ret = 200
  138. br.Success = true
  139. }
  140. // @Title 新增角色
  141. // @Description 新增角色接口
  142. // @Param request body request.SysRoleAddReq true "type json string"
  143. // @Success 200 新增成功
  144. // @router /add [post]
  145. func (this *SysRoleController) Add() {
  146. br := new(models.BaseResponse).Init()
  147. defer func() {
  148. this.Data["json"] = br
  149. this.ServeJSON()
  150. }()
  151. var req request.SysRoleAddReq
  152. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  153. if err != nil {
  154. br.Msg = "参数解析异常!"
  155. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  156. return
  157. }
  158. if req.SysRoleName == "" {
  159. br.Msg = "角色名称不能为空"
  160. return
  161. }
  162. count, err := models.GetSysRoleCountByRoleName(req.SysRoleName)
  163. if err != nil {
  164. br.Msg = "新增角色失败"
  165. br.ErrMsg = "新增角色失败,系统异常,Err:" + err.Error()
  166. return
  167. }
  168. if count > 0 {
  169. br.Msg = "角色已存在,请重新输入"
  170. return
  171. }
  172. sysRole := &models.SysRole{}
  173. sysRole.SysRoleName = req.SysRoleName
  174. sysRole.CreateTime = time.Now()
  175. sysRole.ModifyTime = time.Now()
  176. err = sysRole.Add()
  177. if err != nil {
  178. br.Msg = "新增角色失败"
  179. br.ErrMsg = "新增角色失败,系统异常,Err:" + err.Error()
  180. return
  181. }
  182. br.Ret = 200
  183. br.Msg = "新增角色成功"
  184. br.Success = true
  185. }
  186. // @Title 新增角色
  187. // @Description 新增角色接口
  188. // @Param request body request.SysRoleAddReq true "type json string"
  189. // @Success 200 新增成功
  190. // @router /edit [post]
  191. func (this *SysRoleController) Edit() {
  192. br := new(models.BaseResponse).Init()
  193. defer func() {
  194. this.Data["json"] = br
  195. this.ServeJSON()
  196. }()
  197. var req request.SysRoleEditReq
  198. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  199. if err != nil {
  200. br.Msg = "参数解析异常!"
  201. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  202. return
  203. }
  204. if req.SysRoleId <= 0 {
  205. br.Msg = "参数错误"
  206. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  207. return
  208. }
  209. if req.SysRoleName == "" {
  210. br.Msg = "分组名称不能为空"
  211. return
  212. }
  213. sysRole, err := models.GetSysRoleById(req.SysRoleId)
  214. if err != nil {
  215. if err.Error() == utils.ErrNoRow() {
  216. br.Msg = "角色不存在,请刷新页面"
  217. return
  218. }
  219. br.Msg = "操作失败"
  220. br.ErrMsg = "获取角色失败, Err:" + err.Error()
  221. return
  222. }
  223. if sysRole.SysRoleName == "admin" {
  224. br.Msg = "admin角色不可编辑"
  225. return
  226. }
  227. oldSysRole, err := models.GetSysRoleByRoleName(req.SysRoleName)
  228. if err != nil && err != orm.ErrNoRows {
  229. br.Msg = "操作失败"
  230. br.ErrMsg = "获取角色失败, Err:" + err.Error()
  231. return
  232. }
  233. if oldSysRole != nil && oldSysRole.SysRoleId != req.SysRoleId {
  234. br.Msg = "名称已存在,请重新输入"
  235. return
  236. }
  237. err = models.UpdateSysUserRoleByRoleId(req.SysRoleId, req.SysRoleName)
  238. if err != nil {
  239. br.Msg = "编辑失败"
  240. br.ErrMsg = "编辑角色失败,Err:" + err.Error()
  241. return
  242. }
  243. br.Ret = 200
  244. br.Success = true
  245. br.Msg = "编辑成功"
  246. }
  247. // ButtonList
  248. // @Title 角色-按钮权限列表
  249. // @Description 角色-按钮权限列表
  250. // @Param RoleId query int true "角色Id"
  251. // @Success 200 {object} system.SysRoleListResp
  252. // @router /menu/buttons [get]
  253. func (this *SysRoleController) ButtonList() {
  254. br := new(models.BaseResponse).Init()
  255. defer func() {
  256. if br.ErrMsg == "" {
  257. br.IsSendEmail = false
  258. }
  259. this.Data["json"] = br
  260. this.ServeJSON()
  261. }()
  262. sysUser := this.SysUser
  263. if sysUser == nil {
  264. br.Msg = "请登录"
  265. br.ErrMsg = "请登录,SysUser Is Empty"
  266. br.Ret = 408
  267. return
  268. }
  269. roleId := sysUser.SysRoleId
  270. list, e := models.GetMenuButtonsByRoleId(roleId)
  271. if e != nil {
  272. br.Msg = "获取失败"
  273. br.ErrMsg = "获取角色按钮权限失败, Err: " + e.Error()
  274. return
  275. }
  276. buttonList := make([]*response.SysMenuButtonResp, 0)
  277. for _, v := range list {
  278. // 把菜单过滤
  279. if v.MenuType == 1 {
  280. continue
  281. }
  282. tmp := new(response.SysMenuButtonResp)
  283. tmp.SysMenuId = v.SysMenuId
  284. tmp.ParentId = v.ParentId
  285. tmp.Name = v.Name
  286. tmp.MenuType = v.MenuType
  287. tmp.ButtonCode = v.ButtonCode
  288. buttonList = append(buttonList, tmp)
  289. }
  290. br.Data = buttonList
  291. br.Ret = 200
  292. br.Success = true
  293. br.Msg = "获取成功"
  294. }
  295. // @Title 获取用户权限菜单
  296. // @Description 获取用户权限菜单接口
  297. // @Success 200 {object} system.MenuListResp
  298. // @router /menu/list [get]
  299. func (this *SysRoleController) SysMenuList() {
  300. br := new(models.BaseResponse).Init()
  301. defer func() {
  302. this.Data["json"] = br
  303. this.ServeJSON()
  304. }()
  305. sysUser := this.SysUser
  306. if sysUser == nil {
  307. br.Msg = "请登录"
  308. br.ErrMsg = "请登录,SysUser Is Empty"
  309. return
  310. }
  311. roleId := sysUser.SysRoleId
  312. if roleId <= 0 {
  313. br.Msg = "参数错误"
  314. return
  315. }
  316. list, err := models.GetMenuListByRoleIds(roleId)
  317. if err != nil {
  318. br.Msg = "获取失败"
  319. br.ErrMsg = "获取失败,Err:" + err.Error()
  320. return
  321. }
  322. // items := make([]*models.SysMenuItem, 0)
  323. // for _, v := range list {
  324. // t := &models.SysMenuItem{
  325. // SysMenuId: v.SysMenuId,
  326. // ParentId: v.ParentId,
  327. // Name: v.Name,
  328. // Level: v.Level,
  329. // Sort: v.Sort,
  330. // Path: v.Path,
  331. // IconPath: v.IconPath,
  332. // ButtonCode: v.ButtonCode,
  333. // Children: make([]*models.SysMenuItem, 0),
  334. // }
  335. // items = append(items, t)
  336. // }
  337. // 递归返回树形结构
  338. list = services.GetMenuTreeRecursive(list, 0)
  339. br.Ret = 200
  340. br.Success = true
  341. br.Msg = "获取成功"
  342. br.Data = list
  343. }
  344. // SysRoleMenuAuthSave
  345. // @Title 角色设置权限-保存
  346. // @Description 角色设置权限-保存
  347. // @Param request body RoleMenusSaveReq true "type json string"
  348. // @Success 200 {object} system.SysRoleListResp
  349. // @router /menu/auth_save [post]
  350. func (this *SysRoleController) SysRoleMenuAuthSave() {
  351. br := new(models.BaseResponse).Init()
  352. defer func() {
  353. if br.ErrMsg == "" {
  354. br.IsSendEmail = false
  355. }
  356. this.Data["json"] = br
  357. this.ServeJSON()
  358. }()
  359. var req request.RoleMenusSaveReq
  360. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  361. if err != nil {
  362. br.Msg = "参数解析异常!"
  363. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  364. return
  365. }
  366. if req.SysRoleId <= 0 {
  367. br.Msg = "参数有误"
  368. return
  369. }
  370. if len(req.SysMenuIds) == 0 {
  371. br.Msg = "请选择菜单"
  372. return
  373. }
  374. sysRole, err := models.GetSysRoleById(req.SysRoleId)
  375. if err == nil {
  376. if sysRole.SysRoleName == "admin" {
  377. br.Msg = "不允许修改admin权限"
  378. return
  379. }
  380. }
  381. halfMap := make(map[int]struct{})
  382. for _, v := range req.HalfMenuIds {
  383. halfMap[v] = struct{}{}
  384. }
  385. items := make([]*models.SysRoleMenuMapping, 0)
  386. for _, v := range req.SysMenuIds {
  387. if _, ok := halfMap[v]; ok {
  388. continue
  389. }
  390. t := new(models.SysRoleMenuMapping)
  391. t.SysMenuId = v
  392. t.SysRoleId = req.SysRoleId
  393. items = append(items, t)
  394. }
  395. for _, v := range req.HalfMenuIds {
  396. t := new(models.SysRoleMenuMapping)
  397. t.SysMenuId = v
  398. t.Type = 1
  399. t.SysRoleId = req.SysRoleId
  400. items = append(items, t)
  401. }
  402. if e := models.CreateMultiSysRoleMenu(req.SysRoleId, items); e != nil {
  403. br.Msg = "保存失败"
  404. br.ErrMsg = "保存角色菜单权限失败, Err: " + e.Error()
  405. return
  406. }
  407. br.Ret = 200
  408. br.Success = true
  409. br.Msg = "设置成功"
  410. }
  411. // SysRoleMenuAuthList
  412. // @Title 角色设置权限-菜单列表
  413. // @Description 角色设置权限-菜单列表
  414. // @Param RoleId query int true "角色Id"
  415. // @Success 200 {object} system.SysRoleListResp
  416. // @router /menu/auth_list [get]
  417. func (this *SysRoleController) SysRoleMenuAuthList() {
  418. br := new(models.BaseResponse).Init()
  419. defer func() {
  420. this.Data["json"] = br
  421. this.ServeJSON()
  422. }()
  423. resp := new(response.SysMenuListResp)
  424. resp.ChoiceList = make([]int, 0)
  425. resp.HalfChoiceList = make([]int, 0)
  426. resp.List = make([]*models.SysMenuItem, 0)
  427. // 角色勾选的权限
  428. roleId, _ := this.GetInt("SysRoleId", 0)
  429. if roleId > 0 {
  430. relates, e := models.GetSysRoleMenuByRoleId(roleId)
  431. if e != nil {
  432. br.Msg = "获取失败"
  433. br.ErrMsg = "获取角色关联菜单失败, Err: " + e.Error()
  434. return
  435. }
  436. for _, r := range relates {
  437. if r.Type == 1 {
  438. resp.HalfChoiceList = append(resp.HalfChoiceList, r.SysMenuId)
  439. continue
  440. }
  441. resp.ChoiceList = append(resp.ChoiceList, r.SysMenuId)
  442. }
  443. }
  444. order := `sort ASC, create_time DESC, sys_menu_id DESC`
  445. list, e := models.GetSysMenuItemsByCondition(``, make([]interface{}, 0), []string{}, order)
  446. if e != nil {
  447. br.Msg = "获取失败"
  448. br.ErrMsg = "获取菜单列表失败, Err: " + e.Error()
  449. return
  450. }
  451. items := make([]*models.SysMenuItem, 0)
  452. for _, v := range list {
  453. t := &models.SysMenuItem{
  454. SysMenuId: v.SysMenuId,
  455. ParentId: v.ParentId,
  456. Name: v.Name,
  457. Level: v.Level,
  458. Sort: v.Sort,
  459. Path: v.Path,
  460. IconPath: v.IconPath,
  461. ButtonCode: v.ButtonCode,
  462. Children: make([]*models.SysMenuItem, 0),
  463. }
  464. items = append(items, t)
  465. }
  466. // 递归返回树形结构
  467. items = services.GetMenuTreeRecursive(items, 0)
  468. resp.List = items
  469. br.Data = resp
  470. br.Ret = 200
  471. br.Success = true
  472. br.Msg = "获取成功"
  473. }