index.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 :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. v-if="![8, 9, 10].includes(scope.row.roleId)"
  130. size="mini"
  131. type="text"
  132. icon="el-icon-delete"
  133. @click="handleDelete(scope.row)"
  134. v-hasPermi="['system:role:remove']"
  135. >删除</el-button>
  136. <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['system:role:edit']">
  137. <span class="el-dropdown-link">
  138. <i class="el-icon-d-arrow-right el-icon--right"></i>更多
  139. </span>
  140. <el-dropdown-menu slot="dropdown">
  141. <el-dropdown-item command="handleDataScope" icon="el-icon-circle-check"
  142. v-hasPermi="['system:role:edit']">数据权限</el-dropdown-item>
  143. <el-dropdown-item command="handleAuthUser" icon="el-icon-user"
  144. v-hasPermi="['system:role:edit']">分配用户</el-dropdown-item>
  145. </el-dropdown-menu>
  146. </el-dropdown>
  147. </template>
  148. </el-table-column>
  149. </el-table>
  150. <pagination
  151. v-show="total>0"
  152. :total="total"
  153. :page.sync="queryParams.pageNum"
  154. :limit.sync="queryParams.pageSize"
  155. @pagination="getList"
  156. />
  157. <!-- 添加或修改角色配置对话框 -->
  158. <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
  159. <el-form ref="form" :model="form" :rules="rules" label-width="100px">
  160. <el-form-item label="角色名称" prop="roleName">
  161. <el-input v-model="form.roleName" placeholder="请输入角色名称" />
  162. </el-form-item>
  163. <el-form-item prop="roleKey">
  164. <span slot="label">
  165. <el-tooltip content="控制器中定义的权限字符,如:@PreAuthorize(`@ss.hasRole('admin')`)" placement="top">
  166. <i class="el-icon-question"></i>
  167. </el-tooltip>
  168. 权限字符
  169. </span>
  170. <el-input v-model="form.roleKey" placeholder="请输入权限字符" />
  171. </el-form-item>
  172. <el-form-item label="角色顺序" prop="roleSort">
  173. <el-input-number v-model="form.roleSort" controls-position="right" :min="0" />
  174. </el-form-item>
  175. <el-form-item label="状态">
  176. <el-radio-group v-model="form.status">
  177. <el-radio
  178. v-for="dict in dict.type.sys_normal_disable"
  179. :key="dict.value"
  180. :label="dict.value"
  181. >{{dict.label}}</el-radio>
  182. </el-radio-group>
  183. </el-form-item>
  184. <el-form-item label="菜单权限">
  185. <el-checkbox v-model="menuExpand" @change="handleCheckedTreeExpand($event, 'menu')">展开/折叠</el-checkbox>
  186. <el-checkbox v-model="menuNodeAll" @change="handleCheckedTreeNodeAll($event, 'menu')">全选/全不选</el-checkbox>
  187. <el-checkbox v-model="form.menuCheckStrictly" @change="handleCheckedTreeConnect($event, 'menu')">父子联动</el-checkbox>
  188. <el-tree
  189. class="tree-border"
  190. :data="menuOptions"
  191. show-checkbox
  192. ref="menu"
  193. node-key="id"
  194. :check-strictly="!form.menuCheckStrictly"
  195. empty-text="加载中,请稍候"
  196. :props="defaultProps"
  197. ></el-tree>
  198. </el-form-item>
  199. <el-form-item label="备注">
  200. <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
  201. </el-form-item>
  202. </el-form>
  203. <div slot="footer" class="dialog-footer">
  204. <el-button type="primary" @click="submitForm">确 定</el-button>
  205. <el-button @click="cancel">取 消</el-button>
  206. </div>
  207. </el-dialog>
  208. <!-- 分配角色数据权限对话框 -->
  209. <el-dialog :title="title" :visible.sync="openDataScope" width="500px" append-to-body>
  210. <el-form :model="form" label-width="80px">
  211. <el-form-item label="角色名称">
  212. <el-input v-model="form.roleName" :disabled="true" />
  213. </el-form-item>
  214. <!-- <el-form-item label="权限字符">
  215. <el-input v-model="form.roleKey" :disabled="true" />
  216. </el-form-item> -->
  217. <el-form-item label="权限范围">
  218. <el-select v-model="form.dataScope" @change="dataScopeSelectChange">
  219. <el-option
  220. v-for="item in dataScopeOptions"
  221. :key="item.value"
  222. :label="item.label"
  223. :value="item.value"
  224. ></el-option>
  225. </el-select>
  226. </el-form-item>
  227. <el-form-item label="数据权限" v-show="form.dataScope == 2">
  228. <el-checkbox v-model="deptExpand" @change="handleCheckedTreeExpand($event, 'dept')">展开/折叠</el-checkbox>
  229. <el-checkbox v-model="deptNodeAll" @change="handleCheckedTreeNodeAll($event, 'dept')">全选/全不选</el-checkbox>
  230. <el-checkbox v-model="form.deptCheckStrictly" @change="handleCheckedTreeConnect($event, 'dept')">父子联动</el-checkbox>
  231. <el-tree
  232. class="tree-border"
  233. :data="deptOptions"
  234. show-checkbox
  235. default-expand-all
  236. ref="dept"
  237. node-key="id"
  238. :check-strictly="!form.deptCheckStrictly"
  239. empty-text="加载中,请稍候"
  240. :props="defaultProps"
  241. ></el-tree>
  242. </el-form-item>
  243. </el-form>
  244. <div slot="footer" class="dialog-footer">
  245. <el-button type="primary" @click="submitDataScope">确 定</el-button>
  246. <el-button @click="cancelDataScope">取 消</el-button>
  247. </div>
  248. </el-dialog>
  249. </div>
  250. </template>
  251. <script>
  252. import { listRole, getRole, delRole, addRole, updateRole, dataScope, changeRoleStatus, deptTreeSelect } from "@/api/system/role";
  253. import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
  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. getMenuAllCheckedKeys() {
  362. // 目前被选中的菜单节点
  363. let checkedKeys = this.$refs.menu.getCheckedKeys();
  364. // 半选中的菜单节点
  365. let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
  366. checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
  367. return checkedKeys;
  368. },
  369. // 所有部门节点数据
  370. getDeptAllCheckedKeys() {
  371. // 目前被选中的部门节点
  372. let checkedKeys = this.$refs.dept.getCheckedKeys();
  373. // 半选中的部门节点
  374. let halfCheckedKeys = this.$refs.dept.getHalfCheckedKeys();
  375. checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
  376. return checkedKeys;
  377. },
  378. /** 根据角色ID查询菜单树结构 */
  379. getRoleMenuTreeselect(roleId) {
  380. return roleMenuTreeselect(roleId).then(response => {
  381. this.menuOptions = response.menus;
  382. return response;
  383. });
  384. },
  385. /** 根据角色ID查询部门树结构 */
  386. getDeptTree(roleId) {
  387. return deptTreeSelect(roleId).then(response => {
  388. this.deptOptions = response.depts;
  389. return response;
  390. });
  391. },
  392. // 角色状态修改
  393. handleStatusChange(row) {
  394. let text = row.status === "0" ? "启用" : "停用";
  395. this.$modal.confirm('确认要"' + text + '""' + row.roleName + '"角色吗?').then(function() {
  396. return changeRoleStatus(row.roleId, row.status);
  397. }).then(() => {
  398. this.$modal.msgSuccess(text + "成功");
  399. }).catch(function() {
  400. row.status = row.status === "0" ? "1" : "0";
  401. });
  402. },
  403. // 取消按钮
  404. cancel() {
  405. this.open = false;
  406. this.reset();
  407. },
  408. // 取消按钮(数据权限)
  409. cancelDataScope() {
  410. this.openDataScope = false;
  411. this.reset();
  412. },
  413. // 表单重置
  414. reset() {
  415. if (this.$refs.menu != undefined) {
  416. this.$refs.menu.setCheckedKeys([]);
  417. }
  418. this.menuExpand = false,
  419. this.menuNodeAll = false,
  420. this.deptExpand = true,
  421. this.deptNodeAll = false,
  422. this.form = {
  423. roleId: undefined,
  424. roleName: undefined,
  425. roleKey: undefined,
  426. roleSort: 0,
  427. status: "0",
  428. menuIds: [],
  429. deptIds: [],
  430. menuCheckStrictly: true,
  431. deptCheckStrictly: true,
  432. remark: undefined
  433. };
  434. this.resetForm("form");
  435. },
  436. /** 搜索按钮操作 */
  437. handleQuery() {
  438. this.queryParams.pageNum = 1;
  439. this.getList();
  440. },
  441. /** 重置按钮操作 */
  442. resetQuery() {
  443. this.dateRange = [];
  444. this.resetForm("queryForm");
  445. this.handleQuery();
  446. },
  447. // 多选框选中数据
  448. handleSelectionChange(selection) {
  449. this.ids = selection.map(item => item.roleId)
  450. this.single = selection.length!=1
  451. this.multiple = !selection.length
  452. },
  453. // 更多操作触发
  454. handleCommand(command, row) {
  455. switch (command) {
  456. case "handleDataScope":
  457. this.handleDataScope(row);
  458. break;
  459. case "handleAuthUser":
  460. this.handleAuthUser(row);
  461. break;
  462. default:
  463. break;
  464. }
  465. },
  466. // 树权限(展开/折叠)
  467. handleCheckedTreeExpand(value, type) {
  468. if (type == 'menu') {
  469. let treeList = this.menuOptions;
  470. for (let i = 0; i < treeList.length; i++) {
  471. this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value;
  472. }
  473. } else if (type == 'dept') {
  474. let treeList = this.deptOptions;
  475. for (let i = 0; i < treeList.length; i++) {
  476. this.$refs.dept.store.nodesMap[treeList[i].id].expanded = value;
  477. }
  478. }
  479. },
  480. // 树权限(全选/全不选)
  481. handleCheckedTreeNodeAll(value, type) {
  482. if (type == 'menu') {
  483. this.$refs.menu.setCheckedNodes(value ? this.menuOptions: []);
  484. } else if (type == 'dept') {
  485. this.$refs.dept.setCheckedNodes(value ? this.deptOptions: []);
  486. }
  487. },
  488. // 树权限(父子联动)
  489. handleCheckedTreeConnect(value, type) {
  490. if (type == 'menu') {
  491. this.form.menuCheckStrictly = value ? true: false;
  492. } else if (type == 'dept') {
  493. this.form.deptCheckStrictly = value ? true: false;
  494. }
  495. },
  496. /** 新增按钮操作 */
  497. handleAdd() {
  498. this.reset();
  499. this.getMenuTreeselect();
  500. this.open = true;
  501. this.title = "添加角色";
  502. },
  503. /** 修改按钮操作 */
  504. handleUpdate(row) {
  505. this.reset();
  506. const roleId = row.roleId || this.ids
  507. const roleMenu = this.getRoleMenuTreeselect(roleId);
  508. getRole(roleId).then(response => {
  509. this.form = response.data;
  510. this.open = true;
  511. this.$nextTick(() => {
  512. roleMenu.then(res => {
  513. let checkedKeys = res.checkedKeys
  514. checkedKeys.forEach((v) => {
  515. this.$nextTick(()=>{
  516. this.$refs.menu.setChecked(v, true ,false);
  517. })
  518. })
  519. });
  520. });
  521. this.title = "修改角色";
  522. });
  523. },
  524. /** 选择角色权限范围触发 */
  525. dataScopeSelectChange(value) {
  526. if(value !== '2') {
  527. this.$refs.dept.setCheckedKeys([]);
  528. }
  529. },
  530. /** 分配数据权限操作 */
  531. handleDataScope(row) {
  532. this.reset();
  533. const deptTreeSelect = this.getDeptTree(row.roleId);
  534. getRole(row.roleId).then(response => {
  535. this.form = response.data;
  536. this.openDataScope = true;
  537. this.$nextTick(() => {
  538. deptTreeSelect.then(res => {
  539. this.$refs.dept.setCheckedKeys(res.checkedKeys);
  540. });
  541. });
  542. this.title = "分配数据权限";
  543. });
  544. },
  545. /** 分配用户操作 */
  546. handleAuthUser: function(row) {
  547. const roleId = row.roleId;
  548. this.$router.push("/system/role-auth/user/" + roleId);
  549. },
  550. /** 提交按钮 */
  551. submitForm: function() {
  552. this.$refs["form"].validate(valid => {
  553. if (valid) {
  554. if (this.form.roleId != undefined) {
  555. this.form.menuIds = this.getMenuAllCheckedKeys();
  556. updateRole(this.form).then(response => {
  557. this.$modal.msgSuccess("修改成功");
  558. this.open = false;
  559. this.getList();
  560. });
  561. } else {
  562. this.form.menuIds = this.getMenuAllCheckedKeys();
  563. addRole(this.form).then(response => {
  564. this.$modal.msgSuccess("新增成功");
  565. this.open = false;
  566. this.getList();
  567. });
  568. }
  569. }
  570. });
  571. },
  572. /** 提交按钮(数据权限) */
  573. submitDataScope: function() {
  574. if (this.form.roleId != undefined) {
  575. this.form.deptIds = this.getDeptAllCheckedKeys();
  576. dataScope(this.form).then(response => {
  577. this.$modal.msgSuccess("修改成功");
  578. this.openDataScope = false;
  579. this.getList();
  580. });
  581. }
  582. },
  583. /** 删除按钮操作 */
  584. handleDelete(row) {
  585. const roleIds = row.roleId || this.ids;
  586. this.$modal.confirm('是否确认删除角色编号为"' + roleIds + '"的数据项?').then(function() {
  587. return delRole(roleIds);
  588. }).then(() => {
  589. this.getList();
  590. this.$modal.msgSuccess("删除成功");
  591. }).catch(() => {});
  592. },
  593. /** 导出按钮操作 */
  594. handleExport() {
  595. this.download('system/role/export', {
  596. ...this.queryParams
  597. }, `role_${new Date().getTime()}.xlsx`)
  598. }
  599. }
  600. };
  601. </script>