authority.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <template>
  2. <div class="authority">
  3. <div class="button-box clearflex">
  4. <el-button @click="addAuthority('0')" type="primary">新增角色</el-button>
  5. </div>
  6. <el-table
  7. :data="tableData"
  8. :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
  9. border
  10. row-key="authorityId"
  11. stripe
  12. style="width: 100%"
  13. >
  14. <el-table-column label="角色id" min-width="180" prop="authorityId"></el-table-column>
  15. <el-table-column label="角色名称" min-width="180" prop="authorityName"></el-table-column>
  16. <el-table-column fixed="right" label="操作" width="460">
  17. <template slot-scope="scope">
  18. <el-button @click="opdendrawer(scope.row)" size="small" type="primary">设置权限</el-button>
  19. <el-button
  20. @click="addAuthority(scope.row.authorityId)"
  21. icon="el-icon-plus"
  22. size="small"
  23. type="primary"
  24. >新增子角色</el-button>
  25. <el-button
  26. @click="copyAuthority(scope.row)"
  27. icon="el-icon-copy-document"
  28. size="small"
  29. type="primary"
  30. >拷贝</el-button>
  31. <el-button
  32. @click="editAuthority(scope.row)"
  33. icon="el-icon-edit"
  34. size="small"
  35. type="primary"
  36. >编辑</el-button>
  37. <el-button
  38. @click="deleteAuth(scope.row)"
  39. icon="el-icon-delete"
  40. size="small"
  41. type="danger"
  42. >删除</el-button>
  43. </template>
  44. </el-table-column>
  45. </el-table>
  46. <!-- 新增角色弹窗 -->
  47. <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
  48. <el-form :model="form" :rules="rules" ref="authorityForm">
  49. <el-form-item label="父级角色" prop="parentId">
  50. <el-cascader
  51. :disabled="dialogType=='add'"
  52. :options="AuthorityOption"
  53. :props="{ checkStrictly: true,label:'authorityName',value:'authorityId',disabled:'disabled',emitPath:false}"
  54. :show-all-levels="false"
  55. filterable
  56. v-model="form.parentId"
  57. ></el-cascader>
  58. </el-form-item>
  59. <el-form-item label="角色ID" prop="authorityId">
  60. <el-input :disabled="dialogType=='edit'" autocomplete="off" v-model="form.authorityId"></el-input>
  61. </el-form-item>
  62. <el-form-item label="角色姓名" prop="authorityName">
  63. <el-input autocomplete="off" v-model="form.authorityName"></el-input>
  64. </el-form-item>
  65. </el-form>
  66. <div class="dialog-footer" slot="footer">
  67. <el-button @click="closeDialog">取 消</el-button>
  68. <el-button @click="enterDialog" type="primary">确 定</el-button>
  69. </div>
  70. </el-dialog>
  71. <el-drawer :visible.sync="drawer" :with-header="false" size="40%" title="角色配置" v-if="drawer">
  72. <el-tabs :before-leave="autoEnter" class="role-box" type="border-card">
  73. <el-tab-pane label="角色菜单">
  74. <Menus :row="activeRow" ref="menus" />
  75. </el-tab-pane>
  76. <el-tab-pane label="角色api">
  77. <apis :row="activeRow" ref="apis" />
  78. </el-tab-pane>
  79. <el-tab-pane label="资源权限">
  80. <Datas :authority="tableData" :row="activeRow" ref="datas" />
  81. </el-tab-pane>
  82. </el-tabs>
  83. </el-drawer>
  84. </div>
  85. </template>
  86. <script>
  87. // 获取列表内容封装在mixins内部 getTableData方法 初始化已封装完成
  88. import {
  89. getAuthorityList,
  90. deleteAuthority,
  91. createAuthority,
  92. updateAuthority,
  93. copyAuthority
  94. } from "@/api/authority";
  95. import Menus from "@/view/superAdmin/authority/components/menus";
  96. import Apis from "@/view/superAdmin/authority/components/apis";
  97. import Datas from "@/view/superAdmin/authority/components/datas";
  98. import infoList from "@/mixins/infoList";
  99. export default {
  100. name: "Authority",
  101. mixins: [infoList],
  102. data() {
  103. var mustUint = (rule, value, callback) => {
  104. if (!/^[0-9]*[1-9][0-9]*$/.test(value)) {
  105. return callback(new Error("请输入正整数"));
  106. }
  107. return callback();
  108. };
  109. return {
  110. AuthorityOption: [
  111. {
  112. authorityId: "0",
  113. authorityName: "根角色"
  114. }
  115. ],
  116. listApi: getAuthorityList,
  117. drawer: false,
  118. dialogType: "add",
  119. activeRow: {},
  120. activeUserId: 0,
  121. dialogTitle: "新增角色",
  122. dialogFormVisible: false,
  123. apiDialogFlag: false,
  124. copyForm: {},
  125. form: {
  126. authorityId: "",
  127. authorityName: "",
  128. parentId: "0"
  129. },
  130. rules: {
  131. authorityId: [
  132. { required: true, message: "请输入角色ID", trigger: "blur" },
  133. { validator: mustUint, trigger: "blur" }
  134. ],
  135. authorityName: [
  136. { required: true, message: "请输入角色名", trigger: "blur" }
  137. ],
  138. parentId: [
  139. { required: true, message: "请选择请求方式", trigger: "blur" }
  140. ]
  141. }
  142. };
  143. },
  144. components: {
  145. Menus,
  146. Apis,
  147. Datas
  148. },
  149. methods: {
  150. autoEnter(activeName, oldActiveName) {
  151. const paneArr = ["menus", "apis", "datas"];
  152. if (oldActiveName) {
  153. if (this.$refs[paneArr[oldActiveName]].needConfirm) {
  154. this.$refs[paneArr[oldActiveName]].enterAndNext();
  155. this.$refs[paneArr[oldActiveName]].needConfirm = false;
  156. }
  157. }
  158. },
  159. // 拷贝角色
  160. copyAuthority(row) {
  161. this.setOptions();
  162. this.dialogTitle = "拷贝角色";
  163. this.dialogType = "copy";
  164. for (let k in this.form) {
  165. this.form[k] = row[k];
  166. }
  167. this.copyForm = row;
  168. this.dialogFormVisible = true;
  169. },
  170. opdendrawer(row) {
  171. this.drawer = true;
  172. this.activeRow = row;
  173. },
  174. // 删除角色
  175. deleteAuth(row) {
  176. this.$confirm("此操作将永久删除该角色, 是否继续?", "提示", {
  177. confirmButtonText: "确定",
  178. cancelButtonText: "取消",
  179. type: "warning"
  180. })
  181. .then(async () => {
  182. const res = await deleteAuthority({ authorityId: row.authorityId });
  183. if (res.code == 0) {
  184. this.$message({
  185. type: "success",
  186. message: "删除成功!"
  187. });
  188. if (this.tableData.length == 1) {
  189. this.page--;
  190. }
  191. this.getTableData();
  192. }
  193. })
  194. .catch(() => {
  195. this.$message({
  196. type: "info",
  197. message: "已取消删除"
  198. });
  199. });
  200. },
  201. // 初始化表单
  202. initForm() {
  203. if (this.$refs.authorityForm) {
  204. this.$refs.authorityForm.resetFields();
  205. }
  206. this.form = {
  207. authorityId: "",
  208. authorityName: "",
  209. parentId: "0"
  210. };
  211. },
  212. // 关闭窗口
  213. closeDialog() {
  214. this.initForm();
  215. this.dialogFormVisible = false;
  216. this.apiDialogFlag = false;
  217. },
  218. // 确定弹窗
  219. async enterDialog() {
  220. if (this.form.authorityId == "0") {
  221. this.$message({
  222. type: "error",
  223. message: "角色id不能为0"
  224. });
  225. return false;
  226. }
  227. this.$refs.authorityForm.validate(async valid => {
  228. if (valid) {
  229. switch (this.dialogType) {
  230. case "add":
  231. {
  232. const res = await createAuthority(this.form);
  233. if (res.code == 0) {
  234. this.$message({
  235. type: "success",
  236. message: "添加成功!"
  237. });
  238. this.getTableData();
  239. this.closeDialog();
  240. }
  241. }
  242. break;
  243. case "edit":
  244. {
  245. const res = await updateAuthority(this.form);
  246. if (res.code == 0) {
  247. this.$message({
  248. type: "success",
  249. message: "添加成功!"
  250. });
  251. this.getTableData();
  252. this.closeDialog();
  253. }
  254. }
  255. break;
  256. case "copy": {
  257. const data = {
  258. authority: {
  259. authorityId: "string",
  260. authorityName: "string",
  261. datauthorityId: [],
  262. parentId: "string"
  263. },
  264. oldAuthorityId: 0
  265. };
  266. data.authority.authorityId = this.form.authorityId;
  267. data.authority.authorityName = this.form.authorityName;
  268. data.authority.parentId = this.form.parentId;
  269. data.authority.dataAuthorityId = this.copyForm.dataAuthorityId;
  270. data.oldAuthorityId = this.copyForm.authorityId;
  271. const res = await copyAuthority(data);
  272. if (res.code == 0) {
  273. this.$message({
  274. type: "success",
  275. message: "复制成功!"
  276. });
  277. this.getTableData();
  278. }
  279. }
  280. }
  281. this.initForm();
  282. this.dialogFormVisible = false;
  283. }
  284. });
  285. },
  286. setOptions() {
  287. this.AuthorityOption = [
  288. {
  289. authorityId: "0",
  290. authorityName: "根角色"
  291. }
  292. ];
  293. this.setAuthorityOptions(this.tableData, this.AuthorityOption, false);
  294. },
  295. setAuthorityOptions(AuthorityData, optionsData, disabled) {
  296. this.form.authorityId = String(this.form.authorityId);
  297. AuthorityData &&
  298. AuthorityData.map(item => {
  299. if (item.children && item.children.length) {
  300. const option = {
  301. authorityId: item.authorityId,
  302. authorityName: item.authorityName,
  303. disabled: disabled || item.authorityId == this.form.authorityId,
  304. children: []
  305. };
  306. this.setAuthorityOptions(
  307. item.children,
  308. option.children,
  309. disabled || item.authorityId == this.form.authorityId
  310. );
  311. optionsData.push(option);
  312. } else {
  313. const option = {
  314. authorityId: item.authorityId,
  315. authorityName: item.authorityName,
  316. disabled: disabled || item.authorityId == this.form.authorityId
  317. };
  318. optionsData.push(option);
  319. }
  320. });
  321. },
  322. // 增加角色
  323. addAuthority(parentId) {
  324. this.initForm();
  325. this.dialogTitle = "新增角色";
  326. this.dialogType = "add";
  327. this.form.parentId = parentId;
  328. this.setOptions();
  329. this.dialogFormVisible = true;
  330. },
  331. // 编辑角色
  332. editAuthority(row) {
  333. this.setOptions();
  334. this.dialogTitle = "编辑角色";
  335. this.dialogType = "edit";
  336. for (let key in this.form) {
  337. this.form[key] = row[key];
  338. }
  339. this.setOptions();
  340. this.dialogFormVisible = true;
  341. }
  342. },
  343. async created() {
  344. this.pageSize = 999;
  345. await this.getTableData();
  346. }
  347. };
  348. </script>
  349. <style lang="scss">
  350. .authority {
  351. .el-input-number {
  352. margin-left: 15px;
  353. span {
  354. display: none;
  355. }
  356. }
  357. .button-box {
  358. padding: 10px 20px;
  359. .el-button {
  360. float: right;
  361. }
  362. }
  363. }
  364. .role-box {
  365. .el-tabs__content {
  366. height: calc(100vh - 150px);
  367. overflow: auto;
  368. }
  369. }
  370. </style>