page-role.component.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. import { Component, OnInit, ViewChild } from '@angular/core';
  2. import { ActivatedRoute, Route, Router, RouterOutlet } from '@angular/router';
  3. import { CompTableListComponent } from '../../../app/comp-table/comp-table-list/comp-table-list.component';
  4. import _Role from '../../../schemas/_Role';
  5. // import { TranslateService } from '@ngx-translate/core';
  6. import * as Parse from 'parse';
  7. import { CommonModule } from '@angular/common';
  8. import { Department } from '../../../schemas/Department';
  9. import { NzSpaceModule } from 'ng-zorro-antd/space';
  10. import { NzPageHeaderModule } from 'ng-zorro-antd/page-header';
  11. import { NzBreadCrumbModule } from 'ng-zorro-antd/breadcrumb';
  12. import { CommonCompModule } from '../../../services/common.modules';
  13. import { NzModalModule, NzModalService } from 'ng-zorro-antd/modal';
  14. import {
  15. NzFormatEmitEvent,
  16. NzTreeModule,
  17. NzTreeNode,
  18. } from 'ng-zorro-antd/tree';
  19. import {
  20. NzContextMenuService,
  21. NzDropdownMenuComponent,
  22. } from 'ng-zorro-antd/dropdown';
  23. import { NzCheckboxModule } from 'ng-zorro-antd/checkbox';
  24. import { NzEmptyModule } from 'ng-zorro-antd/empty';
  25. import { NzRadioModule } from 'ng-zorro-antd/radio';
  26. import { NzMessageService } from 'ng-zorro-antd/message';
  27. interface nodes {
  28. title: string;
  29. key: string;
  30. isLeaf?: boolean;
  31. branch?: string;
  32. children?: Array<any>;
  33. }
  34. interface depart {
  35. name: string;
  36. id?: string;
  37. code?: string;
  38. desc?: string;
  39. parent?: object | any;
  40. branch: string;
  41. }
  42. @Component({
  43. selector: 'app-page-role',
  44. templateUrl: './page-role.component.html',
  45. styleUrls: ['./page-role.component.scss'],
  46. imports: [
  47. CommonModule,
  48. CommonCompModule,
  49. RouterOutlet,
  50. CompTableListComponent,
  51. NzSpaceModule,
  52. NzPageHeaderModule,
  53. NzBreadCrumbModule,
  54. NzTreeModule,
  55. NzCheckboxModule,
  56. NzEmptyModule,
  57. NzModalModule,
  58. NzRadioModule,
  59. ],
  60. standalone: true,
  61. })
  62. export class PageRoleComponent implements OnInit {
  63. @ViewChild(CompTableListComponent) list: CompTableListComponent | undefined;
  64. // _Role = _Role
  65. Department = Department;
  66. user: Parse.User | undefined;
  67. className: string | undefined;
  68. queryParams: any | undefined;
  69. fieldsArray: Array<any> | undefined;
  70. searchValue: string = ''; //搜索内容
  71. searchValueNode: string = '';
  72. nodes: Array<nodes | any> = [];
  73. currentDepart: nodes | any = null;
  74. profiles: Array<any> = [];
  75. checkedShowFilter: boolean = false;
  76. checkedAll: boolean = false; //全选
  77. indeterminate = false;
  78. loading = false;
  79. isVisible: boolean = false;
  80. activatedNode: NzTreeNode | any; //当前选择节点
  81. editType: string = 'add'; //弹窗类型
  82. activeDepart?: Parse.Object; //当前编辑部门
  83. editObject: depart = {
  84. name: '',
  85. code: '',
  86. desc: '',
  87. parent: {},
  88. branch: '',
  89. };
  90. parentMap: Array<any> = [];
  91. parentList: Array<any> = [];
  92. radio: string = '';
  93. /* 添加账号 */
  94. accountIsVisible: boolean = false;
  95. account: any = {
  96. name: '',
  97. phone: '',
  98. email: '',
  99. password: '',
  100. identity: '',
  101. department: '',
  102. companyType: '',
  103. };
  104. constructor(
  105. private route: Router,
  106. private activeRoute: ActivatedRoute,
  107. private nzContextMenuService: NzContextMenuService,
  108. private modal: NzModalService,
  109. private message: NzMessageService
  110. ) {
  111. this.user = Parse.User.current();
  112. this.className = this.Department.className;
  113. this.fieldsArray = this.Department.fieldsArray;
  114. this.queryParams = {
  115. where: {
  116. // user:this.user?.toPointer(),
  117. isDeleted: { $ne: true },
  118. },
  119. };
  120. }
  121. ngOnInit(): void {
  122. this.activeRoute.paramMap.subscribe(async (params) => {
  123. // let isDeleted = params.get('isDeleted');
  124. // if (isDeleted) {
  125. // this.queryParams.where['isDeleted'] = { $eq: true };
  126. // } else {
  127. // this.queryParams.where['isDeleted'] = { $ne: true };
  128. // }
  129. // this.list?.ngOnInit();
  130. this.nodes = await this.getDepart();
  131. });
  132. }
  133. async getDepart(
  134. parent?: string,
  135. searchValue?: string,
  136. filter?: boolean
  137. ): Promise<Array<nodes>> {
  138. let nodes: any = [];
  139. let query = new Parse.Query('Department');
  140. if (!filter) {
  141. query.equalTo('parent', parent ? parent : undefined);
  142. }
  143. searchValue && query.contains('name', searchValue);
  144. query.notEqualTo('isDeleted', true);
  145. query.select('code', 'name', 'branch', 'parent', 'type');
  146. query.descending('createdAt');
  147. query.limit(2000);
  148. if (this.activeDepart) query.notEqualTo('objectId', this.activeDepart?.id);
  149. let res = await query.find();
  150. res.forEach((item) => {
  151. nodes.push({
  152. title: item.get('name'),
  153. key: item.id,
  154. children: [],
  155. branch: item.get('branch'),
  156. isLeaf: item.get('type') == '单位' ? true : false, //是否是最下级
  157. });
  158. });
  159. return nodes;
  160. }
  161. //搜索
  162. async onSearchNodes(e: string, modal?: boolean) {
  163. if (modal) {
  164. this.parentList = await this.getDepart('', e, true);
  165. return;
  166. }
  167. this.nodes = await this.getDepart('', e, e ? true : false);
  168. }
  169. //添加成员
  170. addMember() {
  171. // this.message.warning('权限灰度中')
  172. this.account = {
  173. name: '',
  174. phone: '',
  175. email: '',
  176. password: '',
  177. identity: '',
  178. department: '',
  179. companyType: '',
  180. };
  181. this.accountIsVisible = true;
  182. }
  183. //展开/合并
  184. async nzEvent(event: NzFormatEmitEvent): Promise<void> {
  185. console.log(event);
  186. let node: any = event.node;
  187. if (event.eventName === 'expand') {
  188. // if (node.origin.isParent) {
  189. // node.addChildren([]);
  190. // return;
  191. // }
  192. if (node?._children.length <= 0) {
  193. let data = await this.getDepart(node.key);
  194. node.addChildren(data);
  195. }
  196. console.log(this.nodes);
  197. } else {
  198. // if (node.origin.isParent) {
  199. this.currentDepart = node.origin;
  200. this.getProfile();
  201. // }
  202. }
  203. }
  204. async getProfile() {
  205. this.profiles = [];
  206. this.loading = true;
  207. let queryParams = {
  208. where: {
  209. $or: [
  210. {
  211. user: {
  212. $inQuery: {
  213. where: {
  214. $or: [
  215. {
  216. department: { $eq: this.currentDepart.key },
  217. },
  218. ],
  219. },
  220. className: '_User',
  221. },
  222. },
  223. },
  224. ],
  225. },
  226. };
  227. let query = Parse.Query.fromJSON('Profile', queryParams);
  228. query.include('user');
  229. query.notEqualTo('identity', '国家级管理员');
  230. query.notEqualTo('isDeleted', true);
  231. let r = await query.find();
  232. let profiles: any[] = [];
  233. r.forEach((item) => {
  234. let _item = item.toJSON();
  235. _item['checked'] = false;
  236. profiles.push(_item);
  237. });
  238. this.profiles = profiles;
  239. this.loading = false;
  240. }
  241. //搜索触发
  242. onSearch(event: NzFormatEmitEvent) {
  243. console.log(event);
  244. }
  245. contextMenu(
  246. $event: MouseEvent,
  247. menu: NzDropdownMenuComponent,
  248. node?: any
  249. ): void {
  250. console.log(node);
  251. this.activatedNode = node;
  252. this.nzContextMenuService.create($event, menu);
  253. }
  254. //删除部门
  255. async onDelDepart() {
  256. this.modal.confirm({
  257. nzTitle: '删除',
  258. nzContent:'删除后数据不可恢复,请谨慎操作',
  259. nzOkText: '确认',
  260. nzOkType: 'primary',
  261. nzOkDanger: true,
  262. nzOnOk: async () => {
  263. new Promise(async(resolve, reject) => {
  264. if(this.activatedNode){
  265. let query = new Parse.Query('Department');
  266. let r = await query.get(this.activatedNode?.key);
  267. if(r?.id){
  268. r.set('isDeleted',true)
  269. await r.save()
  270. }
  271. this.message.success('删除成功')
  272. this.nodes = await this.getDepart();
  273. resolve(true)
  274. }
  275. }).catch(() => console.log('Oops errors!'))
  276. },
  277. nzCancelText: '取消',
  278. nzOnCancel: () => console.log('Cancel'),
  279. });
  280. }
  281. onAllChecked(checked: boolean): void {
  282. console.log(checked);
  283. this.profiles = this.profiles.map((item) => {
  284. item.checked = checked;
  285. return item;
  286. });
  287. this.checkedAll = checked;
  288. }
  289. onItemChecked(id: string, e: boolean) {
  290. let checkedAll = true;
  291. this.profiles = this.profiles.map((item) => {
  292. if (id == item.objectId) item.checked = e;
  293. if (!item.checked) checkedAll = false;
  294. return item;
  295. });
  296. this.checkedAll = checkedAll;
  297. }
  298. //新建打开弹窗
  299. async showModalDepart(type: string) {
  300. this.parentList = this.nodes;
  301. this.editObject = {
  302. name: '',
  303. code: '',
  304. desc: '',
  305. parent: '',
  306. branch: '',
  307. };
  308. if (type == 'edit') {
  309. let query = new Parse.Query('Department');
  310. let r = await query.get(this.activatedNode?.key);
  311. this.activeDepart = r;
  312. this.editObject = {
  313. name: this.activeDepart.get('name'),
  314. code: this.activeDepart.get('code'),
  315. desc: this.activeDepart.get('desc'),
  316. parent: {
  317. title: this.activeDepart.get('parent')?.get('name'),
  318. id: this.activeDepart.get('parent')?.id,
  319. },
  320. branch: this.activeDepart.get('branch'),
  321. };
  322. this.parentMap = await this.formatNode(this.activeDepart.get('parent')?.id);
  323. if (this.activatedNode?.parentNode?.origin?.parentNode?.origin.key) {
  324. this.parentList = await this.getDepart(
  325. this.activatedNode?.parentNode?.origin?.parentNode?.origin.key
  326. );
  327. }
  328. }else if(type == 'add' && this.activatedNode){
  329. console.log(this.activatedNode);
  330. this.editObject.parent = {
  331. title: this.activatedNode.origin.title,
  332. id: this.activatedNode.origin.key,
  333. };
  334. this.editObject.branch = this.activatedNode.origin.branch || this.activatedNode.origin.title
  335. this.parentMap = await this.formatNode(this.activatedNode.origin.key);
  336. if (this.activatedNode?.parentNode?.origin.key) {
  337. this.parentList = await this.getDepart(
  338. this.activatedNode.parentNode.origin.key
  339. );
  340. }
  341. }
  342. console.log(this.parentMap);
  343. this.editType = type;
  344. this.isVisible = true;
  345. }
  346. //格式化链
  347. async formatNode(id: string): Promise<Array<any>> {
  348. let query = new Parse.Query('Department');
  349. query.select('name', 'parent');
  350. let r = await query.get(id);
  351. let arr = [
  352. {
  353. title: r.get('name'),
  354. key: r.id,
  355. },
  356. ];
  357. if (r?.get('parent')) {
  358. arr.unshift(...(await this.formatNode(r?.get('parent').id)));
  359. }
  360. return arr;
  361. }
  362. async onPre(data?: any, index?: number) {
  363. if (!data) {
  364. this.parentList = await this.getDepart();
  365. this.parentMap = [];
  366. return;
  367. }
  368. if (index == this.parentMap.length - 1) return;
  369. this.parentMap.splice(index || 0 + 1);
  370. this.parentList = await this.getDepart(data?.key);
  371. }
  372. //选择所属类别下级列表
  373. async onCheckedDepart(e: any, checked?: boolean) {
  374. this.radio = e.key;
  375. console.log(this.radio);
  376. this.parentMap = await this.formatNode(e.key);
  377. if (checked) {
  378. // this.editObject.name = e.title
  379. this.editObject.parent = {
  380. title: e.title,
  381. id: e.key,
  382. };
  383. this.editObject.branch = e.branch || e.title;
  384. return;
  385. }
  386. if (e.isLeaf) {
  387. return;
  388. }
  389. this.parentMap.push({
  390. title: e.title,
  391. key: e.key,
  392. });
  393. this.parentList = await this.getDepart(e?.key);
  394. }
  395. async handleOk():Promise<void> {
  396. if(!this.editObject?.name || !this.editObject.parent?.id){
  397. this.message.error('请填写完整信息')
  398. return
  399. }
  400. if(this.activeDepart?.id && this.editType == 'edit'){
  401. this.activeDepart.set('name',this.editObject?.name)
  402. this.activeDepart.set('code',this.editObject?.code)
  403. this.activeDepart.set('desc',this.editObject.desc)
  404. this.activeDepart.set('parent',{
  405. __type: 'Pointer',
  406. className: 'Department',
  407. objectId: this.editObject.parent?.id,
  408. })
  409. this.activeDepart.set('branch',this.editObject.branch)
  410. }else{
  411. let obj = Parse.Object.extend('Department')
  412. this.activeDepart = new obj()
  413. this.activeDepart?.set('name',this.editObject?.name)
  414. this.activeDepart?.set('code',this.editObject?.code)
  415. this.activeDepart?.set('desc',this.editObject.desc)
  416. this.activeDepart?.set('parent',{
  417. __type: 'Pointer',
  418. className: 'Department',
  419. objectId: this.editObject.parent?.id,
  420. })
  421. this.activeDepart?.set('branch',this.editObject.branch)
  422. }
  423. await this.activeDepart?.save()
  424. this.isVisible = false;
  425. this.message.success(this.editType == 'edit' ? '保存' : '添加' + '成功')
  426. this.activeDepart = undefined
  427. this.nodes = await this.getDepart();
  428. }
  429. handleCancel(): void {
  430. console.log('Button cancel clicked!');
  431. this.isVisible = false;
  432. this.activatedNode = undefined;
  433. this.parentMap = [];
  434. this.accountIsVisible = false;
  435. this.account = null;
  436. }
  437. /* 组织 */
  438. showModalOrganize() {
  439. this.message.warning('权限灰度中');
  440. }
  441. goDateil(id: string) {
  442. this.route.navigate(['/nav-admin/manage/user/edit', { id: id }]);
  443. }
  444. }