Browse Source

BUG修复

seimin 3 years ago
parent
commit
2cb189e9d0
1 changed files with 101 additions and 92 deletions
  1. 101 92
      src/app/views/login/login.component.ts

+ 101 - 92
src/app/views/login/login.component.ts

@@ -1,27 +1,30 @@
1
+import { Component, OnInit } from "@angular/core";
2
+import { Router } from "@angular/router";
3
+import { FormBuilder, Validators, FormGroup } from "@angular/forms";
4
+import { NzMessageService } from "ng-zorro-antd/message";
1
 
5
 
2
-import { Component, OnInit } from '@angular/core';
3
-import { Router } from '@angular/router'
4
-import { FormBuilder, Validators, FormGroup } from '@angular/forms';
5
-import { NzMessageService } from 'ng-zorro-antd/message';
6
-
7
-import { MainService } from '../../services/main.service'
8
-import { AES, mode, pad, enc } from 'crypto-js';
6
+import { MainService } from "../../services/main.service";
7
+import { AES, mode, pad, enc } from "crypto-js";
9
 
8
 
10
 @Component({
9
 @Component({
11
-  selector: 'app-login',
12
-  templateUrl: './login.component.html',
13
-  styleUrls: ['./login.component.less'],
10
+  selector: "app-login",
11
+  templateUrl: "./login.component.html",
12
+  styleUrls: ["./login.component.less"],
14
 })
13
 })
