index.vue 18 KB

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