index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <template>
  2. <div class="app-container">
  3. <el-form :model="queryParams" ref="queryForm" :inline="true">
  4. <el-form-item label="角色名称" prop="roleName">
  5. <el-input
  6. v-model="queryParams.roleName"
  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="roleKey">
  15. <el-input
  16. v-model="queryParams.roleKey"
  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 statusOptions"
  34. :key="dict.dictValue"
  35. :label="dict.dictLabel"
  36. :value="dict.dictValue"
  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="primary" 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="success"
  70. icon="el-icon-edit"
  71. size="mini"
  72. :disabled="single"
  73. @click="handleUpdate"
  74. v-hasPermi="['system:role:edit']"
  75. >修改</el-button>
  76. </el-col>
  77. <el-col :span="1.5">
  78. <el-button
  79. type="danger"
  80. icon="el-icon-delete"
  81. size="mini"
  82. :disabled="multiple"
  83. @click="handleDelete"
  84. v-hasPermi="['system:role:remove']"
  85. >删除</el-button>
  86. </el-col>
  87. <el-col :span="1.5">
  88. <el-button
  89. type="warning"
  90. icon="el-icon-download"
  91. size="mini"
  92. @click="handleExport"
  93. v-hasPermi="['system:role:export']"
  94. >导出</el-button>
  95. </el-col>
  96. </el-row>
  97. <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
  98. <el-table-column type="selection" width="55" align="center" />
  99. <el-table-column label="角色编号" prop="roleId" width="120" />
  100. <el-table-column label="角色名称" prop="roleName" :show-overflow-tooltip="true" width="150" />
  101. <el-table-column label="权限字符" prop="roleKey" :show-overflow-tooltip="true" width="150" />
  102. <el-table-column label="显示顺序" prop="roleSort" width="100" />
  103. <el-table-column label="状态" align="center" width="100">
  104. <template slot-scope="scope">
  105. <el-switch
  106. v-model="scope.row.status"
  107. active-value="0"
  108. inactive-value="1"
  109. @change="handleStatusChange(scope.row)"
  110. ></el-switch>
  111. </template>
  112. </el-table-column>
  113. <el-table-column label="创建时间" align="center" prop="createTime" width="180">
  114. <template slot-scope="scope">
  115. <span>{{ parseTime(scope.row.createTime) }}</span>
  116. </template>
  117. </el-table-column>
  118. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  119. <template slot-scope="scope">
  120. <el-button
  121. size="mini"
  122. type="text"
  123. icon="el-icon-edit"
  124. @click="handleUpdate(scope.row)"
  125. v-hasPermi="['system:role:edit']"
  126. >修改</el-button>
  127. <el-button
  128. size="mini"
  129. type="text"
  130. icon="el-icon-circle-check"
  131. @click="handleDataScope(scope.row)"
  132. v-hasPermi="['system:role:edit']"
  133. >数据权限</el-button>
  134. <el-button
  135. size="mini"
  136. type="text"
  137. icon="el-icon-delete"
  138. @click="handleDelete(scope.row)"
  139. v-hasPermi="['system:role:remove']"
  140. >删除</el-button>
  141. </template>
  142. </el-table-column>
  143. </el-table>
  144. <pagination
  145. v-show="total>0"
  146. :total="total"
  147. :page.sync="queryParams.pageNum"
  148. :limit.sync="queryParams.pageSize"
  149. @pagination="getList"
  150. />
  151. <!-- 添加或修改角色配置对话框 -->
  152. <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
  153. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  154. <el-form-item label="角色名称" prop="roleName">
  155. <el-input v-model="form.roleName" placeholder="请输入角色名称" />
  156. </el-form-item>
  157. <el-form-item label="权限字符" prop="roleKey">
  158. <el-input v-model="form.roleKey" placeholder="请输入权限字符" />
  159. </el-form-item>
  160. <el-form-item label="角色顺序" prop="roleSort">
  161. <el-input-number v-model="form.roleSort" controls-position="right" :min="0" />
  162. </el-form-item>
  163. <el-form-item label="状态">
  164. <el-radio-group v-model="form.status">
  165. <el-radio
  166. v-for="dict in statusOptions"
  167. :key="dict.dictValue"
  168. :label="dict.dictValue"
  169. >{{dict.dictLabel}}</el-radio>
  170. </el-radio-group>
  171. </el-form-item>
  172. <el-form-item label="菜单权限">
  173. <el-tree
  174. :data="menuOptions"
  175. show-checkbox
  176. ref="menu"
  177. node-key="id"
  178. empty-text="加载中,请稍后"
  179. :props="defaultProps"
  180. ></el-tree>
  181. </el-form-item>
  182. <el-form-item label="备注">
  183. <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
  184. </el-form-item>
  185. </el-form>
  186. <div slot="footer" class="dialog-footer">
  187. <el-button type="primary" @click="submitForm">确 定</el-button>
  188. <el-button @click="cancel">取 消</el-button>
  189. </div>
  190. </el-dialog>
  191. <!-- 分配角色数据权限对话框 -->
  192. <el-dialog :title="title" :visible.sync="openDataScope" width="500px" append-to-body>
  193. <el-form :model="form" label-width="80px">
  194. <el-form-item label="角色名称">
  195. <el-input v-model="form.roleName" :disabled="true" />
  196. </el-form-item>
  197. <el-form-item label="权限字符">
  198. <el-input v-model="form.roleKey" :disabled="true" />
  199. </el-form-item>
  200. <el-form-item label="权限范围">
  201. <el-select v-model="form.dataScope">
  202. <el-option
  203. v-for="item in dataScopeOptions"
  204. :key="item.value"
  205. :label="item.label"
  206. :value="item.value"
  207. ></el-option>
  208. </el-select>
  209. </el-form-item>
  210. <el-form-item label="数据权限" v-show="form.dataScope == 2">
  211. <el-tree
  212. :data="deptOptions"
  213. show-checkbox
  214. default-expand-all
  215. ref="dept"
  216. node-key="id"
  217. empty-text="加载中,请稍后"
  218. :props="defaultProps"
  219. ></el-tree>
  220. </el-form-item>
  221. </el-form>
  222. <div slot="footer" class="dialog-footer">
  223. <el-button type="primary" @click="submitDataScope">确 定</el-button>
  224. <el-button @click="cancelDataScope">取 消</el-button>
  225. </div>
  226. </el-dialog>
  227. </div>
  228. </template>
  229. <script>
  230. import { listRole, getRole, delRole, addRole, updateRole, exportRole, dataScope, changeRoleStatus } from "@/api/system/role";
  231. import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
  232. import { treeselect as deptTreeselect, roleDeptTreeselect } from "@/api/system/dept";
  233. export default {
  234. name: "Role",
  235. data() {
  236. return {
  237. // 遮罩层
  238. loading: true,
  239. // 选中数组
  240. ids: [],
  241. // 非单个禁用
  242. single: true,
  243. // 非多个禁用
  244. multiple: true,
  245. // 总条数
  246. total: 0,
  247. // 角色表格数据
  248. roleList: [],
  249. // 弹出层标题
  250. title: "",
  251. // 是否显示弹出层
  252. open: false,
  253. // 是否显示弹出层(数据权限)
  254. openDataScope: false,
  255. // 日期范围
  256. dateRange: [],
  257. // 状态数据字典
  258. statusOptions: [],
  259. // 数据范围选项
  260. dataScopeOptions: [
  261. {
  262. value: "1",
  263. label: "全部数据权限"
  264. },
  265. {
  266. value: "2",
  267. label: "自定数据权限"
  268. },
  269. {
  270. value: "3",
  271. label: "本部门数据权限"
  272. },
  273. {
  274. value: "4",
  275. label: "本部门及以下数据权限"
  276. },
  277. {
  278. value: "5",
  279. label: "仅本人数据权限"
  280. }
  281. ],
  282. // 菜单列表
  283. menuOptions: [],
  284. // 部门列表
  285. deptOptions: [],
  286. // 查询参数
  287. queryParams: {
  288. pageNum: 1,
  289. pageSize: 10,
  290. roleName: undefined,
  291. roleKey: undefined,
  292. status: undefined
  293. },
  294. // 表单参数
  295. form: {},
  296. defaultProps: {
  297. children: "children",
  298. label: "label"
  299. },
  300. // 表单校验
  301. rules: {
  302. roleName: [
  303. { required: true, message: "角色名称不能为空", trigger: "blur" }
  304. ],
  305. roleKey: [
  306. { required: true, message: "权限字符不能为空", trigger: "blur" }
  307. ],
  308. roleSort: [
  309. { required: true, message: "角色顺序不能为空", trigger: "blur" }
  310. ]
  311. }
  312. };
  313. },
  314. created() {
  315. this.getList();
  316. this.getDicts("sys_normal_disable").then(response => {
  317. this.statusOptions = response.data;
  318. });
  319. },
  320. methods: {
  321. /** 查询角色列表 */
  322. getList() {
  323. this.loading = true;
  324. listRole(this.addDateRange(this.queryParams, this.dateRange)).then(
  325. response => {
  326. this.roleList = response.rows;
  327. this.total = response.total;
  328. this.loading = false;
  329. }
  330. );
  331. },
  332. /** 查询菜单树结构 */
  333. getMenuTreeselect() {
  334. menuTreeselect().then(response => {
  335. this.menuOptions = response.data;
  336. });
  337. },
  338. /** 查询部门树结构 */
  339. getDeptTreeselect() {
  340. deptTreeselect().then(response => {
  341. this.deptOptions = response.data;
  342. });
  343. },
  344. // 所有菜单节点数据
  345. getMenuAllCheckedKeys() {
  346. // 目前被选中的菜单节点
  347. let checkedKeys = this.$refs.menu.getHalfCheckedKeys();
  348. // 半选中的菜单节点
  349. let halfCheckedKeys = this.$refs.menu.getCheckedKeys();
  350. checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
  351. return checkedKeys;
  352. },
  353. // 所有部门节点数据
  354. getDeptAllCheckedKeys() {
  355. // 目前被选中的部门节点
  356. let checkedKeys = this.$refs.dept.getHalfCheckedKeys();
  357. // 半选中的部门节点
  358. let halfCheckedKeys = this.$refs.dept.getCheckedKeys();
  359. checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
  360. return checkedKeys;
  361. },
  362. /** 根据角色ID查询菜单树结构 */
  363. getRoleMenuTreeselect(roleId) {
  364. return roleMenuTreeselect(roleId).then(response => {
  365. this.menuOptions = response.menus;
  366. return response;
  367. });
  368. },
  369. /** 根据角色ID查询部门树结构 */
  370. getRoleDeptTreeselect(roleId) {
  371. return roleDeptTreeselect(roleId).then(response => {
  372. this.deptOptions = response.depts;
  373. return response;
  374. });
  375. },
  376. // 角色状态修改
  377. handleStatusChange(row) {
  378. let text = row.status === "0" ? "启用" : "停用";
  379. this.$confirm('确认要"' + text + '""' + row.roleName + '"角色吗?', "警告", {
  380. confirmButtonText: "确定",
  381. cancelButtonText: "取消",
  382. type: "warning"
  383. }).then(function() {
  384. return changeRoleStatus(row.roleId, row.status);
  385. }).then(() => {
  386. this.msgSuccess(text + "成功");
  387. }).catch(function() {
  388. row.status = row.status === "0" ? "1" : "0";
  389. });
  390. },
  391. // 取消按钮
  392. cancel() {
  393. this.open = false;
  394. this.reset();
  395. },
  396. // 取消按钮(数据权限)
  397. cancelDataScope() {
  398. this.openDataScope = false;
  399. this.reset();
  400. },
  401. // 表单重置
  402. reset() {
  403. if (this.$refs.menu != undefined) {
  404. this.$refs.menu.setCheckedKeys([]);
  405. }
  406. this.form = {
  407. roleId: undefined,
  408. roleName: undefined,
  409. roleKey: undefined,
  410. roleSort: 0,
  411. status: "0",
  412. menuIds: [],
  413. deptIds: [],
  414. remark: undefined
  415. };
  416. this.resetForm("form");
  417. },
  418. /** 搜索按钮操作 */
  419. handleQuery() {
  420. this.queryParams.pageNum = 1;
  421. this.getList();
  422. },
  423. /** 重置按钮操作 */
  424. resetQuery() {
  425. this.dateRange = [];
  426. this.resetForm("queryForm");
  427. this.handleQuery();
  428. },
  429. // 多选框选中数据
  430. handleSelectionChange(selection) {
  431. this.ids = selection.map(item => item.roleId)
  432. this.single = selection.length!=1
  433. this.multiple = !selection.length
  434. },
  435. /** 新增按钮操作 */
  436. handleAdd() {
  437. this.reset();
  438. this.getMenuTreeselect();
  439. this.open = true;
  440. this.title = "添加角色";
  441. },
  442. /** 修改按钮操作 */
  443. handleUpdate(row) {
  444. this.reset();
  445. const roleId = row.roleId || this.ids
  446. const roleMenu = this.getRoleMenuTreeselect(roleId);
  447. getRole(roleId).then(response => {
  448. this.form = response.data;
  449. this.open = true;
  450. this.$nextTick(() => {
  451. roleMenu.then(res => {
  452. this.$refs.menu.setCheckedKeys(res.checkedKeys);
  453. });
  454. });
  455. this.title = "修改角色";
  456. });
  457. },
  458. /** 分配数据权限操作 */
  459. handleDataScope(row) {
  460. this.reset();
  461. const roleDeptTreeselect = this.getRoleDeptTreeselect(row.roleId);
  462. getRole(row.roleId).then(response => {
  463. this.form = response.data;
  464. this.openDataScope = true;
  465. this.$nextTick(() => {
  466. roleDeptTreeselect.then(res => {
  467. this.$refs.dept.setCheckedKeys(res.checkedKeys);
  468. });
  469. });
  470. this.title = "分配数据权限";
  471. });
  472. },
  473. /** 提交按钮 */
  474. submitForm: function() {
  475. this.$refs["form"].validate(valid => {
  476. if (valid) {
  477. if (this.form.roleId != undefined) {
  478. this.form.menuIds = this.getMenuAllCheckedKeys();
  479. updateRole(this.form).then(response => {
  480. if (response.code === 200) {
  481. this.msgSuccess("修改成功");
  482. this.open = false;
  483. this.getList();
  484. }
  485. });
  486. } else {
  487. this.form.menuIds = this.getMenuAllCheckedKeys();
  488. addRole(this.form).then(response => {
  489. if (response.code === 200) {
  490. this.msgSuccess("新增成功");
  491. this.open = false;
  492. this.getList();
  493. }
  494. });
  495. }
  496. }
  497. });
  498. },
  499. /** 提交按钮(数据权限) */
  500. submitDataScope: function() {
  501. if (this.form.roleId != undefined) {
  502. this.form.deptIds = this.getDeptAllCheckedKeys();
  503. dataScope(this.form).then(response => {
  504. if (response.code === 200) {
  505. this.msgSuccess("修改成功");
  506. this.openDataScope = false;
  507. this.getList();
  508. }
  509. });
  510. }
  511. },
  512. /** 删除按钮操作 */
  513. handleDelete(row) {
  514. const roleIds = row.roleId || this.ids;
  515. this.$confirm('是否确认删除角色编号为"' + roleIds + '"的数据项?', "警告", {
  516. confirmButtonText: "确定",
  517. cancelButtonText: "取消",
  518. type: "warning"
  519. }).then(function() {
  520. return delRole(roleIds);
  521. }).then(() => {
  522. this.getList();
  523. this.msgSuccess("删除成功");
  524. }).catch(function() {});
  525. },
  526. /** 导出按钮操作 */
  527. handleExport() {
  528. const queryParams = this.queryParams;
  529. this.$confirm('是否确认导出所有角色数据项?', "警告", {
  530. confirmButtonText: "确定",
  531. cancelButtonText: "取消",
  532. type: "warning"
  533. }).then(function() {
  534. return exportRole(queryParams);
  535. }).then(response => {
  536. this.download(response.msg);
  537. }).catch(function() {});
  538. }
  539. }
  540. };
  541. </script>