index.vue 19 KB

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