sys_role.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. oldRoleName := item.RoleName
  110. oldRoleCodeType := item.RoleTypeCode
  111. roleTypeCode := services.GetRoleTypeCode(req.RoleType)
  112. err = system.ModifySysRole(req.RoleName, req.RoleType, roleTypeCode, req.RoleId)
  113. if err != nil {
  114. br.Msg = "修改失败"
  115. br.ErrMsg = "修改失败,Err:" + err.Error()
  116. return
  117. }
  118. if oldRoleName != req.RoleName || oldRoleCodeType != roleTypeCode {
  119. // 查询所有和角色相关的管理员的信息,并更新成最新的角色
  120. err = system.UpdateAdminRoleInfoByRoleId(req.RoleId, req.RoleName, roleTypeCode)
  121. if err != nil {
  122. br.Msg = "操作失败"
  123. br.ErrMsg = "更新管理员角色信息失败, Err: " + err.Error()
  124. return
  125. }
  126. }
  127. // 同步角色缓存
  128. var syncData system.SyncRoleData
  129. syncData.Source = utils.SOURCE_CRM_FLAG
  130. syncData.RoleId = item.RoleId
  131. _ = utils.Rc.LPush(utils.CACHE_SYNC_ROLE, syncData)
  132. br.Ret = 200
  133. br.Success = true
  134. br.IsAddLog = true
  135. br.Msg = "修改成功"
  136. }
  137. // @Title 删除角色
  138. // @Description 删除角色接口
  139. // @Param request body system.SysRoleDeleteReq true "type json string"
  140. // @Success 200 删除成功
  141. // @router /role/delete [post]
  142. func (this *SysRoleController) Delete() {
  143. br := new(models.BaseResponse).Init()
  144. defer func() {
  145. this.Data["json"] = br
  146. this.ServeJSON()
  147. }()
  148. var req system.SysRoleDeleteReq
  149. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  150. if err != nil {
  151. br.Msg = "参数解析异常!"
  152. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  153. return
  154. }
  155. if req.RoleId <= 0 {
  156. br.Msg = "参数错误"
  157. br.ErrMsg = "参数错误,GroupId 小于等于0 "
  158. return
  159. }
  160. role, e := system.GetSysRoleById(req.RoleId)
  161. if e != nil {
  162. if err.Error() == utils.ErrNoRow() {
  163. br.Msg = "角色不存在, 请刷新页面"
  164. return
  165. }
  166. br.Msg = "删除失败"
  167. br.ErrMsg = "获取角色信息失败, Err: " + e.Error()
  168. return
  169. }
  170. err = system.DeleteSysRole(req.RoleId)
  171. if err != nil {
  172. br.Msg = "删除失败"
  173. br.ErrMsg = "删除失败,Err:" + err.Error()
  174. return
  175. }
  176. // 同步角色缓存
  177. var syncData system.SyncRoleData
  178. syncData.Source = utils.SOURCE_CRM_FLAG
  179. syncData.RoleId = role.RoleId
  180. _ = utils.Rc.LPush(utils.CACHE_SYNC_ROLE, syncData)
  181. br.Ret = 200
  182. br.Success = true
  183. br.IsAddLog = true
  184. br.Msg = "删除成功"
  185. }
  186. // @Title 获取角色列表
  187. // @Description 获取角色列表接口
  188. // @Param PageSize query int true "每页数据条数"
  189. // @Param CurrentIndex query int true "当前页页码,从1开始"
  190. // @Param KeyWord query string true "搜索关键词"
  191. // @Success 200 {object} system.SysRoleListResp
  192. // @router /role/list [get]
  193. func (this *SysRoleController) ListSysRole() {
  194. br := new(models.BaseResponse).Init()
  195. defer func() {
  196. this.Data["json"] = br
  197. this.ServeJSON()
  198. }()
  199. sysUser := this.SysUser
  200. if sysUser == nil {
  201. br.Msg = "请登录"
  202. br.ErrMsg = "请登录,SysUser Is Empty"
  203. return
  204. }
  205. pageSize, _ := this.GetInt("PageSize")
  206. currentIndex, _ := this.GetInt("CurrentIndex")
  207. keyWord := this.GetString("KeyWord")
  208. var startSize int
  209. if pageSize <= 0 {
  210. pageSize = utils.PageSize20
  211. }
  212. if currentIndex <= 0 {
  213. currentIndex = 1
  214. }
  215. startSize = utils.StartIndex(currentIndex, pageSize)
  216. var condition string
  217. var pars []interface{}
  218. if keyWord != "" {
  219. condition += ` AND role_name LIKE '%` + keyWord + `%' `
  220. }
  221. total, err := system.GetSysRoleListCount(condition, pars)
  222. if err != nil {
  223. br.Msg = "获取失败"
  224. br.ErrMsg = "获取失败,Err:" + err.Error()
  225. return
  226. }
  227. list, err := system.GetSysRoleList(condition, pars, startSize, pageSize)
  228. if err != nil {
  229. br.Msg = "获取失败"
  230. br.ErrMsg = "获取失败,Err:" + err.Error()
  231. return
  232. }
  233. page := paging.GetPaging(currentIndex, pageSize, total)
  234. resp := new(system.SysRoleListResp)
  235. resp.List = list
  236. resp.Paging = page
  237. br.Ret = 200
  238. br.Success = true
  239. br.Msg = "获取成功"
  240. br.Data = resp
  241. }
  242. // @Title 角色设置权限-菜单列表
  243. // @Description 角色设置权限-菜单列表
  244. // @Param RoleId query int true "角色Id"
  245. // @Success 200 {object} system.SysRoleListResp
  246. // @router /role/menu/list [get]
  247. func (this *SysRoleController) SysRoleMenusList() {
  248. br := new(models.BaseResponse).Init()
  249. defer func() {
  250. this.Data["json"] = br
  251. this.ServeJSON()
  252. }()
  253. roleId, err := this.GetInt("RoleId")
  254. if err != nil {
  255. br.Msg = "获取角色失败"
  256. br.ErrMsg = "获取角色失败,Err:" + err.Error()
  257. return
  258. }
  259. fmt.Println("roleId", roleId)
  260. list := make([]*system.RoleMenuList, 0)
  261. sysUser := this.SysUser
  262. if sysUser == nil {
  263. br.Msg = "请登录"
  264. br.ErrMsg = "请登录,SysUser Is Empty"
  265. return
  266. }
  267. list, err = system.GetRoleMenuList()
  268. if err != nil {
  269. br.Msg = "获取失败"
  270. br.ErrMsg = "获取失败,Err:" + err.Error()
  271. return
  272. }
  273. lenList := len(list)
  274. for i := 0; i < lenList; i++ {
  275. item := list[i]
  276. child, err := system.GetRoleMenuByParentId(item.MenuId)
  277. if err != nil {
  278. br.Msg = "获取失败"
  279. br.ErrMsg = "获取子菜单数据失败,Err:" + err.Error()
  280. return
  281. }
  282. list[i].Child = child
  283. checkList := make([]int, 0)
  284. if roleId > 0 {
  285. checkItem, err := system.GetCheckListRoleMenu(roleId, item.MenuId)
  286. if err != nil {
  287. br.Msg = "获取失败"
  288. br.ErrMsg = "获取选择数据失败,Err:" + err.Error()
  289. return
  290. }
  291. for _, v := range checkItem {
  292. checkList = append(checkList, v.MenuId)
  293. }
  294. }
  295. list[i].CheckList = checkList
  296. }
  297. resp := new(system.RoleMenuListResp)
  298. resp.List = list
  299. br.Ret = 200
  300. br.Success = true
  301. br.Msg = "获取成功"
  302. br.Data = resp
  303. }
  304. // @Title 角色设置权限
  305. // @Description 角色设置权限
  306. // @Param request body system.RoleMenusAddReq true "type json string"
  307. // @Success 200 {object} system.SysRoleListResp
  308. // @router /role/menu/add [post]
  309. func (this *SysRoleController) SysRoleMenusAdd() {
  310. br := new(models.BaseResponse).Init()
  311. defer func() {
  312. this.Data["json"] = br
  313. this.ServeJSON()
  314. }()
  315. var req system.RoleMenusAddReq
  316. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  317. if err != nil {
  318. br.Msg = "参数解析异常!"
  319. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  320. return
  321. }
  322. roleId := req.RoleId
  323. if roleId <= 0 {
  324. br.Msg = "参数错误"
  325. br.ErrMsg = "参数错误,RoleId 小于等于0 "
  326. return
  327. }
  328. if req.MenuIdStr == "" {
  329. br.Msg = "请选择菜单"
  330. br.ErrMsg = "请选择菜单,MenuIdStr IS Empty "
  331. return
  332. }
  333. sysUser := this.SysUser
  334. if sysUser == nil {
  335. br.Msg = "请登录"
  336. br.ErrMsg = "请登录,SysUser Is Empty"
  337. return
  338. }
  339. err = system.DeleteRoleMenuByRoleId(roleId)
  340. if err != nil {
  341. br.Msg = "设置失败"
  342. br.ErrMsg = "清空原有设置失败,Err:" + err.Error()
  343. return
  344. }
  345. menuArr := strings.Split(req.MenuIdStr, ",")
  346. for _, v := range menuArr {
  347. menuId, err := strconv.Atoi(v)
  348. if err != nil {
  349. br.Msg = "设置失败"
  350. br.ErrMsg = "menuId 解析失败,Err:" + err.Error()
  351. return
  352. }
  353. //item := new(system.SysRoleMenu)
  354. item := new(system.SysRoleMenuCrm)
  355. item.RoleId = roleId
  356. item.MenuId = menuId
  357. _, err = system.AddSysRoleMenuCrm(item)
  358. if err != nil {
  359. br.Msg = "设置失败"
  360. br.ErrMsg = "新增设置失败,Err:" + err.Error()
  361. return
  362. }
  363. }
  364. if req.MenuButtonIdStr != "" {
  365. err = system.DeleteSysRoleButton(roleId)
  366. if err != nil {
  367. br.Msg = "设置失败"
  368. br.ErrMsg = "删除已设置数据失败,Err:" + err.Error()
  369. return
  370. }
  371. buttonMap := make(map[string]string)
  372. buttonArr := strings.Split(req.MenuButtonIdStr, ",")
  373. for _, v := range buttonArr {
  374. if _, ok := buttonMap[v]; !ok {
  375. buttonId, err := strconv.Atoi(v)
  376. count, err := system.GetSysRoleButtonCount(roleId, buttonId)
  377. if err != nil {
  378. br.Msg = "判断设置失败存在失败"
  379. br.ErrMsg = "判断设置失败存在失败,Err:" + err.Error()
  380. return
  381. }
  382. if count <= 0 {
  383. button := new(system.SysRoleButton)
  384. button.RoleId = roleId
  385. button.MenuButtonId = buttonId
  386. button.CreateTime = time.Now()
  387. _, err = system.AddSysRoleButton(button)
  388. if err != nil {
  389. br.Msg = "设置失败"
  390. br.ErrMsg = "按钮权限设置失败,Err:" + err.Error()
  391. return
  392. }
  393. }
  394. }
  395. buttonMap[v] = v
  396. }
  397. } else {
  398. err = system.DeleteSysRoleButton(roleId)
  399. if err != nil {
  400. br.Msg = "设置失败"
  401. br.ErrMsg = "删除已设置数据失败,Err:" + err.Error()
  402. return
  403. }
  404. }
  405. br.Ret = 200
  406. br.Success = true
  407. br.IsAddLog = true
  408. br.Msg = "设置成功"
  409. }
  410. // @Title 获取所有角色
  411. // @Description 获取所有角色
  412. // @Success 200 {object} system.SysRoleListResp
  413. // @router /role/all [get]
  414. func (this *SysRoleController) AllSysRole() {
  415. br := new(models.BaseResponse).Init()
  416. defer func() {
  417. this.Data["json"] = br
  418. this.ServeJSON()
  419. }()
  420. sysUser := this.SysUser
  421. if sysUser == nil {
  422. br.Msg = "请登录"
  423. br.ErrMsg = "请登录,SysUser Is Empty"
  424. return
  425. }
  426. levelFlag := false
  427. roleLevel := this.GetString("RoleLevel")
  428. if roleLevel != "" {
  429. levelFlag = true
  430. }
  431. list, err := system.GetSysRoleAll(levelFlag)
  432. if err != nil {
  433. br.Msg = "获取失败"
  434. br.ErrMsg = "获取失败,Err:" + err.Error()
  435. return
  436. }
  437. resp := new(system.SysRoleAllResp)
  438. resp.List = list
  439. br.Ret = 200
  440. br.Success = true
  441. br.Msg = "获取成功"
  442. br.Data = resp
  443. }