UserManagement.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. <template>
  2. <div class="authContainer">
  3. <div class="content">
  4. <div class="list_container userList">
  5. <el-breadcrumb separator-class="el-icon-arrow-right" :class="'title'">
  6. <el-breadcrumb-item>用户列表</el-breadcrumb-item>
  7. </el-breadcrumb>
  8. <el-row v-for="(item, index) in userList" :key="index">
  9. <div @click="changeUser(item)">{{ item.username }}</div>
  10. </el-row>
  11. </div>
  12. <div class="list_container layerList" v-show="isShowLayerList">
  13. <div class="batchSetRegion">
  14. <el-button type="text" @click="openBatchSetRegion"
  15. >批量设置行政区划权限</el-button
  16. >
  17. </div>
  18. <el-breadcrumb separator-class="el-icon-arrow-right" :class="'title'">
  19. <el-breadcrumb-item
  20. v-for="(str, index) in breadcrumbLayerArr"
  21. :key="index"
  22. :title="str"
  23. >{{ str }}
  24. </el-breadcrumb-item>
  25. </el-breadcrumb>
  26. <el-tree
  27. ref="layerTree"
  28. :data="layerList"
  29. show-checkbox
  30. node-key="id"
  31. :default-expanded-keys="[0, 1, 2, 3, 4, 5, 6]"
  32. @check="layerCheck"
  33. :props="defaultLayerListProps"
  34. :expand-on-click-node="false"
  35. >
  36. <span class="custom-tree-node" slot-scope="{ node, data }">
  37. <span>{{ node.label }}</span>
  38. <span v-if="node.level != 1">
  39. <el-button
  40. type="text"
  41. size="mini"
  42. @click="() => openSetRegion(data, node)"
  43. >设置行政区划权限
  44. </el-button>
  45. </span>
  46. </span>
  47. </el-tree>
  48. </div>
  49. <div class="list_container regionList" v-show="isShowRegionList">
  50. <el-breadcrumb separator-class="el-icon-arrow-right" :class="'title'">
  51. <el-breadcrumb-item
  52. v-for="(str, index) in breadcrumRegionbArr"
  53. :key="index"
  54. :title="str"
  55. >{{ str }}
  56. </el-breadcrumb-item>
  57. </el-breadcrumb>
  58. <el-tree
  59. ref="regionTree"
  60. :data="regionList"
  61. show-checkbox
  62. node-key="areaCode"
  63. :default-expanded-keys="[130000]"
  64. @check="regionCheck"
  65. :props="defaultRegionListProps"
  66. ></el-tree>
  67. </div>
  68. <div
  69. class="list_container batchRegionList"
  70. v-show="isShowBatchRegionList"
  71. >
  72. <el-breadcrumb separator-class="el-icon-arrow-right" :class="'title'">
  73. <el-breadcrumb-item>批量设置行政区划权限</el-breadcrumb-item>
  74. </el-breadcrumb>
  75. <el-tree
  76. ref="batchRegionTree"
  77. :data="regionList"
  78. show-checkbox
  79. node-key="areaCode"
  80. :default-expanded-keys="[130000]"
  81. @check="batchRegionCheck"
  82. :props="defaultRegionListProps"
  83. >
  84. </el-tree>
  85. </div>
  86. </div>
  87. <div class="footer">
  88. <div>
  89. <el-button @click="download">示例文档下载</el-button>
  90. <el-button @click="save">保存设置</el-button>
  91. <el-button @click="cancal">取消设置</el-button>
  92. </div>
  93. </div>
  94. </div>
  95. </template>
  96. <script>
  97. import api from "@/api/authManage";
  98. export default {
  99. props: {},
  100. data() {
  101. return {
  102. breadcrumbLayerArr: [""],
  103. breadcrumRegionbArr: [""],
  104. isShowLayerList: false,
  105. isShowRegionList: false,
  106. isShowBatchRegionList: false,
  107. userList: [],
  108. regionList: null,
  109. defaultLayerListProps: {
  110. children: "models",
  111. label: "title",
  112. },
  113. defaultRegionListProps: {
  114. children: "children",
  115. label: "areaName",
  116. },
  117. authInfo: {},
  118. nowLayerId: null,
  119. nowUserId: null,
  120. haveChange: false, // 是否做了修改
  121. exampleWord: systemConfig.exampleWord,
  122. };
  123. },
  124. created() {
  125. let that = this;
  126. $.getJSON("/static/config/hebei.json", function (result) {
  127. that.regionList = result;
  128. });
  129. this.getAllUserList();
  130. },
  131. mounted() {},
  132. methods: {
  133. download() {
  134. const elink = document.createElement("a");
  135. elink.href = this.exampleWord;
  136. elink.setAttribute("download", "接口示例文档.docx");
  137. elink.style.display = "none";
  138. document.body.appendChild(elink);
  139. setTimeout(() => {
  140. elink.click();
  141. document.body.removeChild(elink);
  142. }, 66);
  143. },
  144. // 获取所有用户
  145. getAllUserList() {
  146. let that = this;
  147. let obj = Object.assign({}, systemConfig.defaultUser);
  148. obj.active = false;
  149. this.userList.push(obj);
  150. api
  151. .getAllUser()
  152. .then((result) => {
  153. if (result.code == 200) {
  154. let arr = result.content;
  155. arr.unshift();
  156. that.userList.concat(
  157. arr.map(function (item) {
  158. item.active = false;
  159. return item;
  160. })
  161. );
  162. } else {
  163. this.$checkRequestCode(result);
  164. }
  165. })
  166. .catch((err) => {
  167. that.$message({
  168. type: "error",
  169. message: err,
  170. });
  171. });
  172. },
  173. // 获取单个用户的当前权限
  174. getSingleUserAuthInfo(userId) {
  175. let that = this;
  176. return new Promise((resolve, reject) => {
  177. api
  178. .getSingleUserAuth({ userId: userId })
  179. .then((result) => {
  180. if (result.code == 200) {
  181. resolve(result.content);
  182. } else {
  183. this.$checkRequestCode(result);
  184. reject();
  185. }
  186. })
  187. .catch((err) => {
  188. that.$message({
  189. type: "error",
  190. message: err,
  191. });
  192. reject();
  193. });
  194. });
  195. },
  196. // 保存当前用户用户权限
  197. saveNowUserAuth() {
  198. let that = this;
  199. let keysArr = Object.keys(this.authInfo);
  200. let authInfo = {};
  201. keysArr.map(function (key) {
  202. authInfo[key] = that.handleRegion(that.authInfo[key]);
  203. });
  204. return new Promise((resolve, reject) => {
  205. api
  206. .saveNowUserAuth({
  207. userId: that.nowUserId,
  208. contents: JSON.stringify(authInfo),
  209. })
  210. .then((result) => {
  211. if (result.code == 200) {
  212. that.authInfo = {};
  213. that.$message({
  214. type: "success",
  215. message: "用户权限保存成功!",
  216. });
  217. resolve(result.content);
  218. } else {
  219. this.$checkRequestCode(result);
  220. reject();
  221. }
  222. })
  223. .catch((err) => {
  224. that.$message({
  225. type: "error",
  226. message: err,
  227. });
  228. reject();
  229. });
  230. });
  231. },
  232. // 切换用户
  233. changeUser(item) {
  234. let that = this;
  235. if (this.haveChange) {
  236. // 是否保存当前用户的权限设置?
  237. this.$confirm("是否保存对当前用户权限的设置或改动?", "提示", {
  238. confirmButtonText: "确定",
  239. cancelButtonText: "取消",
  240. type: "warning",
  241. })
  242. .then(() => {
  243. // 转入保存逻辑
  244. that
  245. .saveNowUserAuth()
  246. .then(() => {
  247. that.getAuthAndShowLayer(item);
  248. })
  249. .catch((err) => {
  250. that
  251. .$confirm(err + ",是否继续切换用户?", "提示", {
  252. confirmButtonText: "确定",
  253. cancelButtonText: "取消",
  254. type: "warning",
  255. })
  256. .then(() => {
  257. that.getAuthAndShowLayer(item);
  258. })
  259. .catch(() => {});
  260. });
  261. })
  262. .catch(() => {
  263. that.getAuthAndShowLayer(item);
  264. });
  265. } else {
  266. this.getAuthAndShowLayer(item);
  267. }
  268. },
  269. // 获取当前用户权限,并 打开图层列表,关闭区划列表
  270. getAuthAndShowLayer(item) {
  271. let that = this;
  272. this.nowLayerId = null;
  273. this.nowUserId = null;
  274. this.haveChange = false;
  275. return new Promise((resolve, reject) => {
  276. this.getSingleUserAuthInfo(item.id)
  277. .then((content) => {
  278. that.authInfo = content == -1 ? {} : JSON.parse(content);
  279. that.$refs.layerTree.setCheckedKeys(Object.keys(that.authInfo)); // 设置选中图层
  280. that.isShowLayerList = true;
  281. that.isShowRegionList = false;
  282. that.isShowBatchRegionList = false;
  283. that.breadcrumbLayerArr = [item.username, "图层权限"];
  284. that.nowUserId = item.id;
  285. resolve();
  286. })
  287. .catch((err) => {
  288. reject();
  289. });
  290. });
  291. },
  292. // 勾选图层
  293. layerCheck(item, statusObj) {
  294. let that = this;
  295. this.haveChange = true;
  296. let obj = Object.assign({}, JSON.parse(JSON.stringify(this.authInfo)));
  297. this.authInfo = {};
  298. let layerSelectedArr = this.$refs.layerTree.getCheckedKeys(true);
  299. layerSelectedArr.map(function (layerId) {
  300. if (obj.hasOwnProperty(layerId)) {
  301. that.authInfo[layerId] = obj[layerId];
  302. } else {
  303. that.authInfo[layerId] = [];
  304. }
  305. });
  306. if (!this.nowLayerId) return;
  307. if (layerSelectedArr.indexOf(this.nowLayerId) == -1) {
  308. this.isShowRegionList = false;
  309. this.nowLayerId = null;
  310. }
  311. if (layerSelectedArr.length == 0) {
  312. this.isShowBatchRegionList = false;
  313. this.$refs.batchRegionTree.setCheckedKeys([]);
  314. }
  315. },
  316. // 打开 行政区划
  317. openSetRegion(data, node) {
  318. if (node.checked) {
  319. if (this.authInfo[data.id].length == 0) {
  320. this.$refs.regionTree.setCheckedKeys([]);
  321. } else {
  322. this.$refs.regionTree.setCheckedKeys(this.authInfo[data.id]);
  323. }
  324. } else {
  325. this.$message({
  326. type: "warning",
  327. message:
  328. "未设置当前用户有此图层的查看权限,不能设置当前图层的区划权限!",
  329. });
  330. return;
  331. }
  332. // title设置
  333. this.breadcrumRegionbArr = [].concat(this.breadcrumbLayerArr);
  334. this.breadcrumRegionbArr[1] = data.title;
  335. this.breadcrumRegionbArr.push("行政区划权限");
  336. this.isShowRegionList = true;
  337. this.isShowBatchRegionList = false;
  338. this.nowLayerId = data.id;
  339. },
  340. // 勾选区划 single
  341. regionCheck(item, statusObj) {
  342. this.haveChange = true;
  343. this.authInfo[this.nowLayerId] = statusObj.checkedKeys;
  344. },
  345. // 打开 批量设置行政区划模块
  346. openBatchSetRegion() {
  347. let that = this;
  348. let layerSelectedArr = this.$refs.layerTree.getCheckedKeys(true);
  349. this.$confirm(
  350. "使用批量设置行政区划权限功能会清空当前选中用户的所有已设置的行政区划权限,是否继续使用?",
  351. "提示",
  352. {
  353. confirmButtonText: "确定",
  354. cancelButtonText: "取消",
  355. type: "warning",
  356. }
  357. )
  358. .then(() => {
  359. if (layerSelectedArr.length == 0) {
  360. that.$message({
  361. type: "warning",
  362. message: "当前未有选择图层,无法使用批量设置行政区划权限功能。",
  363. });
  364. return;
  365. }
  366. this.isShowBatchRegionList = true;
  367. this.isShowRegionList = false;
  368. })
  369. .catch(() => {});
  370. },
  371. // 勾选区划 batch
  372. batchRegionCheck(item, statusObj) {
  373. let that = this;
  374. this.haveChange = true;
  375. let keys = Object.keys(this.authInfo);
  376. keys.map(function (layerId) {
  377. that.authInfo[layerId] = statusObj.checkedKeys;
  378. });
  379. },
  380. // 整理行政区划
  381. handleRegion(arr) {
  382. if (arr.length == 0) return [];
  383. let provinceArr = arr.filter(function (num1) {
  384. return num1 % 10000 == 0;
  385. });
  386. let cityArr = arr.filter(function (num2) {
  387. return num2 % 100 == 0;
  388. });
  389. let countryArr = arr.filter(function (num2) {
  390. return num2 % 100 != 0;
  391. });
  392. if (provinceArr.length != 0) {
  393. return provinceArr;
  394. } else {
  395. if (cityArr.length != 0) {
  396. let resetCountryArr = countryArr.filter(function (num3) {
  397. let integer = parseInt(num3 / 100) * 100;
  398. return !cityArr.some(function (num) {
  399. return num == integer;
  400. });
  401. });
  402. return cityArr.concat(resetCountryArr);
  403. } else {
  404. return countryArr;
  405. }
  406. }
  407. },
  408. // 解析行政区划
  409. analysisRegion(arr) {
  410. if (arr.length == 0) return [];
  411. let provinceArr = arr.filter(function (num1) {
  412. return num1 % 10000 == 0;
  413. });
  414. let cityArr = arr.filter(function (num2) {
  415. return num2 % 100 == 0;
  416. });
  417. let countryArr = arr.filter(function (num2) {
  418. return num2 % 100 != 0;
  419. });
  420. if (provinceArr.length != 0) {
  421. return provinceArr;
  422. } else {
  423. if (cityArr.length != 0) {
  424. let resetCountryArr = countryArr.filter(function (num3) {
  425. let integer = parseInt(num3 / 100) * 100;
  426. return !cityArr.some(function (num) {
  427. return num == integer;
  428. });
  429. });
  430. return cityArr.concat(resetCountryArr);
  431. } else {
  432. return countryArr;
  433. }
  434. }
  435. },
  436. // 保存设置
  437. save() {
  438. let that = this;
  439. this.saveNowUserAuth()
  440. .then((result) => {
  441. that.haveChange = false;
  442. that.isShowLayerList = false;
  443. that.isShowRegionList = false;
  444. that.isShowBatchRegionList = false;
  445. })
  446. .catch((err) => {});
  447. },
  448. // 取消设置
  449. cancal() {
  450. this.authInfo = {};
  451. this.breadcrumbLayerArr = [""];
  452. this.breadcrumRegionbArr = [""];
  453. this.nowLayerId = null;
  454. this.nowUserId = null;
  455. this.haveChange = false;
  456. this.isShowLayerList = false;
  457. this.isShowRegionList = false;
  458. this.isShowBatchRegionList = false;
  459. },
  460. },
  461. computed: {
  462. layerList() {
  463. return this.$store.state.layerList.map(function (item) {
  464. item.title = item.name;
  465. return item;
  466. });
  467. },
  468. },
  469. watch: {
  470. isShowRegionList(newVal, oldVal) {
  471. if (!newVal) {
  472. this.$refs.regionTree.setCheckedKeys([]);
  473. }
  474. },
  475. isShowBatchRegionList(newVal, oldVal) {
  476. if (newVal) {
  477. for (const key in this.authInfo) {
  478. if (Object.hasOwnProperty.call(this.authInfo, key)) {
  479. this.authInfo[key] = [];
  480. }
  481. }
  482. }
  483. },
  484. // layerSelectedArr(newArr, oldArr) {
  485. // if (newArr.length == 0) {
  486. // this.isShowBatchRegionList = false;
  487. // this.$refs.batchRegionTree.setCheckedKeys([]);
  488. // }
  489. // }
  490. },
  491. };
  492. </script>
  493. <style lang="less" scoped>
  494. .authContainer {
  495. width: 100%;
  496. height: 100%;
  497. background: #ffffff;
  498. font-family: "Arial Normal", "Arial";
  499. font-weight: 400;
  500. font-style: normal;
  501. color: #aaaaaa;
  502. position: relative;
  503. .content {
  504. height: calc(100% - 60px);
  505. .list_container {
  506. height: calc(100% - 40px);
  507. width: calc(100% / 3 - 40px);
  508. float: left;
  509. overflow: hidden;
  510. overflow-y: auto;
  511. margin: 20px 20px;
  512. border: 1px solid #eeeeee;
  513. border-radius: 10px;
  514. box-sizing: border-box;
  515. position: relative;
  516. &.userList {
  517. .el-row {
  518. height: 40px;
  519. line-height: 40px;
  520. color: #000000;
  521. margin: 0 10px;
  522. padding-left: 10px;
  523. border-bottom: 1px solid #eeeeee;
  524. font-size: 16px;
  525. &:hover {
  526. background-color: #eeeeee;
  527. cursor: pointer;
  528. }
  529. &.active {
  530. background-color: #eeeeee;
  531. }
  532. }
  533. }
  534. .custom-tree-node {
  535. flex: 1;
  536. display: flex;
  537. align-items: center;
  538. justify-content: space-between;
  539. font-size: 14px;
  540. padding-right: 8px;
  541. }
  542. .title {
  543. height: 40px;
  544. line-height: 40px;
  545. margin: 0 10px;
  546. border-bottom: 1px solid #eeeeee;
  547. box-sizing: border-box;
  548. font-size: 18px;
  549. color: #000000;
  550. /deep/ .el-breadcrumb__inner {
  551. font-weight: bold !important;
  552. }
  553. /deep/ .el-breadcrumb__item {
  554. float: unset;
  555. &:nth-child(1) > .el-breadcrumb__inner {
  556. max-width: 120px;
  557. }
  558. &:nth-child(2) > .el-breadcrumb__inner {
  559. max-width: 180px;
  560. }
  561. > .el-breadcrumb__inner {
  562. display: inline-block;
  563. white-space: nowrap;
  564. overflow: hidden;
  565. text-overflow: ellipsis;
  566. cursor: pointer;
  567. }
  568. > i {
  569. display: inline-block;
  570. vertical-align: top;
  571. margin-top: 11px;
  572. }
  573. &:last-child .el-breadcrumb__separator {
  574. display: none;
  575. }
  576. }
  577. }
  578. &.batchRegionList {
  579. .title {
  580. /deep/ .el-breadcrumb__item {
  581. &:nth-child(1) > .el-breadcrumb__inner {
  582. max-width: 200px;
  583. }
  584. }
  585. }
  586. }
  587. .batchSetRegion {
  588. position: absolute;
  589. top: 0px;
  590. right: 7px;
  591. }
  592. }
  593. }
  594. .footer {
  595. box-sizing: border-box;
  596. padding: 0 20px;
  597. > div {
  598. float: right;
  599. }
  600. }
  601. }
  602. </style>