users-management.component.ts 18 KB

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