sys_role.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/system"
  8. "hongze/hz_crm_api/services"
  9. "hongze/hz_crm_api/utils"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. type SysRoleController struct {
  15. BaseAuthController
  16. }
  17. // @Title 新增角色
  18. // @Description 新增角色接口
  19. // @Param request body system.SysRoleAddReq true "type json string"
  20. // @Success 200 新增成功
  21. // @router /role/add [post]
  22. func (this *SysRoleController) Add() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. var req system.SysRoleAddReq
  29. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  30. if err != nil {
  31. br.Msg = "参数解析异常!"
  32. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  33. return
  34. }
  35. if req.RoleName == "" {
  36. br.Msg = "角色名称不能为空"
  37. return
  38. }
  39. count, err := system.GetSysRoleCount(req.RoleName)
  40. if err != nil && err.Error() != utils.ErrNoRow() {
  41. br.Msg = "获取数据失败"
  42. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  43. return
  44. }
  45. if count > 0 {
  46. br.Msg = "角色已存在,请重新输入"
  47. return
  48. }
  49. item := new(system.SysRole)
  50. item.RoleName = req.RoleName
  51. item.RoleType = req.RoleType
  52. item.RoleTypeCode = services.GetRoleTypeCode(req.RoleType)
  53. item.CreateTime = time.Now()
  54. item.ModifyTime = time.Now()
  55. roleId, err := system.AddSysRole(item)
  56. if err != nil {
  57. br.Msg = "新增失败"
  58. br.ErrMsg = "新增失败,Err:" + err.Error()
  59. return
  60. }
  61. // 同步角色缓存
  62. var syncData system.SyncRoleData
  63. syncData.Source = utils.SOURCE_CRM_FLAG
  64. syncData.RoleId = int(roleId)
  65. _ = utils.Rc.LPush(utils.CACHE_SYNC_ROLE, syncData)
  66. br.Ret = 200
  67. br.Success = true
  68. br.Msg = "新增成功"
  69. }
  70. // @Title 修改角色
  71. // @Description 修改角色接口
  72. // @Param request body system.SysRoleEditReq true "type json string"
  73. // @Success 200 修改成功
  74. // @router /role/edit [post]
  75. func (this *SysRoleController) Edit() {
  76. br := new(models.BaseResponse).Init()
  77. defer func() {
  78. this.Data["json"] = br
  79. this.ServeJSON()
  80. }()
  81. var req system.SysRoleEditReq
  82. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  83. if err != nil {
  84. br.Msg = "参数解析异常!"
  85. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  86. return
  87. }
  88. if req.RoleId <= 0 {
  89. br.Msg = "参数错误"
  90. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  91. return
  92. }
  93. if req.RoleName == "" {
  94. br.Msg = "角色名称不能为空"
  95. return
  96. }
  97. item, err := system.GetSysRoleByName(req.RoleName)
  98. if err != nil && err.Error() != utils.ErrNoRow() {
  99. br.Msg = "获取数据失败"
  100. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  101. return
  102. }
  103. if item != nil {
  104. if item.RoleId != req.RoleId {
  105. br.Msg = "名称已存在,请重新输入"
  106. return
  107. }
  108. }
  109. roleTypeCode := services.GetRoleTypeCode(req.RoleType)
  110. err = system.ModifySysRole(req.RoleName, req.RoleType, roleTypeCode, req.RoleId)
  111. if err != nil {
  112. br.Msg = "修改失败"
  113. br.ErrMsg = "修改失败,Err:" + err.Error()
  114. return
  115. }
  116. // 同步角色缓存
  117. var syncData system.SyncRoleData
  118. syncData.Source = utils.SOURCE_CRM_FLAG
  119. syncData.RoleId = item.RoleId
  120. _ = utils.Rc.LPush(utils.CACHE_SYNC_ROLE, syncData)
  121. br.Ret = 200
  122. br.Success = true
  123. br.IsAddLog = true
  124. br.Msg = "修改成功"
  125. }
  126. // @Title 删除角色
  127. // @Description 删除角色接口
  128. // @Param request body system.SysRoleDeleteReq true "type json string"
  129. // @Success 200 删除成功
  130. // @router /role/delete [post]
  131. func (this *SysRoleController) Delete() {
  132. br := new(models.BaseResponse).Init()
  133. defer func() {
  134. this.Data["json"] = br
  135. this.ServeJSON()
  136. }()
  137. var req system.SysRoleDeleteReq
  138. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  139. if err != nil {
  140. br.Msg = "参数解析异常!"
  141. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  142. return
  143. }
  144. if req.RoleId <= 0 {
  145. br.Msg = "参数错误"
  146. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  147. return
  148. }
  149. role, e := system.GetSysRoleById(req.RoleId)
  150. if e != nil {
  151. if err.Error() == utils.ErrNoRow() {
  152. br.Msg = "角色不存在, 请刷新页面"
  153. return
  154. }
  155. br.Msg = "删除失败"
  156. br.ErrMsg = "获取角色信息失败, Err: " + e.Error()
  157. return
  158. }
  159. err = system.DeleteSysRole(req.RoleId)
  160. if err != nil {
  161. br.Msg = "删除失败"
  162. br.ErrMsg = "删除失败,Err:" + err.Error()
  163. return
  164. }
  165. // 同步角色缓存
  166. var syncData system.SyncRoleData
  167. syncData.Source = utils.SOURCE_CRM_FLAG
  168. syncData.RoleId = role.RoleId
  169. _ = utils.Rc.LPush(utils.CACHE_SYNC_ROLE, syncData)
  170. br.Ret = 200
  171. br.Success = true
  172. br.IsAddLog = true
  173. br.Msg = "删除成功"
  174. }
  175. // @Title 获取角色列表
  176. // @Description 获取角色列表接口
  177. // @Param PageSize query int true "每页数据条数"
  178. // @Param CurrentIndex query int true "当前页页码,从1开始"
  179. // @Param KeyWord query string true "搜索关键词"
  180. // @Success 200 {object} system.SysRoleListResp
  181. // @router /role/list [get]
  182. func (this *SysRoleController) ListSysRole() {
  183. br := new(models.BaseResponse).Init()
  184. defer func() {
  185. this.Data["json"] = br
  186. this.ServeJSON()
  187. }()
  188. sysUser := this.SysUser
  189. if sysUser == nil {
  190. br.Msg = "请登录"
  191. br.ErrMsg = "请登录,SysUser Is Empty"
  192. return
  193. }
  194. pageSize, _ := this.GetInt("PageSize")
  195. currentIndex, _ := this.GetInt("CurrentIndex")
  196. keyWord := this.GetString("KeyWord")
  197. var startSize int
  198. if pageSize <= 0 {
  199. pageSize = utils.PageSize20
  200. }
  201. if currentIndex <= 0 {
  202. currentIndex = 1
  203. }
  204. startSize = utils.StartIndex(currentIndex, pageSize)
  205. var condition string
  206. var pars []interface{}
  207. if keyWord != "" {
  208. condition += ` AND role_name LIKE '%` + keyWord + `%' `
  209. }
  210. total, err := system.GetSysRoleListCount(condition, pars)
  211. if err != nil {
  212. br.Msg = "获取失败"
  213. br.ErrMsg = "获取失败,Err:" + err.Error()
  214. return
  215. }
  216. list, err := system.GetSysRoleList(condition, pars, startSize, pageSize)
  217. if err != nil {
  218. br.Msg = "获取失败"
  219. br.ErrMsg = "获取失败,Err:" + err.Error()
  220. return
  221. }
  222. page := paging.GetPaging(currentIndex, pageSize, total)
  223. resp := new(system.SysRoleListResp)
  224. resp.List = list
  225. resp.Paging = page
  226. br.Ret = 200
  227. br.Success = true
  228. br.Msg = "获取成功"
  229. br.Data = resp
  230. }
  231. // @Title 角色设置权限-菜单列表
  232. // @Description 角色设置权限-菜单列表
  233. // @Param RoleId query int true "角色Id"
  234. // @Success 200 {object} system.SysRoleListResp
  235. // @router /role/menu/list [get]
  236. func (this *SysRoleController) SysRoleMenusList() {
  237. br := new(models.BaseResponse).Init()
  238. defer func() {
  239. this.Data["json"] = br
  240. this.ServeJSON()
  241. }()
  242. roleId, err := this.GetInt("RoleId")
  243. if err != nil {
  244. br.Msg = "获取角色失败"
  245. br.ErrMsg = "获取角色失败,Err:" + err.Error()
  246. return
  247. }
  248. fmt.Println("roleId", roleId)
  249. list := make([]*system.RoleMenuList, 0)
  250. sysUser := this.SysUser
  251. if sysUser == nil {
  252. br.Msg = "请登录"
  253. br.ErrMsg = "请登录,SysUser Is Empty"
  254. return
  255. }
  256. list, err = system.GetRoleMenuList()
  257. if err != nil {
  258. br.Msg = "获取失败"
  259. br.ErrMsg = "获取失败,Err:" + err.Error()
  260. return
  261. }
  262. lenList := len(list)
  263. for i := 0; i < lenList; i++ {
  264. item := list[i]
  265. child, err := system.GetRoleMenuByParentId(item.MenuId)
  266. if err != nil {
  267. br.Msg = "获取失败"
  268. br.ErrMsg = "获取子菜单数据失败,Err:" + err.Error()
  269. return
  270. }
  271. list[i].Child = child
  272. checkList := make([]int, 0)
  273. if roleId > 0 {
  274. checkItem, err := system.GetCheckListRoleMenu(roleId, item.MenuId)
  275. if err != nil {
  276. br.Msg = "获取失败"
  277. br.ErrMsg = "获取选择数据失败,Err:" + err.Error()
  278. return
  279. }
  280. for _, v := range checkItem {
  281. checkList = append(checkList, v.MenuId)
  282. }
  283. }
  284. list[i].CheckList = checkList
  285. }
  286. resp := new(system.RoleMenuListResp)
  287. resp.List = list
  288. br.Ret = 200
  289. br.Success = true
  290. br.Msg = "获取成功"
  291. br.Data = resp
  292. }
  293. // @Title 角色设置权限
  294. // @Description 角色设置权限
  295. // @Param request body system.RoleMenusAddReq true "type json string"
  296. // @Success 200 {object} system.SysRoleListResp
  297. // @router /role/menu/add [post]
  298. func (this *SysRoleController) SysRoleMenusAdd() {
  299. br := new(models.BaseResponse).Init()
  300. defer func() {
  301. this.Data["json"] = br
  302. this.ServeJSON()
  303. }()
  304. var req system.RoleMenusAddReq
  305. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  306. if err != nil {
  307. br.Msg = "参数解析异常!"
  308. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  309. return
  310. }
  311. roleId := req.RoleId
  312. if roleId <= 0 {
  313. br.Msg = "参数错误"
  314. br.ErrMsg = "参数错误,RoleId 小于等于0 "
  315. return
  316. }
  317. if req.MenuIdStr == "" {
  318. br.Msg = "请选择菜单"
  319. br.ErrMsg = "请选择菜单,MenuIdStr IS Empty "
  320. return
  321. }
  322. sysUser := this.SysUser
  323. if sysUser == nil {
  324. br.Msg = "请登录"
  325. br.ErrMsg = "请登录,SysUser Is Empty"
  326. return
  327. }
  328. err = system.DeleteRoleMenuByRoleId(roleId)
  329. if err != nil {
  330. br.Msg = "设置失败"
  331. br.ErrMsg = "清空原有设置失败,Err:" + err.Error()
  332. return
  333. }
  334. menuArr := strings.Split(req.MenuIdStr, ",")
  335. for _, v := range menuArr {
  336. menuId, err := strconv.Atoi(v)
  337. if err != nil {
  338. br.Msg = "设置失败"
  339. br.ErrMsg = "menuId 解析失败,Err:" + err.Error()
  340. return
  341. }
  342. //item := new(system.SysRoleMenu)
  343. item := new(system.SysRoleMenuCrm)
  344. item.RoleId = roleId
  345. item.MenuId = menuId
  346. _, err = system.AddSysRoleMenuCrm(item)
  347. if err != nil {
  348. br.Msg = "设置失败"
  349. br.ErrMsg = "新增设置失败,Err:" + err.Error()
  350. return
  351. }
  352. }
  353. if req.MenuButtonIdStr != "" {
  354. err = system.DeleteSysRoleButton(roleId)
  355. if err != nil {
  356. br.Msg = "设置失败"
  357. br.ErrMsg = "删除已设置数据失败,Err:" + err.Error()
  358. return
  359. }
  360. buttonMap := make(map[string]string)
  361. buttonArr := strings.Split(req.MenuButtonIdStr, ",")
  362. for _, v := range buttonArr {
  363. if _, ok := buttonMap[v]; !ok {
  364. buttonId, err := strconv.Atoi(v)
  365. count, err := system.GetSysRoleButtonCount(roleId, buttonId)
  366. if err != nil {
  367. br.Msg = "判断设置失败存在失败"
  368. br.ErrMsg = "判断设置失败存在失败,Err:" + err.Error()
  369. return
  370. }
  371. if count <= 0 {
  372. button := new(system.SysRoleButton)
  373. button.RoleId = roleId
  374. button.MenuButtonId = buttonId
  375. button.CreateTime = time.Now()
  376. _, err = system.AddSysRoleButton(button)
  377. if err != nil {
  378. br.Msg = "设置失败"
  379. br.ErrMsg = "按钮权限设置失败,Err:" + err.Error()
  380. return
  381. }
  382. }
  383. }
  384. buttonMap[v] = v
  385. }
  386. } else {
  387. err = system.DeleteSysRoleButton(roleId)
  388. if err != nil {
  389. br.Msg = "设置失败"
  390. br.ErrMsg = "删除已设置数据失败,Err:" + err.Error()
  391. return
  392. }
  393. }
  394. br.Ret = 200
  395. br.Success = true
  396. br.IsAddLog = true
  397. br.Msg = "设置成功"
  398. }
  399. // @Title 获取所有角色
  400. // @Description 获取所有角色
  401. // @Success 200 {object} system.SysRoleListResp
  402. // @router /role/all [get]
  403. func (this *SysRoleController) AllSysRole() {
  404. br := new(models.BaseResponse).Init()
  405. defer func() {
  406. this.Data["json"] = br
  407. this.ServeJSON()
  408. }()
  409. sysUser := this.SysUser
  410. if sysUser == nil {
  411. br.Msg = "请登录"
  412. br.ErrMsg = "请登录,SysUser Is Empty"
  413. return
  414. }
  415. levelFlag := false
  416. roleLevel := this.GetString("RoleLevel")
  417. if roleLevel != "" {
  418. levelFlag = true
  419. }
  420. list, err := system.GetSysRoleAll(levelFlag)
  421. if err != nil {
  422. br.Msg = "获取失败"
  423. br.ErrMsg = "获取失败,Err:" + err.Error()
  424. return
  425. }
  426. resp := new(system.SysRoleAllResp)
  427. resp.List = list
  428. br.Ret = 200
  429. br.Success = true
  430. br.Msg = "获取成功"
  431. br.Data = resp
  432. }