sys_role.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_forum_admin/models"
  5. "eta/eta_forum_admin/models/system"
  6. "eta/eta_forum_admin/utils"
  7. "fmt"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "strconv"
  10. "strings"
  11. )
  12. type SysRoleController struct {
  13. BaseAuthController
  14. }
  15. // @Title 获取角色列表
  16. // @Description 获取角色列表接口
  17. // @Param PageSize query int true "每页数据条数"
  18. // @Param CurrentIndex query int true "当前页页码,从1开始"
  19. // @Param KeyWord query string true "搜索关键词"
  20. // @Success 200 {object} system.SysRoleListResp
  21. // @router /role/list [get]
  22. func (this *SysRoleController) ListSysRole() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. sysUser := this.SysUser
  29. if sysUser == nil {
  30. br.Msg = "请登录"
  31. br.ErrMsg = "请登录,SysUser Is Empty"
  32. return
  33. }
  34. pageSize, _ := this.GetInt("PageSize")
  35. currentIndex, _ := this.GetInt("CurrentIndex")
  36. keyWord := this.GetString("KeyWord")
  37. var startSize int
  38. if pageSize <= 0 {
  39. pageSize = utils.PageSize20
  40. }
  41. if currentIndex <= 0 {
  42. currentIndex = 1
  43. }
  44. startSize = utils.StartIndex(currentIndex, pageSize)
  45. var condition string
  46. var pars []interface{}
  47. if keyWord != "" {
  48. condition += ` AND role_name LIKE '%` + keyWord + `%' `
  49. }
  50. total, err := system.GetSysRoleListCount(condition, pars)
  51. if err != nil {
  52. br.Msg = "获取失败"
  53. br.ErrMsg = "获取失败,Err:" + err.Error()
  54. return
  55. }
  56. list, err := system.GetSysRoleList(condition, pars, startSize, pageSize)
  57. if err != nil {
  58. br.Msg = "获取失败"
  59. br.ErrMsg = "获取失败,Err:" + err.Error()
  60. return
  61. }
  62. page := paging.GetPaging(currentIndex, pageSize, total)
  63. resp := new(system.SysRoleListResp)
  64. resp.List = list
  65. resp.Paging = page
  66. br.Ret = 200
  67. br.Success = true
  68. br.Msg = "获取成功"
  69. br.Data = resp
  70. }
  71. // @Title 角色设置权限-菜单列表
  72. // @Description 角色设置权限-菜单列表
  73. // @Param RoleId query int true "角色Id"
  74. // @Success 200 {object} system.SysRoleListResp
  75. // @router /role/menu/list [get]
  76. func (this *SysRoleController) SysRoleMenusList() {
  77. br := new(models.BaseResponse).Init()
  78. defer func() {
  79. this.Data["json"] = br
  80. this.ServeJSON()
  81. }()
  82. roleId, err := this.GetInt("RoleId")
  83. if err != nil {
  84. br.Msg = "获取角色失败"
  85. br.ErrMsg = "获取角色失败,Err:" + err.Error()
  86. return
  87. }
  88. fmt.Println("roleId", roleId)
  89. list := make([]*system.RoleMenuList, 0)
  90. sysUser := this.SysUser
  91. if sysUser == nil {
  92. br.Msg = "请登录"
  93. br.ErrMsg = "请登录,SysUser Is Empty"
  94. return
  95. }
  96. list, err = system.GetRoleMenuList()
  97. if err != nil {
  98. br.Msg = "获取失败"
  99. br.ErrMsg = "获取失败,Err:" + err.Error()
  100. return
  101. }
  102. lenList := len(list)
  103. for i := 0; i < lenList; i++ {
  104. item := list[i]
  105. child, err := system.GetRoleMenuByParentId(item.MenuId)
  106. if err != nil {
  107. br.Msg = "获取失败"
  108. br.ErrMsg = "获取子菜单数据失败,Err:" + err.Error()
  109. return
  110. }
  111. list[i].Child = child
  112. checkList := make([]int, 0)
  113. if roleId > 0 {
  114. checkItem, err := system.GetCheckListRoleMenu(roleId, item.MenuId)
  115. if err != nil {
  116. br.Msg = "获取失败"
  117. br.ErrMsg = "获取选择数据失败,Err:" + err.Error()
  118. return
  119. }
  120. for _, v := range checkItem {
  121. checkList = append(checkList, v.MenuId)
  122. }
  123. }
  124. list[i].CheckList = checkList
  125. }
  126. resp := new(system.RoleMenuListResp)
  127. resp.List = list
  128. br.Ret = 200
  129. br.Success = true
  130. br.Msg = "获取成功"
  131. br.Data = resp
  132. }
  133. // @Title 角色设置权限
  134. // @Description 角色设置权限
  135. // @Param request body system.RoleMenusAddReq true "type json string"
  136. // @Success 200 {object} system.SysRoleListResp
  137. // @router /role/menu/add [post]
  138. func (this *SysRoleController) SysRoleMenusAdd() {
  139. br := new(models.BaseResponse).Init()
  140. defer func() {
  141. this.Data["json"] = br
  142. this.ServeJSON()
  143. }()
  144. var req system.RoleMenusAddReq
  145. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  146. if err != nil {
  147. br.Msg = "参数解析异常!"
  148. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  149. return
  150. }
  151. roleId := req.RoleId
  152. if roleId <= 0 {
  153. br.Msg = "参数错误"
  154. br.ErrMsg = "参数错误,RoleId 小于等于0 "
  155. return
  156. }
  157. if req.MenuIdStr == "" {
  158. br.Msg = "请选择菜单"
  159. br.ErrMsg = "请选择菜单,MenuIdStr IS Empty "
  160. return
  161. }
  162. sysUser := this.SysUser
  163. if sysUser == nil {
  164. br.Msg = "请登录"
  165. br.ErrMsg = "请登录,SysUser Is Empty"
  166. return
  167. }
  168. err = system.DeleteRoleMenuByRoleId(roleId)
  169. if err != nil {
  170. br.Msg = "设置失败"
  171. br.ErrMsg = "清空原有设置失败,Err:" + err.Error()
  172. return
  173. }
  174. menuArr := strings.Split(req.MenuIdStr, ",")
  175. for _, v := range menuArr {
  176. menuId, err := strconv.Atoi(v)
  177. if err != nil {
  178. br.Msg = "设置失败"
  179. br.ErrMsg = "menuId 解析失败,Err:" + err.Error()
  180. return
  181. }
  182. //item := new(system.SysRoleMenu)
  183. item := new(system.SysRoleMenu)
  184. item.RoleId = roleId
  185. item.MenuId = menuId
  186. _, err = system.AddSysRoleMenu(item)
  187. if err != nil {
  188. br.Msg = "设置失败"
  189. br.ErrMsg = "新增设置失败,Err:" + err.Error()
  190. return
  191. }
  192. }
  193. /*if req.MenuButtonIdStr != "" {
  194. err = system.DeleteSysRoleButton(roleId)
  195. if err != nil {
  196. br.Msg = "设置失败"
  197. br.ErrMsg = "删除已设置数据失败,Err:" + err.Error()
  198. return
  199. }
  200. buttonMap := make(map[string]string)
  201. buttonArr := strings.Split(req.MenuButtonIdStr, ",")
  202. for _, v := range buttonArr {
  203. if _, ok := buttonMap[v]; !ok {
  204. buttonId, err := strconv.Atoi(v)
  205. count, err := system.GetSysRoleButtonCount(roleId, buttonId)
  206. if err != nil {
  207. br.Msg = "判断设置失败存在失败"
  208. br.ErrMsg = "判断设置失败存在失败,Err:" + err.Error()
  209. return
  210. }
  211. if count <= 0 {
  212. button := new(system.SysRoleButton)
  213. button.RoleId = roleId
  214. button.MenuButtonId = buttonId
  215. button.CreateTime = time.Now()
  216. _, err = system.AddSysRoleButton(button)
  217. if err != nil {
  218. br.Msg = "设置失败"
  219. br.ErrMsg = "按钮权限设置失败,Err:" + err.Error()
  220. return
  221. }
  222. }
  223. }
  224. buttonMap[v] = v
  225. }
  226. } else {
  227. err = system.DeleteSysRoleButton(roleId)
  228. if err != nil {
  229. br.Msg = "设置失败"
  230. br.ErrMsg = "删除已设置数据失败,Err:" + err.Error()
  231. return
  232. }
  233. }*/
  234. br.Ret = 200
  235. br.Success = true
  236. br.IsAddLog = true
  237. br.Msg = "设置成功"
  238. }
  239. // @Title 获取所有角色
  240. // @Description 获取所有角色
  241. // @Success 200 {object} system.SysRoleListResp
  242. // @router /role/all [get]
  243. func (this *SysRoleController) AllSysRole() {
  244. br := new(models.BaseResponse).Init()
  245. defer func() {
  246. this.Data["json"] = br
  247. this.ServeJSON()
  248. }()
  249. sysUser := this.SysUser
  250. if sysUser == nil {
  251. br.Msg = "请登录"
  252. br.ErrMsg = "请登录,SysUser Is Empty"
  253. return
  254. }
  255. levelFlag := false
  256. roleLevel := this.GetString("RoleLevel")
  257. if roleLevel != "" {
  258. levelFlag = true
  259. }
  260. list, err := system.GetSysRoleAll(levelFlag)
  261. if err != nil {
  262. br.Msg = "获取失败"
  263. br.ErrMsg = "获取失败,Err:" + err.Error()
  264. return
  265. }
  266. resp := new(system.SysRoleAllResp)
  267. resp.List = list
  268. br.Ret = 200
  269. br.Success = true
  270. br.Msg = "获取成功"
  271. br.Data = resp
  272. }
  273. // @Title 获取菜单全部按钮
  274. // @Description 获取菜单全部按钮接口
  275. // @Param RoleId query int true "角色ID"
  276. // @Success 200 {object} system.MenuButtonsResp
  277. // @router /menu/buttons [get]
  278. func (this *SysRoleController) SysMenuButtons() {
  279. br := new(models.BaseResponse).Init()
  280. defer func() {
  281. this.Data["json"] = br
  282. this.ServeJSON()
  283. }()
  284. list, err := system.GetButtonMenu()
  285. if err != nil {
  286. br.Msg = "获取失败"
  287. br.ErrMsg = "获取失败,Err:" + err.Error()
  288. return
  289. }
  290. listLen := len(list)
  291. for i := 0; i < listLen; i++ {
  292. item := list[i]
  293. menus, err := system.GetSysMenuButtonItems(item.MenuId)
  294. if err != nil {
  295. br.Msg = "获取失败"
  296. br.ErrMsg = "获取失败,Err:" + err.Error()
  297. return
  298. }
  299. list[i].List = menus
  300. }
  301. roleId, _ := this.GetInt("RoleId")
  302. checkList := make([]*int, 0)
  303. cl, err := system.GetRoleButtonCheck(roleId)
  304. if err != nil && err.Error() != utils.ErrNoRow() {
  305. br.Msg = "获取失败"
  306. br.ErrMsg = "获取失败,Err:" + err.Error()
  307. return
  308. }
  309. for _, v := range cl {
  310. checkList = append(checkList, &v.MenuButtonId)
  311. }
  312. resp := new(system.MenuButtonsResp)
  313. resp.List = list
  314. resp.CheckList = checkList
  315. br.Ret = 200
  316. br.Success = true
  317. br.Msg = "获取成功"
  318. br.Data = resp
  319. }