15
 export class LoginComponent implements OnInit {
14
 export class LoginComponent implements OnInit {
16
   validateForm: FormGroup;
15
   validateForm: FormGroup;
17
   loading: boolean = false;
16
   loading: boolean = false;
18
   // 是否单点登录
17
   // 是否单点登录
19
   isSingleSignOn: boolean;
18
   isSingleSignOn: boolean;
20
-  constructor(private router: Router, private fb: FormBuilder, private mainService: MainService, private msg: NzMessageService) {
21
-  }
19
+  constructor(
20
+    private router: Router,
21
+    private fb: FormBuilder,
22
+    private mainService: MainService,
23
+    private msg: NzMessageService
24
+  ) {}
22
   ngOnInit() {
25
   ngOnInit() {
23
     let url = location.href;
26
     let url = location.href;
24
-    this.isSingleSignOn = url.includes('?');
27
+    this.isSingleSignOn = url.includes("?");
25
     if (this.isSingleSignOn) {
28
     if (this.isSingleSignOn) {
26
       this.singleSignOn();
29
       this.singleSignOn();
27
     } else {
30
     } else {
@@ -31,40 +34,40 @@ export class LoginComponent implements OnInit {
31
   //单点登录
34
   //单点登录
32
   singleSignOn() {
35
   singleSignOn() {
33
     let url = location.href;
36
     let url = location.href;
34
-    let paramsStr = url.split('?')[1];
37
+    let paramsStr = url.split("?")[1];
35
     let paramsObj = {};
38
     let paramsObj = {};
36
-    paramsStr.split('&').forEach(item => {
37
-      let arr = item.split('=');
39
+    paramsStr.split("&").forEach((item) => {
40
+      let arr = item.split("=");
38
       paramsObj[arr[0] + 1] = arr[1];
41
       paramsObj[arr[0] + 1] = arr[1];
39
-    })
42
+    });
40
     this.singleSignOnLogin(paramsObj);
43
     this.singleSignOnLogin(paramsObj);
41
   }
44
   }
42
   // 单点登录
45
   // 单点登录
43
   singleSignOnLogin(paramsObj): void {
46
   singleSignOnLogin(paramsObj): void {
44
-    const loadingId = this.msg.loading('登录中..', { nzDuration: 0 }).messageId;
47
+    const loadingId = this.msg.loading("登录中..", { nzDuration: 0 }).messageId;
45
     this.loading = true;
48
     this.loading = true;
46
     this.mainService.singleSignOnLogin(paramsObj).subscribe(
49
     this.mainService.singleSignOnLogin(paramsObj).subscribe(
47
-      data => {
50
+      (data) => {
48
         this.loading = false;
51
         this.loading = false;
49
         this.msg.remove(loadingId);
52
         this.msg.remove(loadingId);
50
-        localStorage.removeItem('remember');
51
-        this.apiLogin(data.userId,data.pwd);
52
-      },err=>{
53
+        this.apiLogin(data.userId, data.pwd, true);
54
+      },
55
+      (err) => {
53
         this.loading = false;
56
         this.loading = false;
54
         this.msg.remove(loadingId);
57
         this.msg.remove(loadingId);
55
-        this.msg.error('登录失败', {
56
-          nzDuration: 1000
58
+        this.msg.error("登录失败", {
59
+          nzDuration: 1000,
57
         });
60
         });
58
-        this.router.navigateByUrl('/login');
61
+        this.router.navigateByUrl("/login");
59
       }
62
       }
60
-    )
63
+    );
61
   }
64
   }
62
   //aes加密
65
   //aes加密
63
   encryptByEnAES(data: string): string {
66
   encryptByEnAES(data: string): string {
64
     let Key = "dsadmin";
67
     let Key = "dsadmin";
65
     let tmpAES = AES.encrypt(data, Key, {
68
     let tmpAES = AES.encrypt(data, Key, {
66
       mode: mode.CBC,
69
       mode: mode.CBC,
67
-      padding: pad.Pkcs7
70
+      padding: pad.Pkcs7,
68
     });
71
     });
69
     return tmpAES.toString();
72
     return tmpAES.toString();
70
   }
73
   }
@@ -73,7 +76,7 @@ export class LoginComponent implements OnInit {
73
     let Key = "dsadmin";
76
     let Key = "dsadmin";
74
     let tmpDeAES = AES.decrypt(data, Key, {
77
     let tmpDeAES = AES.decrypt(data, Key, {
75
       mode: mode.CBC,
78
       mode: mode.CBC,
76
-      padding: pad.Pkcs7
79
+      padding: pad.Pkcs7,
77
     });
80
     });
78
     return tmpDeAES.toString(enc.Utf8);
81
     return tmpDeAES.toString(enc.Utf8);
79
   }
82
   }
@@ -81,25 +84,25 @@ export class LoginComponent implements OnInit {
81
   // 初始化菜单
84
   // 初始化菜单
82
   initMenu(menu) {
85
   initMenu(menu) {
83
     let menuLis = this.transform(menu);
86
     let menuLis = this.transform(menu);
84
-    menuLis.forEach(item => {
87
+    menuLis.forEach((item) => {
85
       item.flag = false;
88
       item.flag = false;
86
       item.flagBg = false;
89
       item.flagBg = false;
87
-      item.childrens.forEach(value => {
90
+      item.childrens.forEach((value) => {
88
         value.flag = false;
91
         value.flag = false;
89
-      })
90
-    })
91
-    localStorage.setItem('menu', JSON.stringify(menuLis));
92
-    localStorage.setItem('index', 'true');//首页默认高亮
92
+      });
93
+    });
94
+    localStorage.setItem("menu", JSON.stringify(menuLis));
95
+    localStorage.setItem("index", "true"); //首页默认高亮
93
   }
96
   }
94
 
97
 
95
   // 初始化登录表单
98
   // 初始化登录表单
96
   initForm() {
99
   initForm() {
97
     let username, passsword;
100
     let username, passsword;
98
     // 如果本地存储的有remember字段(记住密码功能)
101
     // 如果本地存储的有remember字段(记住密码功能)
99
-    if (localStorage.getItem('remember')) {
100
-      let remember = JSON.parse(localStorage.getItem('remember'));
102
+    if (localStorage.getItem("remember")) {
103
+      let remember = JSON.parse(localStorage.getItem("remember"));
101
       // 比较记住密码的功能是否在有效期内
104
       // 比较记住密码的功能是否在有效期内
102
-      if (remember.date > (new Date().getTime())) {
105
+      if (remember.date > new Date().getTime()) {
103
         username = this.encryptByDeAES(remember.username);
106
         username = this.encryptByDeAES(remember.username);
104
         passsword = this.encryptByDeAES(remember.password);
107
         passsword = this.encryptByDeAES(remember.password);
105
       }
108
       }
@@ -108,14 +111,14 @@ export class LoginComponent implements OnInit {
108
     this.validateForm = this.fb.group({
111
     this.validateForm = this.fb.group({
109
       userName: [username || null, [Validators.required]],
112
       userName: [username || null, [Validators.required]],
110
       password: [passsword || null, [Validators.required]],
113
       password: [passsword || null, [Validators.required]],
111
-      remember: [true]
114
+      remember: [true],
112
     });
115
     });
113
   }
116
   }
114
   // enter触发
117
   // enter触发
115
   enterUp(e): void {
118
   enterUp(e): void {
116
     let keyCode = e.which || e.keyCode || 0;
119
     let keyCode = e.which || e.keyCode || 0;
117
     if (keyCode == 13) {
120
     if (keyCode == 13) {
118
-      this.submitForm()
121
+      this.submitForm();
119
     }
122
     }
120
   }
123
   }
121
   // 登录
124
   // 登录
@@ -125,113 +128,119 @@ export class LoginComponent implements OnInit {
125
       this.validateForm.controls[i].updateValueAndValidity();
128
       this.validateForm.controls[i].updateValueAndValidity();
126
     }
129
     }
127
     // 用户名和密码必填
130
     // 用户名和密码必填
128
-    if (!this.validateForm.value.userName || !this.validateForm.value.password) {
129
-      this.msg.info('用户名或密码不能为空!');
131
+    if (
132
+      !this.validateForm.value.userName ||
133
+      !this.validateForm.value.password
134
+    ) {
135
+      this.msg.info("用户名或密码不能为空!");
130
       return;
136
       return;
131
     }
137
     }
132
-    this.apiLogin(this.validateForm.value.userName,this.validateForm.value.password);
138
+    this.apiLogin(
139
+      this.validateForm.value.userName,
140
+      this.validateForm.value.password
141
+    );
133
   }
142
   }
134
-  // 登录接口
135
-  apiLogin(user,pwd){
136
-    const loadingId = this.msg.loading('登录中..', { nzDuration: 0 }).messageId;
143
+  // 登录接口,true是单点登录
144
+  apiLogin(user, pwd, flag = false) {
145
+    const loadingId = this.msg.loading("登录中..", { nzDuration: 0 }).messageId;
137
     this.loading = true;
146
     this.loading = true;
138
-    this.mainService.login(user, pwd).subscribe(
139
-      data => {
140
-        this.loading = false;
141
-        this.msg.remove(loadingId);
142
-        if (data.status == 200) {
147
+    this.mainService.login(user, pwd).subscribe((data) => {
148
+      this.loading = false;
149
+      this.msg.remove(loadingId);
150
+      if (data.status == 200) {
151
+        if (!flag) {
143
           // 记住密码勾选中
152
           // 记住密码勾选中
144
           if (this.validateForm.value.remember) {
153
           if (this.validateForm.value.remember) {
145
             let remember = {
154
             let remember = {
146
               username: this.encryptByEnAES(this.validateForm.value.userName),
155
               username: this.encryptByEnAES(this.validateForm.value.userName),
147
               password: this.encryptByEnAES(this.validateForm.value.password),
156
               password: this.encryptByEnAES(this.validateForm.value.password),
148
-              date: new Date().getTime() + 90 * 24 * 3600 * 1000//3个月
149
-            }
150
-            localStorage.setItem('remember', JSON.stringify(remember));
157
+              date: new Date().getTime() + 90 * 24 * 3600 * 1000, //3个月
158
+            };
159
+            localStorage.setItem("remember", JSON.stringify(remember));
151
           } else {
160
           } else {
152
-            localStorage.removeItem('remember');
161
+            localStorage.removeItem("remember");
153
           }
162
           }
154
-          this.toRoute2(data)
155
-          this.initMenu(data.user.menu)
156
-        } else {
157
-          this.msg.error(data.remarks, {
158
-            nzDuration: 1000
159
-          });
160
         }
163
         }
164
+        this.toRoute2(data);
165
+        this.initMenu(data.user.menu);
166
+      } else {
167
+        this.msg.error(data.remarks, {
168
+          nzDuration: 1000,
169
+        });
161
       }
170
       }
162
-    )
171
+    });
163
   }
172
   }
164
 
173
 
165
   // 页面跳转
174
   // 页面跳转
166
   toRoute2(data) {
175
   toRoute2(data) {
167
     let roleMenus = data.user.menu;
176
     let roleMenus = data.user.menu;
168
-    let canLogin = false;//是否可以进入系统主页面(无护士端,调度台,药房端权限)
169
-    let successLoginMsg = true;//登录成功提示,true是提示,false不提示
177
+    let canLogin = false; //是否可以进入系统主页面(无护士端,调度台,药房端权限)
178
+    let successLoginMsg = true; //登录成功提示,true是提示,false不提示
170
     let fwt = false;
179
     let fwt = false;
171
     let ddt = false;
180
     let ddt = false;
172
     let yfang = false;
181
     let yfang = false;
173
-    roleMenus.forEach(e => {
174
-      if (e.title.includes('药房端')) {
182
+    roleMenus.forEach((e) => {
183
+      if (e.title.includes("药房端")) {
175
         yfang = true;
184
         yfang = true;
176
-        console.log('药房端')
177
-      } else if (e.title.includes('护士端')) {
185
+        console.log("药房端");
186
+      } else if (e.title.includes("护士端")) {
178
         fwt = true;
187
         fwt = true;
179
-        console.log('护士端')
180
-      } else if (e.title.includes('调度台')) {
188
+        console.log("护士端");
189
+      } else if (e.title.includes("调度台")) {
181
         ddt = true;
190
         ddt = true;
182
-        console.log('调度台')
191
+        console.log("调度台");
183
       }
192
       }
184
-      canLogin = true
193
+      canLogin = true;
185
     });
194
     });
186
     if (fwt) {
195
     if (fwt) {
187
       // 护士角色跳护士端
196
       // 护士角色跳护士端
188
       if (successLoginMsg) {
197
       if (successLoginMsg) {
189
-        this.msg.success('登录成功!', {
190
-          nzDuration: 1000
198
+        this.msg.success("登录成功!", {
199
+          nzDuration: 1000,
191
         });
200
         });
192
       }
201
       }
193
       successLoginMsg = false;
202
       successLoginMsg = false;
194
-      localStorage.setItem('user', JSON.stringify(data.user))
195
-      this.router.navigateByUrl('/nurse');
203
+      localStorage.setItem("user", JSON.stringify(data.user));
204
+      this.router.navigateByUrl("/nurse");
196
 
205
 
197
       return;
206
       return;
198
     } else if (ddt) {
207
     } else if (ddt) {
199
       // 调度台
208
       // 调度台
200
       if (successLoginMsg) {
209
       if (successLoginMsg) {
201
-        this.msg.success('登录成功!', {
202
-          nzDuration: 1000
210
+        this.msg.success("登录成功!", {
211
+          nzDuration: 1000,
203
         });
212
         });
204
       }
213
       }
205
       successLoginMsg = false;
214
       successLoginMsg = false;
206
-      localStorage.setItem('user', JSON.stringify(data.user))
207
-      this.router.navigateByUrl('/dispatchingDesk');
215
+      localStorage.setItem("user", JSON.stringify(data.user));
216
+      this.router.navigateByUrl("/dispatchingDesk");
208
       return;
217
       return;
209
     } else if (yfang) {
218
     } else if (yfang) {
210
       // 药房端
219
       // 药房端
211
       if (successLoginMsg) {
220
       if (successLoginMsg) {
212
-        this.msg.success('登录成功!', {
213
-          nzDuration: 1000
221
+        this.msg.success("登录成功!", {
222
+          nzDuration: 1000,
214
         });
223
         });
215
       }
224
       }
216
       successLoginMsg = false;
225
       successLoginMsg = false;
217
-      localStorage.setItem('user', JSON.stringify(data.user))
218
-      this.router.navigateByUrl('/pharmacy');
226
+      localStorage.setItem("user", JSON.stringify(data.user));
227
+      this.router.navigateByUrl("/pharmacy");
219
       return;
228
       return;
220
     }
229
     }
221
     // 有菜单可以进入系统主页面
230
     // 有菜单可以进入系统主页面
222
     if (!canLogin) {
231
     if (!canLogin) {
223
-      this.msg.success('暂无权限进入系统!', {
224
-        nzDuration: 1000
232
+      this.msg.success("暂无权限进入系统!", {
233
+        nzDuration: 1000,
225
       });
234
       });
226
     } else {
235
     } else {
227
       if (successLoginMsg) {
236
       if (successLoginMsg) {
228
-        this.msg.success('登录成功!', {
229
-          nzDuration: 1000
237
+        this.msg.success("登录成功!", {
238
+          nzDuration: 1000,
230
         });
239
         });
231
       }
240
       }
232
       successLoginMsg = false;
241
       successLoginMsg = false;
233
-      localStorage.setItem('user', JSON.stringify(data.user))
234
-      this.router.navigateByUrl('/main/home');
242
+      localStorage.setItem("user", JSON.stringify(data.user));
243
+      this.router.navigateByUrl("/main/home");
235
     }
244
     }
236
   }
245
   }
237
 
246
 
@@ -259,8 +268,8 @@ export class LoginComponent implements OnInit {
259
     const idMapping = nodes.reduce((acc, el, i) => {
268
     const idMapping = nodes.reduce((acc, el, i) => {
260
       acc[el.id] = i;
269
       acc[el.id] = i;
261
       return acc;
270
       return acc;
262
-    }, {})
263
-    nodes.forEach(el => {
271
+    }, {});
272
+    nodes.forEach((el) => {
264
       if (el.parentid === null || el.parentid === undefined) {
273
       if (el.parentid === null || el.parentid === undefined) {
265
         parents.push(el);
274
         parents.push(el);
266
       } else {
275
       } else {
@@ -268,6 +277,6 @@ export class LoginComponent implements OnInit {
268
         parentEl.childrens = [...(parentEl.childrens || []), el];
277
         parentEl.childrens = [...(parentEl.childrens || []), el];
269
       }
278
       }
270
     });
279
     });
271
-    return parents
280
+    return parents;
272
   }
281
   }
273
 }
282
 }