role.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <basic-container>
  3. <avue-crud :option="option"
  4. :data="data"
  5. ref="crud"
  6. v-model="form"
  7. :permission="permissionList"
  8. @row-del="rowDel"
  9. @row-update="rowUpdate"
  10. @row-save="rowSave"
  11. @search-change="searchChange"
  12. @search-reset="searchReset"
  13. @selection-change="selectionChange"
  14. @on-load="onLoad">
  15. <template slot="menuLeft">
  16. <el-button type="danger"
  17. size="small"
  18. icon="el-icon-delete"
  19. v-if="permission.role_delete"
  20. plain
  21. @click="handleDelete">删 除
  22. </el-button>
  23. <el-button size="small"
  24. icon="el-icon-delete"
  25. @click="handleRole"
  26. plain>权限设置
  27. </el-button>
  28. </template>
  29. <template slot-scope="{row}"
  30. slot="roleId">
  31. <el-tag>{{row.roleName}}</el-tag>
  32. </template>
  33. <template slot-scope="{row}"
  34. slot="deptId">
  35. <el-tag>{{row.deptName}}</el-tag>
  36. </template>
  37. </avue-crud>
  38. <el-dialog title="提示"
  39. :visible.sync="box"
  40. width="40%">
  41. <el-tree :data="list"
  42. show-checkbox
  43. node-key="id"
  44. ref="tree"
  45. :default-checked-keys="defaultObj"
  46. :props="props">
  47. </el-tree>
  48. <span slot="footer"
  49. class="dialog-footer">
  50. <el-button @click="box = false">取 消</el-button>
  51. <el-button type="primary"
  52. @click="submit">确 定</el-button>
  53. </span>
  54. </el-dialog>
  55. </basic-container>
  56. </template>
  57. <script>
  58. import {
  59. getList,
  60. remove,
  61. update,
  62. add,
  63. grant,
  64. grantTree,
  65. getRole,
  66. getRoleTree
  67. } from "@/api/system/role";
  68. import { mapGetters } from "vuex";
  69. export default {
  70. data() {
  71. return {
  72. form: {},
  73. box: false,
  74. props: {
  75. label: "title",
  76. valie: "key"
  77. },
  78. list: [],
  79. defaultObj: [],
  80. selectionList: [],
  81. page: {
  82. pageSize: 10,
  83. currentPage: 1,
  84. total: 0
  85. },
  86. option: {
  87. tip: false,
  88. tree: true,
  89. border: true,
  90. index: true,
  91. selection: true,
  92. viewBtn: true,
  93. column: [
  94. {
  95. label: "角色名称",
  96. prop: "roleName",
  97. search: true,
  98. rules: [
  99. {
  100. required: true,
  101. message: "请输入角色名称",
  102. trigger: "blur"
  103. }
  104. ]
  105. },
  106. {
  107. label: "角色别名",
  108. prop: "roleAlias",
  109. search: true,
  110. rules: [
  111. {
  112. required: true,
  113. message: "请输入角色别名",
  114. trigger: "blur"
  115. }
  116. ]
  117. },
  118. {
  119. label: "上级角色",
  120. prop: "parentId",
  121. dicData: [],
  122. type: "tree",
  123. hide: true,
  124. props: {
  125. label: "title"
  126. },
  127. rules: [
  128. {
  129. required: false,
  130. message: "请选择上级角色",
  131. trigger: "click"
  132. }
  133. ]
  134. },
  135. {
  136. label: "角色排序",
  137. prop: "sort",
  138. type: "number",
  139. rules: [
  140. {
  141. required: true,
  142. message: "请输入角色排序",
  143. trigger: "blur"
  144. }
  145. ]
  146. }
  147. ]
  148. },
  149. data: []
  150. };
  151. },
  152. computed: {
  153. ...mapGetters(["permission"]),
  154. permissionList() {
  155. return {
  156. addBtn: this.vaildData(this.permission.role_add, false),
  157. viewBtn: this.vaildData(this.permission.role_view, false),
  158. delBtn: this.vaildData(this.permission.role_delete, false),
  159. editBtn: this.vaildData(this.permission.role_edit, false)
  160. };
  161. },
  162. ids() {
  163. let ids = [];
  164. this.selectionList.forEach(ele => {
  165. ids.push(ele.id);
  166. });
  167. return ids.join(",");
  168. }
  169. },
  170. methods: {
  171. submit() {
  172. const menuLIst = this.$refs.tree.getCheckedKeys().join(",");
  173. grant(this.ids[0], menuLIst).then(() => {
  174. this.box = false;
  175. this.$message({
  176. type: "success",
  177. message: "操作成功!"
  178. });
  179. this.onLoad(this.page);
  180. });
  181. },
  182. rowSave(row, loading) {
  183. add(row).then(() => {
  184. loading();
  185. this.onLoad(this.page);
  186. this.$message({
  187. type: "success",
  188. message: "操作成功!"
  189. });
  190. });
  191. },
  192. rowUpdate(row, index, loading) {
  193. update(row).then(() => {
  194. this.onLoad(this.page);
  195. loading();
  196. this.$message({
  197. type: "success",
  198. message: "操作成功!"
  199. });
  200. });
  201. },
  202. rowDel(row) {
  203. this.$confirm("确定将选择数据删除?", {
  204. confirmButtonText: "确定",
  205. cancelButtonText: "取消",
  206. type: "warning"
  207. })
  208. .then(() => {
  209. return remove(row.id);
  210. })
  211. .then(() => {
  212. this.onLoad(this.page);
  213. this.$message({
  214. type: "success",
  215. message: "操作成功!"
  216. });
  217. });
  218. },
  219. searchReset() {
  220. this.onLoad(this.page);
  221. },
  222. searchChange(params) {
  223. this.onLoad(this.page, params);
  224. },
  225. selectionChange(list) {
  226. this.selectionList = list;
  227. },
  228. handleRole() {
  229. if (this.selectionList.length !== 1) {
  230. this.$message.warning("请选择至少一条数据");
  231. return;
  232. }
  233. this.defaultObj = [];
  234. grantTree()
  235. .then(res => {
  236. this.list = res.data.data;
  237. return getRole(this.ids[0]);
  238. })
  239. .then(res => {
  240. this.defaultObj = res.data.data;
  241. this.box = true;
  242. });
  243. },
  244. handleDelete() {
  245. if (this.selectionList.length === 0) {
  246. this.$message.warning("请选择至少一条数据");
  247. return;
  248. }
  249. this.$confirm("确定将选择数据删除?", {
  250. confirmButtonText: "确定",
  251. cancelButtonText: "取消",
  252. type: "warning"
  253. })
  254. .then(() => {
  255. return remove(this.ids);
  256. })
  257. .then(() => {
  258. this.onLoad(this.page);
  259. this.$message({
  260. type: "success",
  261. message: "操作成功!"
  262. });
  263. this.$refs.crud.toggleSelection();
  264. });
  265. },
  266. onLoad(page, params = {}) {
  267. getList(page.currentPage, page.pageSize, params).then(res => {
  268. const data = res.data.data;
  269. this.data = data;
  270. getRoleTree().then(res => {
  271. const data = res.data.data;
  272. const index = this.$refs.crud.findColumnIndex("parentId");
  273. this.option.column[index].dicData = data;
  274. });
  275. });
  276. }
  277. }
  278. };
  279. </script>
  280. <style>
  281. </style>