authority.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 "@/components/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. this.getTableData();
  189. }
  190. })
  191. .catch(() => {
  192. this.$message({
  193. type: "info",
  194. message: "已取消删除"
  195. });
  196. });
  197. },
  198. // 初始化表单
  199. initForm() {
  200. if (this.$refs.authorityForm) {
  201. this.$refs.authorityForm.resetFields();
  202. }
  203. this.form = {
  204. authorityId: "",
  205. authorityName: "",
  206. parentId: "0"
  207. };
  208. },
  209. // 关闭窗口
  210. closeDialog() {
  211. this.initForm();
  212. this.dialogFormVisible = false;
  213. this.apiDialogFlag = false;
  214. },
  215. // 确定弹窗
  216. async enterDialog() {
  217. if (this.form.authorityId == "0") {
  218. this.$message({
  219. type: "error",
  220. message: "角色id不能为0"
  221. });
  222. return false;
  223. }
  224. this.$refs.authorityForm.validate(async valid => {
  225. if (valid) {
  226. switch (this.dialogType) {
  227. case "add":
  228. {
  229. const res = await createAuthority(this.form);
  230. if (res.code == 0) {
  231. this.$message({
  232. type: "success",
  233. message: "添加成功!"
  234. });
  235. this.getTableData();
  236. this.closeDialog();
  237. }
  238. }
  239. break;
  240. case "edit":
  241. {
  242. const res = await updateAuthority(this.form);
  243. if (res.code == 0) {
  244. this.$message({
  245. type: "success",
  246. message: "添加成功!"
  247. });
  248. this.getTableData();
  249. this.closeDialog();
  250. }
  251. }
  252. break;
  253. case "copy": {
  254. const data = {
  255. authority: {
  256. authorityId: "string",
  257. authorityName: "string",
  258. datauthorityId: [],
  259. parentId: "string"
  260. },
  261. oldAuthorityId: 0
  262. };
  263. data.authority.authorityId = this.form.authorityId;
  264. data.authority.authorityName = this.form.authorityName;
  265. data.authority.parentId = this.form.parentId;
  266. data.authority.dataAuthorityId = this.copyForm.dataAuthorityId;
  267. data.oldAuthorityId = this.copyForm.authorityId;
  268. const res = await copyAuthority(data);
  269. if (res.code == 0) {
  270. this.$message({
  271. type: "success",
  272. message: "复制成功!"
  273. });
  274. this.getTableData();
  275. }
  276. }
  277. }
  278. this.initForm();
  279. this.dialogFormVisible = false;
  280. }
  281. });
  282. },
  283. setOptions() {
  284. this.AuthorityOption = [
  285. {
  286. authorityId: "0",
  287. authorityName: "根角色"
  288. }
  289. ];
  290. this.setAuthorityOptions(this.tableData, this.AuthorityOption, false);
  291. },
  292. setAuthorityOptions(AuthorityData, optionsData, disabled) {
  293. this.form.authorityId = String(this.form.authorityId);
  294. AuthorityData &&
  295. AuthorityData.map(item => {
  296. if (item.children && item.children.length) {
  297. const option = {
  298. authorityId: item.authorityId,
  299. authorityName: item.authorityName,
  300. disabled: disabled || item.authorityId == this.form.authorityId,
  301. children: []
  302. };
  303. this.setAuthorityOptions(
  304. item.children,
  305. option.children,
  306. disabled || item.authorityId == this.form.authorityId
  307. );
  308. optionsData.push(option);
  309. } else {
  310. const option = {
  311. authorityId: item.authorityId,
  312. authorityName: item.authorityName,
  313. disabled: disabled || item.authorityId == this.form.authorityId
  314. };
  315. optionsData.push(option);
  316. }
  317. });
  318. },
  319. // 增加角色
  320. addAuthority(parentId) {
  321. this.initForm();
  322. this.dialogTitle = "新增角色";
  323. this.dialogType = "add";
  324. this.form.parentId = parentId;
  325. this.setOptions();
  326. this.dialogFormVisible = true;
  327. },
  328. // 编辑角色
  329. editAuthority(row) {
  330. this.setOptions();
  331. this.dialogTitle = "编辑角色";
  332. this.dialogType = "edit";
  333. for (let key in this.form) {
  334. this.form[key] = row[key];
  335. }
  336. this.setOptions();
  337. this.dialogFormVisible = true;
  338. }
  339. },
  340. async created() {
  341. this.pageSize = 999;
  342. await this.getTableData();
  343. }
  344. };
  345. </script>
  346. <style lang="scss">
  347. .authority {
  348. .el-input-number {
  349. margin-left: 15px;
  350. span {
  351. display: none;
  352. }
  353. }
  354. .button-box {
  355. padding: 10px 20px;
  356. .el-button {
  357. float: right;
  358. }
  359. }
  360. }
  361. .role-box {
  362. .el-tabs__content {
  363. height: calc(100vh - 150px);
  364. overflow: auto;
  365. }
  366. }
  367. </style>