role.vue 6.8 KB

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