AuthAdmin.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <template>
  2. <div class="box">
  3. <div class="box-content">
  4. <!-- input + button* 2 -->
  5. <div style="margin-bottom: 10px">
  6. <el-form :inline="true" label-width="80px">
  7. <el-form-item>
  8. <el-input v-model="keyword" placeholder="请输入职位名称"></el-input>
  9. </el-form-item>
  10. <el-form-item>
  11. <el-button @click="search" type="success">搜索</el-button>
  12. </el-form-item>
  13. <el-form-item>
  14. <el-button
  15. type="primary"
  16. v-if="hasPerm('auth:add')"
  17. @click="dialogVisible = true"
  18. >新增职位</el-button
  19. >
  20. </el-form-item>
  21. </el-form>
  22. </div>
  23. <div>
  24. <el-table :data="tableData" border :height="720 + 'px'">
  25. <el-table-column prop="id" label="职位id"> </el-table-column>
  26. <el-table-column prop="groupName" label="职位名称"> </el-table-column>
  27. <el-table-column prop="remark" label="备注"> </el-table-column>
  28. <el-table-column label="操作">
  29. <template slot-scope="scope">
  30. <el-button
  31. size="mini"
  32. type="primary"
  33. v-if="hasPerm('auth:edit')"
  34. @click="handleEdit(scope.row)"
  35. >编辑</el-button
  36. >
  37. <el-button
  38. size="mini"
  39. type="warning"
  40. v-if="hasPerm('auth:Limits')"
  41. @click="handleLimits(scope.row)"
  42. >权限分配</el-button
  43. >
  44. <el-button
  45. size="mini"
  46. type="danger"
  47. v-if="hasPerm('auth:User')"
  48. @click="handleUser(scope.row)"
  49. >用户分配</el-button
  50. >
  51. <el-button
  52. size="mini"
  53. type="danger"
  54. v-if="hasPerm('auth:del')"
  55. @click="del(scope.row)"
  56. >删除</el-button
  57. >
  58. </template>
  59. </el-table-column>
  60. </el-table>
  61. <table-footer
  62. :totals="total"
  63. :size="size"
  64. @sizeChange="sizeChange"
  65. @pageChange="pageChange"
  66. ></table-footer>
  67. </div>
  68. </div>
  69. <!-- // 新增-->
  70. <el-dialog
  71. :title="showType ? '编辑职位' : '新增职位'"
  72. :visible.sync="dialogVisible"
  73. width="30%"
  74. >
  75. <div>
  76. <el-form ref="form" :model="form" label-width="100px">
  77. <el-form-item label="职位名称">
  78. <el-input v-model="form.groupName"></el-input>
  79. </el-form-item>
  80. <el-form-item label="备注">
  81. <el-input v-model="form.remark"></el-input>
  82. </el-form-item>
  83. </el-form>
  84. </div>
  85. <span slot="footer" class="dialog-footer">
  86. <el-button @click="reset">取 消</el-button>
  87. <el-button v-if="showType" type="primary" @click="setUpdate"
  88. >更 新</el-button
  89. >
  90. <el-button v-else type="primary" @click="onSubmit">新 增</el-button>
  91. </span>
  92. </el-dialog>
  93. <!-- 权限分配 -->
  94. <a-modal
  95. title="职位分配"
  96. :width="600"
  97. :visible="dialogLimits"
  98. @ok="limitSave"
  99. ok-text="确认"
  100. cancel-text="取消"
  101. @cancel="dialogLimits = false"
  102. :destroyOnClose="true"
  103. >
  104. <a-tree
  105. v-model="checkedKeys"
  106. multiple
  107. checkable
  108. :auto-expand-parent="autoExpandParent"
  109. :expanded-keys="expandedKeys"
  110. :tree-data="menuTreeData"
  111. :selected-keys="selectedKeys"
  112. :replaceFields="replaceFields"
  113. @expand="onExpand"
  114. @check="onCheck"
  115. />
  116. </a-modal>
  117. <el-dialog title="用户分配" :visible.sync="dialogUsers" width="50%">
  118. <div>
  119. <el-table
  120. :data="userList"
  121. ref="multipleTable"
  122. tooltip-effect="dark"
  123. @selection-change="handleSelectionChange"
  124. >
  125. <el-table-column type="selection" width="55"> </el-table-column>
  126. <el-table-column prop="id" label="用户ID"> </el-table-column>
  127. <el-table-column prop="account" label="用户姓名"> </el-table-column>
  128. </el-table>
  129. </div>
  130. <span slot="footer" class="dialog-footer">
  131. <el-button @click="dialogUsers = false">取 消</el-button>
  132. <el-button type="primary" @click="userSave">保存</el-button>
  133. </span>
  134. </el-dialog>
  135. </div>
  136. </template>
  137. <script>
  138. import { Tree } from "ant-design-vue";
  139. import TableFooter from "../../components/TableFooter";
  140. import {
  141. getUserMenu,
  142. getAuth,
  143. delAuth,
  144. addAuth,
  145. editAuth,
  146. getMenuByGroup,
  147. saveGroupMenu,
  148. getAcountByGroup,
  149. saveAccountGroup,
  150. getUserList,
  151. } from "../../utils/api";
  152. export default {
  153. name: "AuthAdmin",
  154. components: {
  155. TableFooter,
  156. ATree: Tree,
  157. },
  158. data() {
  159. return {
  160. tableData: [],
  161. showType: false,
  162. dialogVisible: false,
  163. form: {
  164. id: "",
  165. groupName: "",
  166. remark: "",
  167. },
  168. dialogLimits: false,
  169. // 权限分配list
  170. limitList: [],
  171. defaultProps: {
  172. children: "children",
  173. label: "title",
  174. },
  175. autoExpandParent: true,
  176. // 默认选择的参数
  177. defaultList: [],
  178. expandedKeys: [],
  179. checkedKeys: [],
  180. replaceFields: {
  181. key: "id",
  182. },
  183. keyword: "",
  184. total: 0,
  185. size: 20,
  186. pageNum: 1,
  187. selectId: "",
  188. // 用户列表
  189. userList: [],
  190. dialogUsers: false,
  191. userSelect: [],
  192. leastChilds: [],
  193. commitKeys: [],
  194. selectedKeys: [],
  195. menuTreeData: [],
  196. // 判断是否点击权限的复选框
  197. isKey: false,
  198. };
  199. },
  200. methods: {
  201. // 搜索
  202. search() {
  203. this.init();
  204. },
  205. // 修改size
  206. sizeChange(val) {
  207. this.size = val;
  208. this.init();
  209. },
  210. // 修改页数
  211. pageChange(val) {
  212. this.pageNum = val;
  213. this.init();
  214. },
  215. handleEdit(data) {
  216. this.showType = true;
  217. this.dialogVisible = true;
  218. this.form.remark = data.remark;
  219. this.form.groupName = data.groupName;
  220. this.form.id = data.id;
  221. },
  222. // 用户分配
  223. handleUser(data) {
  224. this.dialogUsers = true;
  225. this.selectId = data.id;
  226. getAcountByGroup({ groupId: data.id }).then((res) => {
  227. if (res.code === 10006) {
  228. this.userSelect = [];
  229. this.$nextTick(() => {
  230. this.$refs.multipleTable.clearSelection();
  231. this.$forceUpdate();
  232. });
  233. } else {
  234. let arr = res.data.split(",");
  235. console.log(arr);
  236. this.$nextTick(() => {
  237. this.$refs.multipleTable.clearSelection();
  238. for (let key in this.userList) {
  239. for (let i in arr) {
  240. if (this.userList[key]["id"] == arr[i]) {
  241. this.$refs.multipleTable.toggleRowSelection(
  242. this.userList[key],
  243. true
  244. );
  245. }
  246. }
  247. }
  248. });
  249. }
  250. });
  251. },
  252. // 用户分配选择
  253. handleSelectionChange(val) {
  254. this.userSelect = val;
  255. },
  256. // 用户分配保存
  257. userSave() {
  258. let users = [];
  259. this.userSelect.forEach((item) => {
  260. users.push(item.id);
  261. });
  262. let str = users.join(",");
  263. saveAccountGroup({ accountId: str, groupIds: this.selectId }).then(
  264. (res) => {
  265. if (res.code === 10001) {
  266. this.$message.success(res.message);
  267. } else {
  268. this.userSelect = [];
  269. this.selectId = "";
  270. this.$message(res.message);
  271. }
  272. }
  273. );
  274. this.dialogUsers = false;
  275. },
  276. // 新增角色
  277. onSubmit() {
  278. let params = {
  279. groupName: this.form.groupName,
  280. remark: this.form.remark,
  281. };
  282. addAuth(params).then((res) => {
  283. if (res.code === 10000) {
  284. this.init();
  285. this.$message.success(res.message);
  286. } else {
  287. this.$message.error(res.message);
  288. }
  289. this.dialogVisible = false;
  290. this.reset();
  291. });
  292. },
  293. // handleCheckChange() {
  294. // // console.log(data, ls, lg)
  295. // this.selectList = [];
  296. // console.log(this.$refs.tree.getCheckedKeys());
  297. // this.selectList = this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys());
  298. // },
  299. onExpand(expandedKeys) {
  300. this.expandedKeys = expandedKeys;
  301. this.autoExpandParent = false;
  302. },
  303. onCheck(checkedKeys, info) {
  304. this.isKey = true;
  305. this.checkedKeys = checkedKeys;
  306. this.commitKeys = checkedKeys.concat(info.halfCheckedKeys);
  307. },
  308. pickCheckedKeys(data) {
  309. for (let i = 0; i < data.length; i++) {
  310. if (this.leastChilds.includes(data[i])) {
  311. this.checkedKeys.push(data[i]);
  312. }
  313. }
  314. },
  315. onSelect(selectedKeys) {
  316. this.selectedKeys = selectedKeys;
  317. },
  318. // 权限分配
  319. handleLimits(data) {
  320. this.dialogLimits = true;
  321. this.selectId = data.id;
  322. this.defaultList = [];
  323. getMenuByGroup({ groupId: data.id }).then((res) => {
  324. let arr;
  325. if (res.code === 10000) {
  326. arr = res.data.split(",");
  327. this.defaultList = arr.map(Number);
  328. this.pickCheckedKeys(this.defaultList);
  329. this.selectList = this.defaultList;
  330. } else {
  331. arr = [];
  332. this.defaultList = [];
  333. this.selectList = [];
  334. this.selectedKeys = [];
  335. this.checkedKeys = [];
  336. }
  337. });
  338. },
  339. // 权限分配保存
  340. limitSave() {
  341. // this.selectList = this.$refs.tree.getCheckedKeys();
  342. let menuIds;
  343. if (this.commitKeys.length > 0) {
  344. menuIds = this.commitKeys.join(",");
  345. } else {
  346. menuIds = "";
  347. }
  348. saveGroupMenu({ groupId: this.selectId, menuIds: menuIds }).then(
  349. (res) => {
  350. if (res.code === 10001) {
  351. this.$message.success(res.message);
  352. }
  353. this.selectId = "";
  354. this.dialogLimits = false;
  355. this.selectList = [];
  356. }
  357. );
  358. if (this.isKey) {
  359. let menuIds;
  360. if (this.commitKeys.length > 0) {
  361. menuIds = this.commitKeys.join(",");
  362. } else {
  363. menuIds = "";
  364. }
  365. saveGroupMenu({ groupId: this.selectId, menuIds: menuIds }).then(
  366. (res) => {
  367. if (res.code === 10001) {
  368. this.$message.success(res.message);
  369. }
  370. this.selectId = "";
  371. this.dialogLimits = false;
  372. this.selectList = [];
  373. }
  374. );
  375. } else {
  376. this.dialogLimits = false;
  377. }
  378. },
  379. init() {
  380. // 后端分页
  381. let params = {
  382. pageNum: this.pageNum,
  383. pageSize: this.size,
  384. searchStr: this.keyword,
  385. };
  386. getAuth(params).then((res) => {
  387. this.tableData = res.records;
  388. this.total = res.total;
  389. });
  390. },
  391. // 重置
  392. reset() {
  393. this.form.id = "";
  394. this.form.groupName = "";
  395. this.form.remark = "";
  396. this.pageNum = 1;
  397. this.keyword = "";
  398. this.showType = false;
  399. this.dialogVisible = false;
  400. },
  401. // 更新
  402. setUpdate() {
  403. let params = {
  404. groupName: this.form.groupName,
  405. remark: this.form.remark,
  406. id: this.form.id,
  407. };
  408. editAuth(params).then((res) => {
  409. if (res.code === 10000) {
  410. this.init();
  411. this.$message.success(res.message);
  412. }
  413. this.dialogVisible = false;
  414. this.reset();
  415. });
  416. },
  417. // 删除
  418. del(data) {
  419. this.$confirm("此操作将永久删除批量删除, 是否继续?", "提示", {
  420. confirmButtonText: "确定",
  421. cancelButtonText: "取消",
  422. type: "warning",
  423. })
  424. .then(() => {
  425. delAuth({ groupId: data.id }).then((res) => {
  426. if (res.code === 10000) {
  427. this.init();
  428. this.$message.success(res.message);
  429. }
  430. });
  431. })
  432. .catch(() => {
  433. this.$message({
  434. type: "info",
  435. message: "已取消删除",
  436. });
  437. });
  438. },
  439. // 菜单列表
  440. authInit() {
  441. let params = {
  442. userId: localStorage.getItem("gold_UserId"),
  443. };
  444. getUserMenu(params).then((res) => {
  445. if (res.code === 10000) {
  446. this.menuTreeData = res.data;
  447. this.getLeastChilds(res.data);
  448. this.menuTreeData.forEach((item) => {
  449. this.expandedKeys.push(item.id);
  450. });
  451. // this.expandedMenuKeys(this.roleEntity)
  452. }
  453. });
  454. },
  455. getLeastChilds(data) {
  456. for (let i = 0; i < data.length; i++) {
  457. this.pushLeastChilds(data[i]);
  458. }
  459. },
  460. pushLeastChilds(e) {
  461. if (e.children.length > 0) {
  462. this.getLeastChilds(e.children);
  463. return;
  464. }
  465. this.leastChilds.push(e.id);
  466. },
  467. // 用户列表
  468. userInit() {
  469. let params = {
  470. pageNum: 1,
  471. pageSize: 200,
  472. searchStr: "",
  473. };
  474. getUserList(params).then((res) => {
  475. if (res.code === 10000) {
  476. this.userList = res.data.records;
  477. }
  478. });
  479. },
  480. },
  481. mounted() {
  482. this.init();
  483. this.authInit();
  484. this.userInit();
  485. },
  486. };
  487. </script>
  488. <style scoped>
  489. .box {
  490. width: 100%;
  491. height: 100%;
  492. box-sizing: border-box;
  493. padding: 20px;
  494. }
  495. </style>