users-management.component.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. import { Component, OnInit, ViewChild } from "@angular/core";
  2. import { ActivatedRoute, Router } from "@angular/router";
  3. import { FormBuilder, Validators, FormGroup } from "@angular/forms";
  4. import { MainService } from "../../services/main.service";
  5. import { OverlayScrollbarsComponent } from "overlayscrollbars-ngx";
  6. import { ToolService } from "../../services/tool.service";
  7. import { forkJoin, Subject } from "rxjs";
  8. import { debounceTime } from "rxjs/operators";
  9. import { NzMessageService } from "ng-zorro-antd";
  10. @Component({
  11. selector: "app-users-management",
  12. templateUrl: "./users-management.component.html",
  13. styleUrls: ["./users-management.component.less"],
  14. })
  15. export class UsersManagementComponent implements OnInit {
  16. @ViewChild("osComponentRef1", {
  17. read: OverlayScrollbarsComponent,
  18. static: false,
  19. })
  20. osComponentRef1: OverlayScrollbarsComponent;
  21. constructor(
  22. private message: NzMessageService,
  23. private fb: FormBuilder,
  24. private route: ActivatedRoute,
  25. private router: Router,
  26. private mainService: MainService,
  27. private tool: ToolService
  28. ) {}
  29. listOfData: any[] = []; //表格数据
  30. modal: boolean = false; //新增/编辑模态框
  31. add: boolean; //true:新增;false:编辑
  32. validateForm: FormGroup; //新增/编辑表单
  33. coopId: number; //表格中执行操作的id
  34. department: any; //所属科室
  35. num; //工号
  36. name; //姓名
  37. userType: any; //用户类型
  38. hosId: any; //院区(搜索)
  39. userGroup; //所属组
  40. userGroup1; //所属组(搜索)
  41. userTypes: Array<any> = []; //所有用户类型
  42. alldepart: any = []; //所有所属科室
  43. alldepart1: any = []; //所有所属科室(搜索)
  44. allUserGroup: Array<any>; //所有用户组
  45. allUserGroup1: Array<any>; //用户组(搜索)
  46. allUserRole: Array<any>; //所有角色
  47. pageIndex: number = 1; //页码
  48. listLength: number = 10; //总条数
  49. pageSize: number = 10; //每页条数
  50. promptContent: string; //操作提示框提示信息
  51. ifSuccess: boolean; //操作成功/失败
  52. promptInfo: string; //操作结果提示信息
  53. promptModalShow: boolean; //操作提示框是否展示
  54. btnLoading: boolean = false; //提交按钮loading状态
  55. wxRequired = false; //新增或编辑用户的时候,微信号是否必填
  56. changeInpSubject = new Subject(); //防抖
  57. ngOnInit() {
  58. //防抖
  59. this.changeInpSubject.pipe(debounceTime(500)).subscribe((v) => {
  60. this.getDeparts(v[0], v[1], v[2]);
  61. });
  62. this.coopBtns = this.tool.initCoopBtns(this.route);
  63. this.initForm();
  64. this.getAllRole();
  65. this.getUserTypes();
  66. this.getGender();
  67. }
  68. // 初始化增删改按钮
  69. coopBtns: any = {};
  70. // 搜索
  71. search() {
  72. this.pageIndex = 1;
  73. this.getList();
  74. }
  75. // 重置
  76. reset() {
  77. this.pageIndex = 1;
  78. this.name = "";
  79. this.num = "";
  80. this.userType = null;
  81. this.department = null;
  82. this.userGroup1 = null;
  83. this.changeHosp1(this.hosId);
  84. this.getList();
  85. }
  86. // 表格数据
  87. loading1 = false;
  88. getList() {
  89. let data = {
  90. idx: this.pageIndex - 1,
  91. sum: this.pageSize,
  92. user: {
  93. name: this.name || "",
  94. dept: { id: this.department || "" },
  95. usertype: { id: this.userType || "" },
  96. account: this.num || "",
  97. groupdata: { id: this.userGroup1 || "" },
  98. hospital: { id: this.hosId || "" },
  99. userTypeIds: String(this.userTypes.map(v => v.id))
  100. },
  101. };
  102. if (!data.user.dept || !data.user.dept.id) {
  103. delete data.user.dept;
  104. }
  105. if (!data.user.usertype || !data.user.usertype.id) {
  106. delete data.user.usertype;
  107. }
  108. if (!data.user.groupdata || !data.user.groupdata.id) {
  109. delete data.user.groupdata;
  110. }
  111. if (!data.user.hospital || !data.user.hospital.id) {
  112. delete data.user.hospital;
  113. }
  114. this.loading1 = true;
  115. this.mainService
  116. .getFetchDataList("data", "user", data)
  117. .subscribe((data) => {
  118. this.loading1 = false;
  119. this.listOfData = data.list;
  120. this.listLength = data.totalNum;
  121. });
  122. }
  123. // 获取所有用户类型
  124. getUserTypes() {
  125. this.mainService.getDictionary("list", "usertype").subscribe((data) => {
  126. this.userTypes = data;
  127. this.getAllHospital();
  128. });
  129. }
  130. // 获取相应的院区
  131. getAllHospital() {
  132. this.hosId = this.tool.getCurrentHospital().id + "";
  133. this.changeHosp1(this.hosId);
  134. this.getList();
  135. }
  136. // 角色列表
  137. getAllRole() {
  138. var that = this;
  139. let data = {
  140. idx: 0,
  141. sum: 1000,
  142. };
  143. that.mainService
  144. .getFetchDataList("data", "role", data)
  145. .subscribe((data) => {
  146. that.allUserRole = data.list;
  147. });
  148. }
  149. // 切换院区选项获取对应科室列表
  150. changeHosp() {
  151. let hid = this.hosId - 0;
  152. let data = {
  153. department: {
  154. hospital: { id: hid },
  155. },
  156. idx: 0,
  157. sum: 20,
  158. };
  159. let groupData = {
  160. group2: {
  161. hospitals: hid,
  162. type:1
  163. },
  164. idx: 0,
  165. sum: 9999,
  166. };
  167. this.maskFlag = this.message.loading("正在加载中..", {
  168. nzDuration: 0,
  169. }).messageId;
  170. let department$ = this.mainService.getFetchDataList(
  171. "data",
  172. "department",
  173. data
  174. );
  175. let group$ = this.mainService.getFetchDataList("data", "group2", groupData);
  176. forkJoin(department$, group$).subscribe((res) => {
  177. this.message.remove(this.maskFlag);
  178. this.maskFlag = false;
  179. this.modal = true;
  180. this.alldepart = res[0]["list"];
  181. this.allUserGroup = res[1]["list"];
  182. });
  183. }
  184. // 切换院区选项获取对应科室列表(搜索)seimin
  185. changeHosp1(id) {
  186. this.department = null;
  187. this.userGroup1 = null;
  188. let departmentData = {
  189. department: {
  190. hospital: { id },
  191. },
  192. idx: 0,
  193. sum: 20,
  194. };
  195. let groupData = {
  196. group2: {
  197. hospitals: id,
  198. type:1
  199. },
  200. idx: 0,
  201. sum: 9999,
  202. };
  203. this.mainService
  204. .getFetchDataList("data", "department", departmentData)
  205. .subscribe((data) => {
  206. this.alldepart1 = data.list;
  207. });
  208. this.mainService
  209. .getFetchDataList("data", "group2", groupData)
  210. .subscribe((data) => {
  211. this.allUserGroup1 = data.list;
  212. });
  213. }
  214. // 获取性别
  215. genders: Array<any> = [];
  216. getGender() {
  217. var that = this;
  218. that.mainService.getDictionary("list", "user_gender").subscribe((data) => {
  219. that.genders = data;
  220. });
  221. }
  222. // 新增弹框
  223. showModal() {
  224. this.add = true;
  225. this.initForm();
  226. this.changeHosp();
  227. }
  228. hideModal() {
  229. this.modal = false;
  230. this.initForm();
  231. }
  232. // 初始化新增form表单
  233. initForm() {
  234. if (this.add) {
  235. this.alldepart = [];
  236. this.allUserGroup = [];
  237. }
  238. this.validateForm = this.fb.group({
  239. name: [null, [Validators.required]],
  240. account: [null, [Validators.required]],
  241. usertype: [null, [Validators.required]],
  242. dept: [null, [Validators.required]],
  243. deptPhone: [null, [Validators.required]],
  244. gender: [null, [Validators.required]],
  245. userGroup: [null],
  246. role: [null, [Validators.required]],
  247. weixin: [null],
  248. });
  249. }
  250. // 选择用户类型
  251. usertypeChange() {
  252. if (this.validateForm.value.usertype == 106) {
  253. //配送人员
  254. this.wxRequired = true;
  255. this.validateForm.get("weixin")!.setValidators(Validators.required);
  256. this.validateForm.get("weixin")!.markAsDirty();
  257. } else {
  258. this.wxRequired = false;
  259. this.validateForm.get("weixin")!.clearValidators();
  260. this.validateForm.get("weixin")!.markAsPristine();
  261. }
  262. this.validateForm.get("weixin")!.updateValueAndValidity();
  263. }
  264. // 表单提交
  265. submitForm(): void {
  266. var that = this;
  267. for (const i in that.validateForm.controls) {
  268. that.validateForm.controls[i].markAsDirty({ onlySelf: true });
  269. that.validateForm.controls[i].updateValueAndValidity();
  270. }
  271. if (that.validateForm.invalid) return;
  272. that.btnLoading = true;
  273. let groups = [],
  274. roles = [];
  275. if (that.validateForm.value.userGroup) {
  276. that.validateForm.value.userGroup.forEach((element) => {
  277. groups.push({ id: element });
  278. });
  279. }
  280. that.validateForm.value.role.forEach((element) => {
  281. roles.push({ id: element });
  282. });
  283. let data = {
  284. user: {
  285. name: that.validateForm.value.name,
  286. account: that.validateForm.value.account,
  287. gender: { id: that.validateForm.value.gender, key: "user_gender" },
  288. usertype: { id: that.validateForm.value.usertype },
  289. dept: { id: that.validateForm.value.dept },
  290. group: groups,
  291. role: roles,
  292. phone: that.validateForm.value.deptPhone,
  293. hospital: { id: that.hosId },
  294. weixin: that.validateForm.value.weixin,
  295. },
  296. };
  297. if (!that.validateForm.value.userGroup) {
  298. delete data.user.group;
  299. } else if (that.validateForm.value.userGroup.length === 0) {
  300. delete data.user.group;
  301. }
  302. if (!that.add) {
  303. data.user["id"] = that.coopId;
  304. }
  305. that.mainService
  306. .coopData(that.add ? "addData" : "updData", "user", data)
  307. .subscribe((data) => {
  308. that.btnLoading = false;
  309. that.hideModal();
  310. that.initForm();
  311. if (data.status == 200) {
  312. that.showPromptModal(that.add ? "新增" : "编辑", true, "");
  313. } else {
  314. that.showPromptModal(that.add ? "新增" : "编辑", false, data.msg);
  315. }
  316. });
  317. }
  318. // 编辑
  319. maskFlag: any = false;
  320. edit(data) {
  321. // ----------------
  322. var hid = data.hospital.id;
  323. let departmentData = {
  324. department: {
  325. hospital: { id: hid },
  326. },
  327. idx: 0,
  328. sum: 20,
  329. };
  330. let groupData = {
  331. group2: {
  332. hospitals: hid,
  333. type:1
  334. },
  335. idx: 0,
  336. sum: 9999,
  337. };
  338. this.maskFlag = this.message.loading("正在加载中..", {
  339. nzDuration: 0,
  340. }).messageId;
  341. this.mainService
  342. .getFetchDataList("data", "group2", groupData)
  343. .subscribe((item1) => {
  344. this.allUserGroup = item1.list;
  345. this.mainService
  346. .getFetchDataList("data", "department", departmentData)
  347. .subscribe((item2) => {
  348. this.message.remove(this.maskFlag);
  349. this.maskFlag = false;
  350. this.modal = true;
  351. this.alldepart = item2.list;
  352. if (data.dept) {
  353. let has = item2.list.some((d) => d.id == data.dept.id);
  354. if (!has) {
  355. this.alldepart = [data.dept, ...item2.list];
  356. }
  357. }
  358. this.isLoading = false;
  359. // ------------------------
  360. this.add = false;
  361. this.coopId = data.id;
  362. let groups = [],
  363. roles = [];
  364. if (data.group) {
  365. data.group.forEach((element) => {
  366. groups.push(element["id"] + "");
  367. });
  368. }
  369. if (data.role) {
  370. data.role.forEach((element) => {
  371. roles.push(element["id"] + "");
  372. });
  373. }
  374. this.validateForm.controls.name.setValue(data.name);
  375. this.validateForm.controls.account.setValue(data.account);
  376. this.validateForm.controls.usertype.setValue(data.usertype.id + "");
  377. if (data.dept) {
  378. this.validateForm.controls.dept.setValue(data.dept.id);
  379. }
  380. this.validateForm.controls.deptPhone.setValue(data.phone);
  381. this.validateForm.controls.gender.setValue(data.gender.id + "");
  382. this.validateForm.controls.userGroup.setValue(
  383. groups.length ? groups : null
  384. );
  385. this.validateForm.controls.role.setValue(
  386. roles.length ? roles : null
  387. );
  388. this.validateForm.controls.gender.setValue(data.gender.id + "");
  389. this.validateForm.controls.weixin.setValue(data.weixin);
  390. this.usertypeChange();
  391. });
  392. });
  393. // ----------------
  394. }
  395. // 删除
  396. delModal: boolean = false; //删除模态框
  397. del(data) {
  398. let that = this;
  399. that.coopId = data.id;
  400. that.delModal = true;
  401. }
  402. // 确认删除(删除企业微信用户)
  403. confirmDel() {
  404. let that = this;
  405. that.btnLoading = true;
  406. that.mainService
  407. .rmvDataAndWeChatNum([that.coopId], true)
  408. .subscribe((data) => {
  409. that.btnLoading = false;
  410. that.hideDelModal();
  411. if (data["status"] == 200) {
  412. if (
  413. that.listOfData.length == 1 &&
  414. that.pageIndex == Math.ceil(that.listLength / that.pageSize)
  415. ) {
  416. that.listLength--;
  417. that.pageIndex = Math.ceil(that.listLength / that.pageSize);
  418. }
  419. that.showPromptModal("删除", true, "");
  420. } else {
  421. that.showPromptModal("删除", false, data["msg"]);
  422. }
  423. });
  424. }
  425. // 确认删除(不删除企业用户)
  426. cancenlLoading = false;
  427. cancelDel() {
  428. let that = this;
  429. that.cancenlLoading = true;
  430. that.mainService
  431. .rmvDataAndWeChatNum([that.coopId], false)
  432. .subscribe((data) => {
  433. that.cancenlLoading = false;
  434. that.hideDelModal();
  435. if (data["status"] == 200) {
  436. if (
  437. that.listOfData.length == 1 &&
  438. that.pageIndex == Math.ceil(that.listLength / that.pageSize)
  439. ) {
  440. that.listLength--;
  441. that.pageIndex = Math.ceil(that.listLength / that.pageSize);
  442. }
  443. that.showPromptModal("删除", true, "");
  444. } else {
  445. that.showPromptModal("删除", false, data["msg"]);
  446. }
  447. });
  448. }
  449. // 关闭删除模态框
  450. hideDelModal() {
  451. this.delModal = false;
  452. }
  453. // ==================重置密码start====================
  454. // 重置密码
  455. resetModal: boolean = false; //删除模态框
  456. resetPwd(data) {
  457. this.coopId = data.id;
  458. this.resetModal = true;
  459. }
  460. // 确认重置密码
  461. confirmReset() {
  462. this.btnLoading = true;
  463. this.mainService
  464. .resetpwd(this.coopId)
  465. .subscribe((data) => {
  466. this.btnLoading = false;
  467. this.hideResetModal();
  468. if (data["status"] == 200) {
  469. this.showPromptModal("重置密码", true, "");
  470. } else {
  471. this.showPromptModal("重置密码", false, data["msg"]);
  472. }
  473. });
  474. }
  475. // 取消
  476. cancelReset() {
  477. this.resetModal = false;
  478. }
  479. // 关闭删除模态框
  480. hideResetModal() {
  481. this.resetModal = false;
  482. }
  483. // ==================重置密码end====================
  484. // 展示信息提示框(con:提示信息,success:操作是否成功,promptInfo:操作结果提示信息)
  485. showPromptModal(con, success, promptInfo?) {
  486. this.promptModalShow = false;
  487. this.promptContent = con;
  488. this.ifSuccess = success;
  489. this.promptInfo = promptInfo;
  490. setTimeout(() => {
  491. this.promptModalShow = true;
  492. }, 100);
  493. this.getList();
  494. }
  495. // 查看
  496. detail(id) {
  497. this.router.navigateByUrl("/main/usersManagement/userDetail/" + id);
  498. }
  499. // 边输边搜节流阀
  500. isLoading = false;
  501. changeInp(deptId, type, e) {
  502. if (!deptId) {
  503. if (type == "search") {
  504. this.alldepart1 = [];
  505. } else if (type == "add") {
  506. this.alldepart = [];
  507. }
  508. return;
  509. }
  510. this.isLoading = true;
  511. this.changeInpSubject.next([deptId, type, e]);
  512. }
  513. // 获取所有科室
  514. snum = 0;
  515. getDeparts(deptId, type, dept) {
  516. var that = this;
  517. let data = {
  518. department: {
  519. dept,
  520. hospital: {
  521. id: deptId,
  522. },
  523. },
  524. idx: 0,
  525. sum: 20,
  526. };
  527. this.snum++;
  528. that.mainService
  529. .getFetchDataList("data", "department", data)
  530. .subscribe((data) => {
  531. this.snum--;
  532. if (type == "search") {
  533. that.alldepart1 = data.list;
  534. } else if (type == "add") {
  535. that.alldepart = data.list;
  536. }
  537. if (this.snum === 0) {
  538. that.isLoading = false;
  539. }
  540. });
  541. }
  542. }