auth.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <template>
  2. <div class="auth scroll-y">
  3. <div class="flex auth-content">
  4. <div class="box role-info auth-list">
  5. <div class="manageTitle">角色信息</div>
  6. <div class="role-info-content t30">
  7. <el-form ref="ruleFormRef" :model="ruleForm" class="demo-ruleForm">
  8. <el-form-item label="角色名称" prop="role_name">
  9. <el-input disabled v-model="ruleForm.role_name" size="default" placeholder="请输入角色名称" />
  10. </el-form-item>
  11. <el-form-item label="角色描述" prop="role_comment">
  12. <el-input disabled v-model="ruleForm.role_comment" size="default" type="textarea" :rows="3" placeholder="请输入角色描述" />
  13. </el-form-item>
  14. <el-form-item label="是否启用" prop="role_status">
  15. <el-radio-group disabled v-model="ruleForm.role_status">
  16. <el-radio :label="1">启用</el-radio>
  17. <el-radio :label="2"> 禁用</el-radio>
  18. </el-radio-group>
  19. </el-form-item>
  20. </el-form>
  21. </div>
  22. </div>
  23. <div class="box auth-list">
  24. <div class="manageTitle">权限树</div>
  25. <div class="t30 auth-tree">
  26. <el-scrollbar>
  27. <el-tree ref="treeRef" highlight-current @check="currentChange" :default-expanded-keys="checkKeys" :data="data" show-checkbox :props="defaultProps" node-key="auth_id" @node-click="handleNodeClick">
  28. <template #default="{data}">
  29. <span class="custom-tree-node">
  30. {{ data.auth_name }}
  31. </span>
  32. <span v-if="
  33. data.edit_col_condition ||
  34. data.delete_row_condition ||
  35. data.edit_row_condition ||
  36. data.new_col_condition ||
  37. data.query_col_conditon ||
  38. data.query_row_condition" class="custom-tree-icon"></span>
  39. </template>
  40. </el-tree>
  41. </el-scrollbar>
  42. </div>
  43. </div>
  44. <div class="box auth-list">
  45. <div class="flex">
  46. <div class="manageTitle">权限规则</div>
  47. <el-radio-group v-model="auth">
  48. <el-radio label="1">显示权限</el-radio>
  49. <el-radio label="2">显示及编辑权限</el-radio>
  50. </el-radio-group>
  51. </div>
  52. <div class="t30">
  53. <el-form :model="form" class="demo-ruleForm">
  54. <el-form-item label="许可查询行">
  55. <el-input size="default" v-model="form.query_row_condition" placeholder="请输入内容"></el-input>
  56. </el-form-item>
  57. <el-form-item label="许可查询列">
  58. <el-input size="default" v-model="form.query_col_conditon" placeholder="请输入内容"></el-input>
  59. </el-form-item>
  60. <el-form-item label="许可删除行">
  61. <el-input size="default" v-model="form.delete_row_condition" placeholder="请输入内容"></el-input>
  62. </el-form-item>
  63. <el-form-item label="许可新增列">
  64. <el-input size="default" v-model="form.new_col_condition" placeholder="请输入内容"></el-input>
  65. </el-form-item>
  66. <el-form-item label="许可编辑行">
  67. <el-input size="default" v-model="form.edit_row_condition" placeholder="请输入内容"></el-input>
  68. </el-form-item>
  69. <el-form-item label="许可编辑列">
  70. <el-input size="default" v-model="form.edit_col_condition" placeholder="请输入内容"></el-input>
  71. </el-form-item>
  72. </el-form>
  73. </div>
  74. </div>
  75. </div>
  76. </div>
  77. </template>
  78. <script setup lang="ts">
  79. import { ref, onBeforeMount, watch } from "vue";
  80. import { Query, GeneralDataReception } from "@/api/dataIntegration";
  81. import { translateDataToTreeAll } from "@/utils/validate";
  82. import { ElMessage, ElTree } from "element-plus";
  83. import tree from "../hooks/useTree";
  84. import * as _ from "lodash";
  85. const route = useRoute();
  86. const role_id = route.query.role_id;
  87. const ruleForm = ref({
  88. role_name: "",
  89. role_comment: "",
  90. role_status: "",
  91. });
  92. const auth = ref("1");
  93. const form = ref({
  94. query_row_condition: "",
  95. query_col_conditon: "",
  96. delete_row_condition: "",
  97. new_col_condition: "",
  98. edit_row_condition: "",
  99. edit_col_condition: "",
  100. });
  101. const maxReq = ref(20);
  102. const proAll = ref<any>([]);
  103. const proNum = ref(0);
  104. const checkObj = ref<any>(null);
  105. const data = ref<any>([]);
  106. const newData = ref<any>([]);
  107. const checkData = ref<any>([]);
  108. const checkKeys = ref<any>([-1]);
  109. const boxData = ref<any>([]);
  110. const treeRef = ref<InstanceType<typeof ElTree>>();
  111. onBeforeMount(() => {
  112. roleDetails();
  113. getTree();
  114. });
  115. watch(
  116. () => form,
  117. (newValue) => {
  118. newData.value.forEach((item) => {
  119. if (item.children) {
  120. delete item.children;
  121. }
  122. if (item.auth_id == checkObj.value.auth_id) {
  123. item = Object.assign(item, newValue.value);
  124. }
  125. });
  126. checkKeys.value = [checkObj.value.auth_id];
  127. toTree();
  128. },
  129. { deep: true }
  130. );
  131. //角色详情
  132. const roleDetails = async () => {
  133. try {
  134. const { code, message, returnData } = await Query({
  135. id: DATACONTENT_ID.roleDetailsId,
  136. dataContent: [role_id],
  137. });
  138. if (code == 0) {
  139. ruleForm.value = returnData.listValues[0];
  140. } else {
  141. ElMessage.error(message);
  142. }
  143. } catch (error) {}
  144. };
  145. const toTree = () => {
  146. data.value = [];
  147. const chks = treeRef.value!.getCheckedNodes(false, true);
  148. const arr = translateDataToTreeAll(newData.value, "up_auth_id", "auth_id");
  149. const items = {
  150. auth_id: -1,
  151. auth_name: "所有权限",
  152. auth_status: 0,
  153. up_auth_id: -2,
  154. auth_type: 0,
  155. children: arr,
  156. disabled: true,
  157. };
  158. data.value = [items];
  159. if (chks && chks.length) {
  160. checkBox(chks, newData.value);
  161. }
  162. };
  163. //权限树
  164. const getTree = async () => {
  165. try {
  166. const { returnData, code } = await Query({
  167. id: DATACONTENT_ID.authTreeNewId,
  168. dataContent: [sessionStorage.getItem("User_Id")],
  169. });
  170. if (code == 0 && returnData.listValues) {
  171. newData.value = _.cloneDeep(returnData.listValues);
  172. toTree();
  173. roleAuths();
  174. } else {
  175. }
  176. } catch (error) {}
  177. // const res = await tree.getTree();
  178. // newData.value = await tree.newData.value;
  179. // data.value = res.value;
  180. };
  181. const checkBox = (arr, all) => {
  182. const checks: any = [];
  183. const nots = _.cloneDeep(arr);
  184. const caps = _.cloneDeep(all);
  185. arr.forEach((item) => {
  186. checks.push(item.auth_id);
  187. });
  188. const res = [...caps, ...nots].filter(
  189. (item) =>
  190. !(
  191. caps.some((p) => item.auth_id == p.auth_id) &&
  192. nots.some((c) => item.auth_id == c.auth_id)
  193. )
  194. );
  195. treeRef.value!.setCheckedKeys(checks);
  196. setTimeout(() => {
  197. res.forEach((item) => {
  198. treeRef.value!.setChecked(item.auth_id, false, false);
  199. });
  200. }, 0);
  201. };
  202. //权限详情
  203. const roleAuths = async () => {
  204. try {
  205. const { code, message, returnData } = await Query({
  206. id: DATACONTENT_ID.roleAuthId,
  207. dataContent: [role_id],
  208. });
  209. if (code == 0 && returnData.listValues) {
  210. boxData.value.push(returnData.listValues);
  211. checkBox(returnData.listValues, newData.value);
  212. } else {
  213. ElMessage.error(message);
  214. }
  215. } catch (error) {}
  216. };
  217. const handleNodeClick = (data) => {
  218. checkObj.value = data;
  219. checkData.value.push(_.cloneDeep(data));
  220. const datas = _.unionBy(checkData.value, "auth_id");
  221. checkData.value = datas;
  222. const checkNode = checkData.value.filter(
  223. (item) => item.auth_id == data.auth_id
  224. );
  225. if (checkNode.length) {
  226. const formObj = checkNode[0];
  227. form.value = formObj;
  228. } else {
  229. clearFormData();
  230. }
  231. };
  232. const clearFormData = () => {
  233. form.value.query_row_condition = "";
  234. form.value.query_col_conditon = "";
  235. form.value.delete_row_condition = "";
  236. form.value.new_col_condition = "";
  237. form.value.edit_row_condition = "";
  238. form.value.edit_col_condition = "";
  239. };
  240. const currentChange = () => {
  241. const datas = treeRef.value!.getCheckedNodes(false, true);
  242. datas.forEach((item, index) => {
  243. if (item.auth_id == -1) {
  244. datas.splice(index, 1);
  245. }
  246. });
  247. boxData.value.push(datas);
  248. const data1 = boxData.value[boxData.value.length - 1]; //最后一条数据
  249. const data2 = boxData.value[boxData.value.length - 2]; //倒数第二条数据
  250. if (data1.length > data2.length) {
  251. checksBoxTs(data1, data2, "add");
  252. } else {
  253. checksBoxTs(data2, data1, "del");
  254. }
  255. };
  256. const checksBoxTs = async (datas, arr, type) => {
  257. const res = [...datas, ...arr].filter(
  258. (item) =>
  259. !(
  260. datas.some((p) => item.auth_id == p.auth_id) &&
  261. arr.some((c) => item.auth_id == c.auth_id)
  262. )
  263. );
  264. const [msg1, msg2] = [<any>[], <any>[]];
  265. if (res && res.length) {
  266. for (let i = 0; i < res.length; i++) {
  267. //delete res[i].children;
  268. const cap = _.cloneDeep(res[i]);
  269. if (cap.children) {
  270. delete cap.children;
  271. }
  272. let obj = <any>{
  273. auth_id: cap.auth_id || cap.AuthId,
  274. role_id: role_id,
  275. };
  276. let authObj = cap.AuthList
  277. ? Object.assign(_.cloneDeep(obj), cap.AuthList)
  278. : Object.assign(_.cloneDeep(obj), cap);
  279. if (type == "add") {
  280. obj.event = 1;
  281. authObj.event = 1;
  282. msg1.push(obj);
  283. msg2.push(authObj);
  284. } else {
  285. obj.event = 3;
  286. authObj.event = 3;
  287. msg1.push(obj);
  288. msg2.push(authObj);
  289. }
  290. }
  291. GeneralDataReception({
  292. serviceId: SERVICE_ID.roleMsgTreeId,
  293. dataContent: JSON.stringify(msg1),
  294. }).then((result) => {
  295. if (result.code == 0) {
  296. if (type == "add") {
  297. ElMessage.success("操作权限成功");
  298. } else {
  299. ElMessage.success("操作权限成功");
  300. }
  301. } else {
  302. if (type == "add") {
  303. ElMessage.error("操作权限失败");
  304. } else {
  305. ElMessage.error("操作权限失败");
  306. }
  307. }
  308. });
  309. if (msg2.length > maxReq.value) {
  310. foxFunc(msg2);
  311. } else {
  312. GeneralDataReception({
  313. serviceId: SERVICE_ID.roleMsgAuthId,
  314. dataContent: JSON.stringify(msg2),
  315. }).then((result) => {
  316. if (result.code == 0) {
  317. if (type == "add") {
  318. ElMessage.success("操作规则成功");
  319. } else {
  320. ElMessage.success("操作规则成功");
  321. }
  322. } else {
  323. if (type == "add") {
  324. ElMessage.error("操作规则失败");
  325. } else {
  326. ElMessage.error("操作规则失败");
  327. }
  328. }
  329. });
  330. }
  331. } else {
  332. }
  333. };
  334. const foxFunc = async (arr?: Array<any>) => {
  335. if (proAll.value.length == 0) {
  336. proAll.value = _.cloneDeep(arr);
  337. }
  338. const limit = _.chunk(proAll.value, maxReq.value);
  339. const reqUts = [];
  340. limit.forEach((item, index) => {
  341. const req = GeneralDataReception({
  342. serviceId: SERVICE_ID.roleMsgAuthId,
  343. dataContent: JSON.stringify(limit[index]),
  344. });
  345. reqUts.push(req);
  346. });
  347. const result = Promise.all(reqUts);
  348. const resultLen = await result;
  349. if (limit.length == resultLen.length) {
  350. proAll.value = [];
  351. proNum.value = 0;
  352. ElMessage.success("操作规则成功");
  353. } else {
  354. proNum.value += 1;
  355. if (proNum.value == 4) {
  356. ElMessage.success("网络错误,请稍后重试");
  357. } else {
  358. foxFunc();
  359. }
  360. }
  361. };
  362. const defaultProps = {
  363. children: "children",
  364. label: "auth_name",
  365. };
  366. </script>
  367. <style lang="scss" scoped>
  368. .auth {
  369. &-content {
  370. height: 100%;
  371. }
  372. &-list {
  373. flex: 1;
  374. margin-right: 24px;
  375. &:last-child {
  376. margin-right: 0;
  377. }
  378. :deep .el-tree {
  379. .custom-tree-icon {
  380. position: absolute;
  381. right: 0;
  382. width: 14px;
  383. height: 14px;
  384. background: url("@/assets/index/ic_date.png") no-repeat;
  385. background-size: 100% 100%;
  386. }
  387. }
  388. .auth-tree {
  389. height: calc(100% - 60px);
  390. }
  391. }
  392. }
  393. </style>