index.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. <template>
  2. <div class="app-container">
  3. <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
  4. <el-form-item label="角色名称" prop="roleName">
  5. <el-input
  6. v-model="queryParams.roleName"
  7. placeholder="请输入角色名称"
  8. clearable
  9. style="width: 240px"
  10. @keyup.enter.native="handleQuery"
  11. />
  12. </el-form-item>
  13. <el-form-item label="权限字符" prop="roleKey">
  14. <el-input
  15. v-model="queryParams.roleKey"
  16. placeholder="请输入权限字符"
  17. clearable
  18. style="width: 240px"
  19. @keyup.enter.native="handleQuery"
  20. />
  21. </el-form-item>
  22. <el-form-item label="状态" prop="status">
  23. <el-select
  24. v-model="queryParams.status"
  25. placeholder="角色状态"
  26. clearable
  27. style="width: 240px"
  28. >
  29. <el-option
  30. v-for="dict in dict.type.sys_normal_disable"
  31. :key="dict.value"
  32. :label="dict.label"
  33. :value="dict.value"
  34. />
  35. </el-select>
  36. </el-form-item>
  37. <el-form-item label="创建时间">
  38. <el-date-picker
  39. v-model="dateRange"
  40. style="width: 240px"
  41. value-format="yyyy-MM-dd"
  42. type="daterange"
  43. range-separator="-"
  44. start-placeholder="开始日期"
  45. end-placeholder="结束日期"
  46. ></el-date-picker>
  47. </el-form-item>
  48. <el-form-item>
  49. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  50. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  51. </el-form-item>
  52. </el-form>
  53. <el-row :gutter="10" class="mb8">
  54. <el-col :span="1.5">
  55. <el-button
  56. type="primary"
  57. plain
  58. icon="el-icon-plus"
  59. size="mini"
  60. @click="handleAdd"
  61. v-hasPermi="['system:role:add']"
  62. >新增</el-button>
  63. </el-col>
  64. <el-col :span="1.5">
  65. <el-button
  66. type="success"
  67. plain
  68. icon="el-icon-edit"
  69. size="mini"
  70. :disabled="single"
  71. @click="handleUpdate"
  72. v-hasPermi="['system:role:edit']"
  73. >修改</el-button>
  74. </el-col>
  75. <el-col :span="1.5">
  76. <el-button
  77. type="danger"
  78. plain
  79. icon="el-icon-delete"
  80. size="mini"
  81. :disabled="multiple"
  82. @click="handleDelete"
  83. v-hasPermi="['system:role:remove']"
  84. >删除</el-button>
  85. </el-col>
  86. <el-col :span="1.5">
  87. <el-button
  88. type="warning"
  89. plain
  90. icon="el-icon-download"
  91. size="mini"
  92. @click="handleExport"
  93. v-hasPermi="['system:role:export']"
  94. >导出</el-button>
  95. </el-col>
  96. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  97. </el-row>
  98. <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
  99. <el-table-column type="selection" width="55" align="center" />
  100. <el-table-column label="角色编号" prop="roleId" width="120" />
  101. <el-table-column label="角色名称" prop="roleName" :show-overflow-tooltip="true" width="150" />
  102. <el-table-column label="权限字符" prop="roleKey" :show-overflow-tooltip="true" width="150" />
  103. <el-table-column label="显示顺序" prop="roleSort" width="100" />
  104. <el-table-column label="状态" align="center" width="100">
  105. <template slot-scope="scope">
  106. <el-switch
  107. v-model="scope.row.status"
  108. active-value="0"
  109. inactive-value="1"
  110. @change="handleStatusChange(scope.row)"
  111. ></el-switch>
  112. </template>
  113. </el-table-column>
  114. <el-table-column label="创建时间" align="center" prop="createTime" width="180">
  115. <template slot-scope="scope">
  116. <span>{{ parseTime(scope.row.createTime) }}</span>
  117. </template>
  118. </el-table-column>
  119. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  120. <template slot-scope="scope" v-if="scope.row.roleId !== 1">
  121. <el-button
  122. size="mini"
  123. type="text"
  124. icon="el-icon-edit"
  125. @click="handleUpdate(scope.row)"
  126. v-hasPermi="['system:role:edit']"
  127. >修改</el-button>
  128. <el-button
  129. size="mini"
  130. type="text"
  131. icon="el-icon-delete"
  132. @click="handleDelete(scope.row)"
  133. v-hasPermi="['system:role:remove']"
  134. >删除</el-button>
  135. <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['system:role:edit']">
  136. <span class="el-dropdown-link">
  137. <i class="el-icon-d-arrow-right el-icon--right"></i>更多
  138. </span>
  139. <el-dropdown-menu slot="dropdown">
  140. <el-dropdown-item command="handleDataScope" icon="el-icon-circle-check"
  141. v-hasPermi="['system:role:edit']">数据权限</el-dropdown-item>
  142. <el-dropdown-item command="handleAuthUser" icon="el-icon-user"
  143. v-hasPermi="['system:role:edit']">分配用户</el-dropdown-item>
  144. </el-dropdown-menu>
  145. </el-dropdown>
  146. </template>
  147. </el-table-column>
  148. </el-table>
  149. <pagination
  150. v-show="total>0"
  151. :total="total"
  152. :page.sync="queryParams.pageNum"
  153. :limit.sync="queryParams.pageSize"
  154. @pagination="getList"
  155. />
  156. <!-- 添加或修改角色配置对话框 -->
  157. <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
  158. <el-form ref="form" :model="form" :rules="rules" label-width="100px">
  159. <el-form-item label="角色名称" prop="roleName">
  160. <el-input v-model="form.roleName" placeholder="请输入角色名称" />
  161. </el-form-item>
  162. <el-form-item prop="roleKey">
  163. <span slot="label">
  164. <el-tooltip content="控制器中定义的权限字符,如:@PreAuthorize(`@ss.hasRole('admin')`)" placement="top">
  165. <i class="el-icon-question"></i>
  166. </el-tooltip>
  167. 权限字符
  168. </span>
  169. <el-input v-model="form.roleKey" placeholder="请输入权限字符" />
  170. </el-form-item>
  171. <el-form-item label="角色顺序" prop="roleSort">
  172. <el-input-number v-model="form.roleSort" controls-position="right" :min="0" />
  173. </el-form-item>
  174. <el-form-item label="状态">
  175. <el-radio-group v-model="form.status">
  176. <el-radio
  177. v-for="dict in dict.type.sys_normal_disable"
  178. :key="dict.value"
  179. :label="dict.value"
  180. >{{dict.label}}</el-radio>
  181. </el-radio-group>
  182. </el-form-item>
  183. <el-form-item label="菜单权限">
  184. <el-checkbox v-model="menuExpand" @change="handleCheckedTreeExpand($event, 'menu')">展开/折叠</el-checkbox>
  185. <el-checkbox v-model="menuNodeAll" @change="handleCheckedTreeNodeAll($event, 'menu')">全选/全不选</el-checkbox>
  186. <el-checkbox v-model="form.menuCheckStrictly" @change="handleCheckedTreeConnect($event, 'menu')">父子联动</el-checkbox>
  187. <el-tree
  188. class="tree-border"
  189. :data="menuOptions"
  190. show-checkbox
  191. ref="menu"
  192. node-key="id"
  193. :check-strictly="!form.menuCheckStrictly"
  194. empty-text="加载中,请稍候"
  195. :props="defaultProps"
  196. ></el-tree>
  197. </el-form-item>
  198. <el-form-item label="备注">
  199. <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
  200. </el-form-item>
  201. </el-form>
  202. <div slot="footer" class="dialog-footer">
  203. <el-button type="primary" @click="submitForm">确 定</el-button>
  204. <el-button @click="cancel">取 消</el-button>
  205. </div>
  206. </el-dialog>
  207. <!-- 分配角色数据权限对话框 -->
  208. <el-dialog :title="title" :visible.sync="openDataScope" width="500px" append-to-body>
  209. <el-form :model="form" label-width="80px">
  210. <el-form-item label="角色名称">
  211. <el-input v-model="form.roleName" :disabled="true" />
  212. </el-form-item>
  213. <el-form-item label="权限字符">
  214. <el-input v-model="form.roleKey" :disabled="true" />
  215. </el-form-item>
  216. <el-form-item label="权限范围">
  217. <el-select v-model="form.dataScope" @change="dataScopeSelectChange">
  218. <el-option
  219. v-for="item in dataScopeOptions"
  220. :key="item.value"
  221. :label="item.label"
  222. :value="item.value"
  223. ></el-option>
  224. </el-select>
  225. </el-form-item>
  226. <el-form-item label="数据权限" v-show="form.dataScope == 2">
  227. <el-checkbox v-model="deptExpand" @change="handleCheckedTreeExpand($event, 'dept')">展开/折叠</el-checkbox>
  228. <el-checkbox v-model="deptNodeAll" @change="handleCheckedTreeNodeAll($event, 'dept')">全选/全不选</el-checkbox>
  229. <el-checkbox v-model="form.deptCheckStrictly" @change="handleCheckedTreeConnect($event, 'dept')">父子联动</el-checkbox>
  230. <el-tree
  231. class="tree-border"
  232. :data="deptOptions"
  233. show-checkbox
  234. default-expand-all
  235. ref="dept"
  236. node-key="id"
  237. :check-strictly="!form.deptCheckStrictly"
  238. empty-text="加载中,请稍候"
  239. :props="defaultProps"
  240. ></el-tree>
  241. </el-form-item>
  242. </el-form>
  243. <div slot="footer" class="dialog-footer">
  244. <el-button type="primary" @click="submitDataScope">确 定</el-button>
  245. <el-button @click="cancelDataScope">取 消</el-button>
  246. </div>
  247. </el-dialog>
  248. </div>
  249. </template>
  250. <script>
  251. import { listRole, getRole, delRole, addRole, updateRole, dataScope, changeRoleStatus } from "@/api/system/role";
  252. import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
  253. import { treeselect as deptTreeselect, roleDeptTreeselect } from "@/api/system/dept";
  254. export default {
  255. name: "Role",
  256. dicts: ['sys_normal_disable'],
  257. data() {
  258. return {
  259. // 遮罩层
  260. loading: true,
  261. // 选中数组
  262. ids: [],
  263. // 非单个禁用
  264. single: true,
  265. // 非多个禁用
  266. multiple: true,
  267. // 显示搜索条件
  268. showSearch: true,
  269. // 总条数
  270. total: 0,
  271. // 角色表格数据
  272. roleList: [],
  273. // 弹出层标题
  274. title: "",
  275. // 是否显示弹出层
  276. open: false,
  277. // 是否显示弹出层(数据权限)
  278. openDataScope: false,
  279. menuExpand: false,
  280. menuNodeAll: false,
  281. deptExpand: true,
  282. deptNodeAll: false,
  283. // 日期范围
  284. dateRange: [],
  285. // 数据范围选项
  286. dataScopeOptions: [
  287. {
  288. value: "1",
  289. label: "全部数据权限"
  290. },
  291. {
  292. value: "2",
  293. label: "自定数据权限"
  294. },
  295. {
  296. value: "3",
  297. label: "本部门数据权限"
  298. },
  299. {
  300. value: "4",
  301. label: "本部门及以下数据权限"
  302. },
  303. {
  304. value: "5",
  305. label: "仅本人数据权限"
  306. }
  307. ],
  308. // 菜单列表
  309. menuOptions: [],
  310. // 部门列表
  311. deptOptions: [],
  312. // 查询参数
  313. queryParams: {
  314. pageNum: 1,
  315. pageSize: 10,
  316. roleName: undefined,
  317. roleKey: undefined,
  318. status: undefined
  319. },
  320. // 表单参数
  321. form: {},
  322. defaultProps: {
  323. children: "children",
  324. label: "label"
  325. },
  326. // 表单校验
  327. rules: {
  328. roleName: [
  329. { required: true, message: "角色名称不能为空", trigger: "blur" }
  330. ],
  331. roleKey: [
  332. { required: true, message: "权限字符不能为空", trigger: "blur" }
  333. ],
  334. roleSort: [
  335. { required: true, message: "角色顺序不能为空", trigger: "blur" }
  336. ]
  337. }
  338. };
  339. },
  340. created() {
  341. this.getList();
  342. },
  343. methods: {
  344. /** 查询角色列表 */
  345. getList() {
  346. this.loading = true;
  347. listRole(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
  348. this.roleList = response.rows;
  349. this.total = response.total;
  350. this.loading = false;
  351. }
  352. );
  353. },
  354. /** 查询菜单树结构 */
  355. getMenuTreeselect() {
  356. menuTreeselect().then(response => {
  357. this.menuOptions = response.data;
  358. });
  359. },
  360. /** 查询部门树结构 */
  361. getDeptTreeselect() {
  362. deptTreeselect().then(response => {
  363. this.deptOptions = response.data;
  364. });
  365. },
  366. // 所有菜单节点数据
  367. getMenuAllCheckedKeys() {
  368. // 目前被选中的菜单节点
  369. let checkedKeys = this.$refs.menu.getCheckedKeys();
  370. // 半选中的菜单节点
  371. let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
  372. checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
  373. return checkedKeys;
  374. },
  375. // 所有部门节点数据
  376. getDeptAllCheckedKeys() {
  377. // 目前被选中的部门节点
  378. let checkedKeys = this.$refs.dept.getCheckedKeys();
  379. // 半选中的部门节点
  380. let halfCheckedKeys = this.$refs.dept.getHalfCheckedKeys();
  381. checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
  382. return checkedKeys;
  383. },
  384. /** 根据角色ID查询菜单树结构 */
  385. getRoleMenuTreeselect(roleId) {
  386. return roleMenuTreeselect(roleId).then(response => {
  387. this.menuOptions = response.menus;
  388. return response;
  389. });
  390. },
  391. /** 根据角色ID查询部门树结构 */
  392. getRoleDeptTreeselect(roleId) {
  393. return roleDeptTreeselect(roleId).then(response => {
  394. this.deptOptions = response.depts;
  395. return response;
  396. });
  397. },
  398. // 角色状态修改
  399. handleStatusChange(row) {
  400. let text = row.status === "0" ? "启用" : "停用";
  401. this.$modal.confirm('确认要"' + text + '""' + row.roleName + '"角色吗?').then(function() {
  402. return changeRoleStatus(row.roleId, row.status);
  403. }).then(() => {
  404. this.$modal.msgSuccess(text + "成功");
  405. }).catch(function() {
  406. row.status = row.status === "0" ? "1" : "0";
  407. });
  408. },
  409. // 取消按钮
  410. cancel() {
  411. this.open = false;
  412. this.reset();
  413. },
  414. // 取消按钮(数据权限)
  415. cancelDataScope() {
  416. this.openDataScope = false;
  417. this.reset();
  418. },
  419. // 表单重置
  420. reset() {
  421. if (this.$refs.menu != undefined) {
  422. this.$refs.menu.setCheckedKeys([]);
  423. }
  424. this.menuExpand = false,
  425. this.menuNodeAll = false,
  426. this.deptExpand = true,
  427. this.deptNodeAll = false,
  428. this.form = {
  429. roleId: undefined,
  430. roleName: undefined,
  431. roleKey: undefined,
  432. roleSort: 0,
  433. status: "0",
  434. menuIds: [],
  435. deptIds: [],
  436. menuCheckStrictly: true,
  437. deptCheckStrictly: true,
  438. remark: undefined
  439. };
  440. this.resetForm("form");
  441. },
  442. /** 搜索按钮操作 */
  443. handleQuery() {
  444. this.queryParams.pageNum = 1;
  445. this.getList();
  446. },
  447. /** 重置按钮操作 */
  448. resetQuery() {
  449. this.dateRange = [];
  450. this.resetForm("queryForm");
  451. this.handleQuery();
  452. },
  453. // 多选框选中数据
  454. handleSelectionChange(selection) {
  455. this.ids = selection.map(item => item.roleId)
  456. this.single = selection.length!=1
  457. this.multiple = !selection.length
  458. },
  459. // 更多操作触发
  460. handleCommand(command, row) {
  461. switch (command) {
  462. case "handleDataScope":
  463. this.handleDataScope(row);
  464. break;
  465. case "handleAuthUser":
  466. this.handleAuthUser(row);
  467. break;
  468. default:
  469. break;
  470. }
  471. },
  472. // 树权限(展开/折叠)
  473. handleCheckedTreeExpand(value, type) {
  474. if (type == 'menu') {
  475. let treeList = this.menuOptions;
  476. for (let i = 0; i < treeList.length; i++) {
  477. this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value;
  478. }
  479. } else if (type == 'dept') {
  480. let treeList = this.deptOptions;
  481. for (let i = 0; i < treeList.length; i++) {
  482. this.$refs.dept.store.nodesMap[treeList[i].id].expanded = value;
  483. }
  484. }
  485. },
  486. // 树权限(全选/全不选)
  487. handleCheckedTreeNodeAll(value, type) {
  488. if (type == 'menu') {
  489. this.$refs.menu.setCheckedNodes(value ? this.menuOptions: []);
  490. } else if (type == 'dept') {
  491. this.$refs.dept.setCheckedNodes(value ? this.deptOptions: []);
  492. }
  493. },
  494. // 树权限(父子联动)
  495. handleCheckedTreeConnect(value, type) {
  496. if (type == 'menu') {
  497. this.form.menuCheckStrictly = value ? true: false;
  498. } else if (type == 'dept') {
  499. this.form.deptCheckStrictly = value ? true: false;
  500. }
  501. },
  502. /** 新增按钮操作 */
  503. handleAdd() {
  504. this.reset();
  505. this.getMenuTreeselect();
  506. this.open = true;
  507. this.title = "添加角色";
  508. },
  509. /** 修改按钮操作 */
  510. handleUpdate(row) {
  511. this.reset();
  512. const roleId = row.roleId || this.ids
  513. const roleMenu = this.getRoleMenuTreeselect(roleId);
  514. getRole(roleId).then(response => {
  515. this.form = response.data;
  516. this.open = true;
  517. this.$nextTick(() => {
  518. roleMenu.then(res => {
  519. let checkedKeys = res.checkedKeys
  520. checkedKeys.forEach((v) => {
  521. this.$nextTick(()=>{
  522. this.$refs.menu.setChecked(v, true ,false);
  523. })
  524. })
  525. });
  526. });
  527. this.title = "修改角色";
  528. });
  529. },
  530. /** 选择角色权限范围触发 */
  531. dataScopeSelectChange(value) {
  532. if(value !== '2') {
  533. this.$refs.dept.setCheckedKeys([]);
  534. }
  535. },
  536. /** 分配数据权限操作 */
  537. handleDataScope(row) {
  538. this.reset();
  539. const roleDeptTreeselect = this.getRoleDeptTreeselect(row.roleId);
  540. getRole(row.roleId).then(response => {
  541. this.form = response.data;
  542. this.openDataScope = true;
  543. this.$nextTick(() => {
  544. roleDeptTreeselect.then(res => {
  545. this.$refs.dept.setCheckedKeys(res.checkedKeys);
  546. });
  547. });
  548. this.title = "分配数据权限";
  549. });
  550. },
  551. /** 分配用户操作 */
  552. handleAuthUser: function(row) {
  553. const roleId = row.roleId;
  554. this.$router.push("/system/role-auth/user/" + roleId);
  555. },
  556. /** 提交按钮 */
  557. submitForm: function() {
  558. this.$refs["form"].validate(valid => {
  559. if (valid) {
  560. if (this.form.roleId != undefined) {
  561. this.form.menuIds = this.getMenuAllCheckedKeys();
  562. updateRole(this.form).then(response => {
  563. this.$modal.msgSuccess("修改成功");
  564. this.open = false;
  565. this.getList();
  566. });
  567. } else {
  568. this.form.menuIds = this.getMenuAllCheckedKeys();
  569. addRole(this.form).then(response => {
  570. this.$modal.msgSuccess("新增成功");
  571. this.open = false;
  572. this.getList();
  573. });
  574. }
  575. }
  576. });
  577. },
  578. /** 提交按钮(数据权限) */
  579. submitDataScope: function() {
  580. if (this.form.roleId != undefined) {
  581. this.form.deptIds = this.getDeptAllCheckedKeys();
  582. dataScope(this.form).then(response => {
  583. this.$modal.msgSuccess("修改成功");
  584. this.openDataScope = false;
  585. this.getList();
  586. });
  587. }
  588. },
  589. /** 删除按钮操作 */
  590. handleDelete(row) {
  591. const roleIds = row.roleId || this.ids;
  592. this.$modal.confirm('是否确认删除角色编号为"' + roleIds + '"的数据项?').then(function() {
  593. return delRole(roleIds);
  594. }).then(() => {
  595. this.getList();
  596. this.$modal.msgSuccess("删除成功");
  597. }).catch(() => {});
  598. },
  599. /** 导出按钮操作 */
  600. handleExport() {
  601. this.download('system/role/export', {
  602. ...this.queryParams
  603. }, `role_${new Date().getTime()}.xlsx`)
  604. }
  605. }
  606. };
  607. </script>