user-edit.component.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { Component, Input, OnInit } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { NzSpaceModule } from 'ng-zorro-antd/space';
  4. import { CommonCompModule } from '../../../services/common.modules';
  5. import { NzTabsModule } from 'ng-zorro-antd/tabs';
  6. import { ActivatedRoute, Router } from '@angular/router';
  7. import { NzAvatarModule } from 'ng-zorro-antd/avatar';
  8. import { NzDropDownModule } from 'ng-zorro-antd/dropdown';
  9. import { NzPopoverModule } from 'ng-zorro-antd/popover';
  10. import { NzTagModule } from 'ng-zorro-antd/tag';
  11. import { NzModalModule } from 'ng-zorro-antd/modal';
  12. import { NzMessageService } from 'ng-zorro-antd/message';
  13. import { textbookServer } from '../../../services/textbook';
  14. import { NzRadioModule } from 'ng-zorro-antd/radio';
  15. import Parse from 'parse';
  16. @Component({
  17. selector: 'app-user-edit',
  18. templateUrl: './user-edit.component.html',
  19. styleUrls: ['./user-edit.component.scss'],
  20. imports: [
  21. CommonModule,
  22. NzSpaceModule,
  23. CommonCompModule,
  24. NzTabsModule,
  25. NzAvatarModule,
  26. NzDropDownModule,
  27. NzPopoverModule,
  28. NzTagModule,
  29. NzModalModule,
  30. NzRadioModule,
  31. ],
  32. standalone: true,
  33. })
  34. export class UserEditComponent implements OnInit {
  35. inputValue: string = `
  36. async function pipe(user, context, callback) {
  37. if (context.connection === "weibo") {
  38. return callback(new Error("当前系统禁止使用微博登录!"))
  39. }
  40. callback(null, user, context)
  41. }`;
  42. isVisible: boolean = false;
  43. user: Parse.Object | any;
  44. profile: Parse.Object | any;
  45. password: string = '';
  46. edit: boolean = false; //编辑权限
  47. //可编辑
  48. userDataJson: any = {
  49. companyType: '',
  50. department: null,
  51. };
  52. parentMap: Array<any> = [];
  53. parentList: Array<any> = []; //单位列表
  54. isShowModal: boolean = false;
  55. searchValue: string = ''; //搜索部门内容
  56. unitTypes: Array<any> = [];
  57. constructor(
  58. public tbookSer: textbookServer,
  59. private activeRoute: ActivatedRoute,
  60. private router: Router,
  61. private message: NzMessageService
  62. ) {}
  63. ngOnInit() {
  64. this.activeRoute.paramMap.subscribe(async (params) => {
  65. let id = params.get('id');
  66. console.log(id);
  67. if (id) {
  68. let query = new Parse.Query('_User');
  69. query.include('department');
  70. this.user = await query.get(id);
  71. let queryProfile = new Parse.Query('Profile');
  72. queryProfile.equalTo('user', id);
  73. this.profile = await queryProfile.first();
  74. this.userDataJson = {
  75. companyType: this.profile?.get('companyType'),
  76. department: this.user.get('department'),
  77. };
  78. }
  79. let arr = ['个人', '评审专家', '高校联系人'];
  80. if (
  81. this.tbookSer.profile.identity == '国家级管理员' ||
  82. (this.tbookSer.profile.identity == '工作联系人' &&
  83. arr.includes(this.profile.identity)) ||
  84. (this.tbookSer.profile.identity == '高校联系人' &&
  85. this.profile.identity == '个人')
  86. ) {
  87. this.edit = true;
  88. }
  89. });
  90. }
  91. async updateUser(type: string) {
  92. console.log(type);
  93. if (!this.edit) {
  94. this.message.warning('同级身份暂无权限操作');
  95. return;
  96. }
  97. switch (type) {
  98. case '已认证':
  99. this.user.set('accountState', '已认证');
  100. break;
  101. case '已禁用':
  102. this.user.set('accountState', '已禁用');
  103. break;
  104. case '删除':
  105. this.user.set('isDeleted', true);
  106. break;
  107. }
  108. await this.user.save();
  109. this.ngOnInit();
  110. }
  111. async handleOk() {
  112. if (!this.edit) {
  113. this.message.warning('同级身份暂无权限操作');
  114. return;
  115. }
  116. this.password = this.password.trim();
  117. if (!this.password) {
  118. this.message.warning('密码格式错误');
  119. return;
  120. }
  121. try {
  122. this.user.set('password', this.password);
  123. await this.user.save();
  124. this.ngOnInit();
  125. } catch (err) {
  126. this.message.warning('保存出错,请注意密码格式');
  127. }
  128. this.isVisible = false;
  129. }
  130. //选择部门
  131. async showModalDepart() {
  132. if(this.unitTypes.length == 0){
  133. await this.getUnitTypes()
  134. }
  135. let parent = this.unitTypes.find(
  136. (item) => item.name == this.userDataJson.companyType
  137. );
  138. if (parent?.id) {
  139. this.parentMap = await this.formatNode(parent.id);
  140. }
  141. this.provinceChange(this.parentMap[this.parentMap.length - 1]?.id);
  142. this.isShowModal = true;
  143. }
  144. async getUnitTypes() {
  145. let query = new Parse.Query('Department');
  146. query.equalTo('branch', undefined);
  147. query.equalTo('parent', undefined);
  148. query.notEqualTo('isDeleted', true);
  149. query.select('name');
  150. let r = await query.find();
  151. r.forEach((item) => {
  152. this.unitTypes.push({ id: item.id, name: item.get('name') });
  153. });
  154. }
  155. //根据所选单位类型获取对应单位
  156. async provinceChange(id?: string, val?: string) {
  157. let query = new Parse.Query('Department');
  158. query.select('name', 'branch', 'parent');
  159. if (this.tbookSer.profile.identity != '国家级管理员') {
  160. query.equalTo(
  161. 'objectId',
  162. this.tbookSer.profile?.user.department?.objectId
  163. );
  164. }
  165. if (this.tbookSer.profile.identity == '国家级管理员' || id) {
  166. query.equalTo('parent', id ? id : null);
  167. }
  168. query.limit(100);
  169. val && query.contains('name', val);
  170. let r = await query.find();
  171. this.parentList = r;
  172. }
  173. async formatNode(id: string): Promise<Array<any>> {
  174. let arr = [];
  175. if (id) {
  176. let query = new Parse.Query('Department');
  177. query.equalTo('objectId', id);
  178. query.select('parent', 'name');
  179. let r = await query.first();
  180. arr.push({
  181. title: r?.get('name'),
  182. id: r?.id,
  183. });
  184. if (r?.get('parent')) {
  185. arr.unshift(...(await this.formatNode(r?.get('parent')?.id)));
  186. }
  187. }
  188. return arr;
  189. }
  190. onCheck(e: any) {
  191. console.log(e);
  192. this.provinceChange();
  193. }
  194. parent:string = '' //搜索时传入的id
  195. //选择部门
  196. async onCheckedDepart(e: any) {
  197. console.log(e);
  198. if (e?.get('parent')?.id) {
  199. this.userDataJson.department = e;
  200. this.parent = e?.get('parent')?.id
  201. } else {
  202. this.provinceChange(e.id);
  203. this.parent = e?.id
  204. }
  205. this.parentMap = await this.formatNode(e.id);
  206. }
  207. handleCancel(): void {
  208. console.log('Button cancel clicked!');
  209. this.userDataJson = {
  210. companyType: this.profile?.get('companyType'),
  211. department: this.user.get('department'),
  212. };
  213. this.isShowModal = false;
  214. this.parent = ''
  215. }
  216. async completeChange(): Promise<void> {
  217. if(!this.userDataJson.department?.id){
  218. this.message.warning('请选择部门')
  219. return
  220. }
  221. this.userDataJson.companyType = this.userDataJson.department.get('branch');
  222. this.profile?.set('companyType', this.userDataJson.companyType)
  223. await this.profile?.save()
  224. this.user?.set('department', this.userDataJson.department?.toPointer())
  225. await this.user?.save()
  226. this.ngOnInit()
  227. this.isShowModal = false;
  228. this.parent = ''
  229. }
  230. }