addRole.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <div class="global">
  3. <div class="mainContainer">
  4. <div class="title">{{ title }}角色</div>
  5. <el-form
  6. ref="ruleForm"
  7. :model="roleGroup"
  8. :rules="rules"
  9. label-width="100px"
  10. class="demo-ruleForm"
  11. >
  12. <el-form-item label="角色组名:" prop="rname">
  13. <el-input v-model="roleGroup.rname" />
  14. </el-form-item>
  15. <el-form-item label="备注:">
  16. <el-input v-model="roleGroup.rremark" />
  17. </el-form-item>
  18. </el-form>
  19. <div class="tree">
  20. <div class="treeLabel">功能:</div>
  21. <div class="treeShow">
  22. <el-tree
  23. ref="resourseTree"
  24. :data="data"
  25. show-checkbox
  26. node-key="resourceCode"
  27. :props="defaultProps"
  28. :default-checked-keys="checkedKeysResourceCodes"
  29. />
  30. </div>
  31. </div>
  32. <div>
  33. <el-button type="success" plain @click="cancel()">取消</el-button>
  34. <el-button type="success" @click="btnSave('ruleForm')">保存</el-button>
  35. </div>
  36. </div>
  37. </div>
  38. </template>
  39. <script>
  40. // 中转站
  41. import bus from '@/utils/bus'
  42. // 引入接口
  43. import {
  44. resourcesTree,
  45. getUserInfo,
  46. addUserInfo,
  47. updateUserInfo,
  48. roleIsExit,
  49. checkUserInfo,
  50. editResourcesTree
  51. } from '@/api/system/userManagement/role'
  52. export default {
  53. name: 'AddRole',
  54. data() {
  55. var checkRname = (rule, value, callback) => {
  56. if (!value) {
  57. callback(new Error('请输入角色组'))
  58. } else {
  59. this.rGroupExit((isExit) => {
  60. if (isExit) {
  61. callback(new Error('该角色组已存在'))
  62. } else {
  63. callback()
  64. }
  65. })
  66. }
  67. }
  68. return {
  69. checkedKeysResourceCodes: [],
  70. // 标题
  71. title: '',
  72. // 参数id
  73. id: '',
  74. // 按钮类型
  75. type: '',
  76. // 校验规则
  77. rules: {
  78. rname: [{ required: true, trigger: 'blur', validator: 'checkRname' }]
  79. },
  80. // 初始化数据
  81. roleGroup: {
  82. rname: '',
  83. rremark: ''
  84. },
  85. defaultProps: {
  86. children: 'children',
  87. label: 'resourceName'
  88. // id:'resourceCode'
  89. },
  90. data: []
  91. }
  92. },
  93. mounted() {
  94. var routeParams = this.$route.query
  95. console.log(routeParams, 'routeParams')
  96. this.id = routeParams.id
  97. this.type = routeParams.type
  98. console.log(this.id)
  99. // 对表单进行重置
  100. this.$nextTick(() => {
  101. this.$refs['ruleForm'].resetFields()
  102. })
  103. if (this.type == 2) {
  104. this.title = '新增'
  105. } else if (this.type == 3) {
  106. this.title = '修改'
  107. this.checkList()
  108. this.editTreeInfo()
  109. console.log('//////')
  110. }
  111. this.getTreeInfo()
  112. },
  113. methods: {
  114. // 判断角色组是否存在
  115. rGroupExit(callback) {
  116. var params = {
  117. rNams: this.roleGroup.rname
  118. }
  119. roleIsExit(params).then((res) => {
  120. if (res.code == 200) {
  121. if (res.data > 0) {
  122. callback(true)
  123. this.roleGroup.rname = ''
  124. } else {
  125. callback(false)
  126. }
  127. } else {
  128. callback(false)
  129. }
  130. })
  131. },
  132. // 获取资源树接口
  133. getTreeInfo() {
  134. resourcesTree()
  135. .then((res) => {
  136. if (res.code == 200) {
  137. this.data = res.data
  138. }
  139. })
  140. .catch((err) => {
  141. console.log(err)
  142. })
  143. },
  144. // 修改资源数
  145. editTreeInfo() {
  146. var params = {
  147. id: this.id
  148. }
  149. editResourcesTree(params).then((res) => {
  150. if (res.code == 200) {
  151. if (res.data != null) {
  152. for (let index = 0; index < res.data.length; index++) {
  153. const ele = res.data[index]
  154. console.log(ele, 'ele.....')
  155. this.checkedKeysResourceCodes.push(ele)
  156. }
  157. // this.roleGroup.resourceCodes=this.checkedKeysResourceCodes;
  158. console.log(this.checkedKeysResourceCodes, 'this.checkedKeysResourceCodes')
  159. this.getTreeInfo()
  160. }
  161. }
  162. })
  163. },
  164. // 新增接口
  165. addList() {
  166. var data = this.roleGroup
  167. addUserInfo(data).then((res) => {
  168. console.log(data, '新增data')
  169. if (res.code == 200) {
  170. this.$message.success('新增成功')
  171. this.$router.push({
  172. path: '/systemManagement/user/roleManagement'
  173. })
  174. bus.$emit('getList')
  175. }
  176. })
  177. },
  178. // 修改接口
  179. updateList() {
  180. var data = this.roleGroup
  181. updateUserInfo(data)
  182. .then((res) => {
  183. if (res.code == 200) {
  184. this.$message.success('修改成功')
  185. this.$router.push({
  186. path: '/systemManagement/user/roleManagement'
  187. })
  188. bus.$emit('getList')
  189. }
  190. })
  191. .catch((err) => {
  192. console.log(err)
  193. })
  194. },
  195. // 查看接口
  196. checkList() {
  197. var id = this.id
  198. checkUserInfo(id)
  199. .then((res) => {
  200. if (res.code == 200) {
  201. this.roleGroup = res.data
  202. }
  203. })
  204. .catch((err) => {
  205. console.log(err)
  206. })
  207. },
  208. // // 取消按钮
  209. cancel() {
  210. this.$router.push({
  211. path: '/systemManagement/user/roleManagement'
  212. })
  213. },
  214. // 点保存按钮
  215. btnSave(formName) {
  216. this.$refs[formName].validate((valid) => {
  217. if (valid) {
  218. // this.addList();
  219. console.log(this.type, 'this.type..')
  220. console.log(this.$refs.resourseTree.getCheckedNodes(), '资源树')
  221. var checkedNodes = this.$refs.resourseTree.getCheckedNodes()
  222. var resourceCodes = []
  223. for (let index = 0; index < checkedNodes.length; index++) {
  224. const ele = checkedNodes[index]
  225. resourceCodes.push(ele.resourceCode)
  226. }
  227. console.log(resourceCodes, '......')
  228. resourceCodes = resourceCodes.join(',')
  229. console.log(resourceCodes, '数组')
  230. this.roleGroup.resourceCodes = resourceCodes
  231. console.log(
  232. this.roleGroup.resourceCodes,
  233. 'this. roleGroup.resourceCodes'
  234. )
  235. if (this.type == 2) {
  236. this.addList()
  237. } else if (this.type == 3) {
  238. this.updateList()
  239. }
  240. } else {
  241. console.log('error submit!!')
  242. return false
  243. }
  244. })
  245. }
  246. }
  247. }
  248. </script>
  249. <style lang="scss" scoped>
  250. .title {
  251. margin-bottom: 20px;
  252. }
  253. .tree {
  254. display: flex;
  255. .treeLabel {
  256. width: 120px;
  257. text-align: right;
  258. }
  259. .treeShow {
  260. width: 600px;
  261. height: 400px;
  262. // max-height: 100px;
  263. // overflow-y:scroll ;
  264. margin-left: 20px;
  265. // background:rgb(244, 240, 240);
  266. }
  267. }
  268. </style>
  269. <style>
  270. .el-input {
  271. width: 20%;
  272. }
  273. .el-tree {
  274. background: rgb(245, 243, 243);
  275. }
  276. </style>