role.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**********************************************
  2. ** @Des: This file ...
  3. ** @Author: haodaquan
  4. ** @Date: 2017-09-14 14:23:32
  5. ** @Last Modified by: haodaquan
  6. ** @Last Modified time: 2017-09-17 11:31:13
  7. ***********************************************/
  8. package backgroundc
  9. import (
  10. "fmt"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/astaxie/beego"
  15. "wuyebaoxiuapi/models/backgroundm"
  16. )
  17. type RoleController struct {
  18. BaseController
  19. }
  20. func (self *RoleController) List() {
  21. self.Data["pageTitle"] = "角色管理"
  22. self.display()
  23. }
  24. func (self *RoleController) Add() {
  25. self.Data["zTree"] = true //引入ztreecss
  26. self.Data["pageTitle"] = "新增角色"
  27. self.display()
  28. }
  29. func (self *RoleController) Edit() {
  30. self.Data["zTree"] = true //引入ztreecss
  31. self.Data["pageTitle"] = "编辑角色"
  32. id, _ := self.GetInt("id", 0)
  33. role, _ := backgroundm.RoleGetById(id)
  34. row := make(map[string]interface{})
  35. row["id"] = role.Id
  36. row["role_name"] = role.RoleName
  37. row["detail"] = role.Detail
  38. self.Data["role"] = row
  39. //获取选择的树节点
  40. roleAuth, _ := backgroundm.RoleAuthGetById(id)
  41. authId := make([]int, 0)
  42. for _, v := range roleAuth {
  43. authId = append(authId, v.AuthId)
  44. }
  45. self.Data["auth"] = authId
  46. fmt.Println(authId)
  47. self.display()
  48. }
  49. func (self *RoleController) AjaxSave() {
  50. role := new(backgroundm.Role)
  51. role.RoleName = strings.TrimSpace(self.GetString("role_name"))
  52. role.Detail = strings.TrimSpace(self.GetString("detail"))
  53. role.CreateTime = time.Now().Unix()
  54. role.UpdateTime = time.Now().Unix()
  55. role.Status = 1
  56. auths := strings.TrimSpace(self.GetString("nodes_data"))
  57. role_id, _ := self.GetInt("id")
  58. if role_id == 0 {
  59. //新增
  60. role.CreateTime = time.Now().Unix()
  61. role.UpdateTime = time.Now().Unix()
  62. role.CreateId = self.userId
  63. role.UpdateId = self.userId
  64. if id, err := backgroundm.RoleAdd(role); err != nil {
  65. self.ajaxMsg(err.Error(), MSG_ERR)
  66. } else {
  67. ra := new(backgroundm.RoleAuth)
  68. authsSlice := strings.Split(auths, ",")
  69. for _, v := range authsSlice {
  70. aid, _ := strconv.Atoi(v)
  71. ra.AuthId = aid
  72. ra.RoleId = id
  73. backgroundm.RoleAuthAdd(ra)
  74. }
  75. }
  76. self.ajaxMsg("", MSG_OK)
  77. }
  78. //修改
  79. role.Id = role_id
  80. role.UpdateTime = time.Now().Unix()
  81. role.UpdateId = self.userId
  82. if err := role.Update(); err != nil {
  83. self.ajaxMsg(err.Error(), MSG_ERR)
  84. } else {
  85. // 删除该角色权限
  86. backgroundm.RoleAuthDelete(role_id)
  87. ra := new(backgroundm.RoleAuth)
  88. authsSlice := strings.Split(auths, ",")
  89. for _, v := range authsSlice {
  90. aid, _ := strconv.Atoi(v)
  91. ra.AuthId = aid
  92. ra.RoleId = int64(role_id)
  93. backgroundm.RoleAuthAdd(ra)
  94. }
  95. }
  96. self.ajaxMsg("", MSG_OK)
  97. }
  98. func (self *RoleController) AjaxDel() {
  99. role_id, _ := self.GetInt("id")
  100. role, _ := backgroundm.RoleGetById(role_id)
  101. role.Status = 0
  102. role.Id = role_id
  103. role.UpdateTime = time.Now().Unix()
  104. if err := role.Update(); err != nil {
  105. self.ajaxMsg(err.Error(), MSG_ERR)
  106. }
  107. // 删除该角色权限
  108. //models.RoleAuthDelete(role_id)
  109. self.ajaxMsg("", MSG_OK)
  110. }
  111. func (self *RoleController) Table() {
  112. //列表
  113. page, err := self.GetInt("page")
  114. if err != nil {
  115. page = 1
  116. }
  117. limit, err := self.GetInt("limit")
  118. if err != nil {
  119. limit = 30
  120. }
  121. roleName := strings.TrimSpace(self.GetString("roleName"))
  122. self.pageSize = limit
  123. //查询条件
  124. filters := make([]interface{}, 0)
  125. filters = append(filters, "status", 1)
  126. if roleName != "" {
  127. filters = append(filters, "role_name__icontains", roleName)
  128. }
  129. result, count := backgroundm.RoleGetList(page, self.pageSize, filters...)
  130. list := make([]map[string]interface{}, len(result))
  131. for k, v := range result {
  132. row := make(map[string]interface{})
  133. row["id"] = v.Id
  134. row["role_name"] = v.RoleName
  135. row["detail"] = v.Detail
  136. row["create_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s")
  137. row["update_time"] = beego.Date(time.Unix(v.UpdateTime, 0), "Y-m-d H:i:s")
  138. list[k] = row
  139. }
  140. self.ajaxList("成功", MSG_OK, count, list)
  141. }