8 changed files with 292 additions and 265 deletions
@ -1,153 +1,154 @@ |
|||||||
import { ApplicationRef, Component, ComponentFactoryResolver, Injector, OnInit } from '@angular/core'; |
import { ApplicationRef, Component, ComponentFactoryResolver, Injector, OnInit } from '@angular/core'; |
||||||
import { HttpClient } from '@angular/common/http' |
import { HttpClient } from '@angular/common/http' |
||||||
import { Data } from '../../interface' |
import { Data } from '../../interface' |
||||||
import { Router,ActivatedRoute } from '@angular/router' |
import { Router, ActivatedRoute } from '@angular/router' |
||||||
import {CacheTokenService} from '../../http-interceptors/cache-token.service'//引入服务
|
import { CacheTokenService } from '../../http-interceptors/cache-token.service'//引入服务
|
||||||
import { MatSnackBar,MatSnackBarConfig } from '@angular/material/snack-bar'; |
import { MatSnackBar, MatSnackBarConfig } from '@angular/material/snack-bar'; |
||||||
import { MatDialog,MatDialogRef } from '@angular/material/dialog'; |
import { MatDialog, MatDialogRef } from '@angular/material/dialog'; |
||||||
import { SearchService } from '@src/app/searchComponent.service'; |
import { SearchService } from '@src/app/searchComponent.service'; |
||||||
import { SearchDownList } from '@src/modules/map/declare/component/SearchDownListPlugins/SearchDownList'; |
import { SearchDownList } from '@src/modules/map/declare/component/SearchDownListPlugins/SearchDownList'; |
||||||
|
import * as global from 'globals'; |
||||||
|
|
||||||
|
|
||||||
@Component({ |
@Component({ |
||||||
selector: 'app-login', |
selector: 'app-login', |
||||||
templateUrl: './login.component.html', |
templateUrl: './login.component.html', |
||||||
styleUrls: ['./login.component.scss'], |
styleUrls: ['./login.component.scss'], |
||||||
}) |
}) |
||||||
export class LoginComponent implements OnInit { |
export class LoginComponent implements OnInit { |
||||||
|
|
||||||
constructor(private http:HttpClient,private router:Router,private route:ActivatedRoute,public token:CacheTokenService,public snackBar: MatSnackBar, public dialog: MatDialog) {
|
constructor(private http: HttpClient, private router: Router, private route: ActivatedRoute, public token: CacheTokenService, public snackBar: MatSnackBar, public dialog: MatDialog) { |
||||||
|
|
||||||
window.injector.get<SearchService>(SearchService).appendComponentToBody(SearchDownList); |
} |
||||||
} |
|
||||||
|
ngOnInit() { |
||||||
ngOnInit() { |
this.automaticLogin() |
||||||
this.automaticLogin() |
} |
||||||
} |
|
||||||
|
errmsg: string = '' |
||||||
errmsg :string = '' |
|
||||||
|
onSubmit(e) { |
||||||
onSubmit(e){ |
this.http.post('/api/Account/SignIn', { |
||||||
this.http.post('/api/Account/SignIn',{ |
name: e.name, |
||||||
name: e.name, |
password: e.password |
||||||
password: e.password}).subscribe( (data: Data) => {
|
}).subscribe((data: Data) => { |
||||||
sessionStorage.setItem("level",data.level); |
sessionStorage.setItem("level", data.level); |
||||||
sessionStorage.setItem("token",data.token); |
sessionStorage.setItem("token", data.token); |
||||||
sessionStorage.setItem("refreshToken",data.refreshToken); |
sessionStorage.setItem("refreshToken", data.refreshToken); |
||||||
sessionStorage.setItem("isDefaultPassword",data.isDefaultPassword);//是否需要修改默认密码
|
sessionStorage.setItem("isDefaultPassword", data.isDefaultPassword);//是否需要修改默认密码
|
||||||
this.http.get('/api/Account/NavMenus').subscribe((data:any)=>{ |
this.http.get('/api/Account/NavMenus').subscribe((data: any) => { |
||||||
let isHave = data.find(item=>{ return item.url == "/statisticanalysis/home"}) |
let isHave = data.find(item => { return item.url == "/statisticanalysis/home" }) |
||||||
let isHaveGis = data.find(item=>{ return item.url == "/gis"}) |
let isHaveGis = data.find(item => { return item.url == "/gis" }) |
||||||
if (isHave) { |
if (isHave) { |
||||||
this.router.navigate([`/statisticanalysis/home`]) |
this.router.navigate([`/statisticanalysis/home`]) |
||||||
this.dialogChangePassword() |
this.dialogChangePassword() |
||||||
return
|
return |
||||||
} else if(isHaveGis){ |
} else if (isHaveGis) { |
||||||
this.router.navigate([`/gis`]) |
this.router.navigate([`/gis`]) |
||||||
this.dialogChangePassword() |
this.dialogChangePassword() |
||||||
return
|
return |
||||||
} else if(data.length != 0){ |
} else if (data.length != 0) { |
||||||
this.router.navigate([`/keyUnit`]) |
this.router.navigate([`/keyUnit`]) |
||||||
this.dialogChangePassword() |
this.dialogChangePassword() |
||||||
return
|
return |
||||||
data.forEach(item => { |
data.forEach(item => { |
||||||
if(item.url){ |
if (item.url) { |
||||||
|
|
||||||
}
|
} |
||||||
}); |
}); |
||||||
}else{ |
} else { |
||||||
this.snackBar.open('该用户角色未分配任何菜单', '确定', { |
this.snackBar.open('该用户角色未分配任何菜单', '确定', { |
||||||
duration: 3000 |
duration: 3000 |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
}) |
||||||
|
if (e.notlogin) { //7天免登录时
|
||||||
|
localStorage.setItem("isnologin", "true") |
||||||
|
localStorage.setItem("token", data.token) |
||||||
|
localStorage.setItem("refreshToken", data.refreshToken) |
||||||
|
} |
||||||
|
//调用服务中的function刷新token
|
||||||
|
this.token.startUp() |
||||||
|
}, |
||||||
|
(err) => { this.errmsg = err } |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
dialogChangePassword() { |
||||||
|
if (sessionStorage.getItem('isDefaultPassword') == 'true') { |
||||||
|
let dialogRef = this.dialog.open(ChangepasswordComponent2, |
||||||
|
{ width: '348px' }); |
||||||
|
|
||||||
|
dialogRef.afterClosed().subscribe((data) => { |
||||||
|
sessionStorage.setItem('isDefaultPassword', 'false') |
||||||
}); |
}); |
||||||
} |
} |
||||||
|
} |
||||||
}) |
//7天免登录自动登录
|
||||||
if(e.notlogin){ //7天免登录时
|
automaticLogin() { |
||||||
localStorage.setItem("isnologin","true") |
let isNoLogin = localStorage.getItem("isnologin") |
||||||
localStorage.setItem("token",data.token) |
if (isNoLogin) { //7天免登录时
|
||||||
localStorage.setItem("refreshToken",data.refreshToken) } |
let token = localStorage.getItem("token"); |
||||||
//调用服务中的function刷新token
|
let refreshToken = localStorage.getItem("refreshToken"); |
||||||
this.token.startUp() |
this.http.post('/api/Account/RefreshToken', { |
||||||
}, |
token: token, |
||||||
(err) => {this.errmsg = err} |
refreshToken: refreshToken |
||||||
) |
}).subscribe((data: any) => { |
||||||
} |
sessionStorage.setItem("level", data.level); |
||||||
|
sessionStorage.setItem("token", data.token); |
||||||
|
sessionStorage.setItem("refreshToken", data.refreshToken); |
||||||
dialogChangePassword(){ |
this.token.startUp() |
||||||
if(sessionStorage.getItem('isDefaultPassword') == 'true'){ |
this.router.navigate(['/keyUnit']) |
||||||
let dialogRef = this.dialog.open(ChangepasswordComponent2,
|
this.snackBar.open('已自动登录', '确定', { duration: 3000 }); |
||||||
{width:'348px'}); |
}) |
||||||
|
} //if
|
||||||
dialogRef.afterClosed().subscribe((data)=>{ |
|
||||||
sessionStorage.setItem('isDefaultPassword','false') |
} |
||||||
});
|
|
||||||
|
//打开弹窗
|
||||||
|
open() { |
||||||
|
this.snackBar.open('请联系管理员', '确定', { |
||||||
|
duration: 3000 |
||||||
|
}); |
||||||
} |
} |
||||||
} |
|
||||||
//7天免登录自动登录
|
|
||||||
automaticLogin () { |
|
||||||
let isNoLogin = localStorage.getItem("isnologin") |
|
||||||
if (isNoLogin) { //7天免登录时
|
|
||||||
let token = localStorage.getItem("token"); |
|
||||||
let refreshToken = localStorage.getItem("refreshToken"); |
|
||||||
this.http.post('/api/Account/RefreshToken', { |
|
||||||
token: token, |
|
||||||
refreshToken: refreshToken}).subscribe((data:any)=>{ |
|
||||||
sessionStorage.setItem("level",data.level); |
|
||||||
sessionStorage.setItem("token",data.token); |
|
||||||
sessionStorage.setItem("refreshToken",data.refreshToken); |
|
||||||
this.token.startUp() |
|
||||||
this.router.navigate(['/keyUnit']) |
|
||||||
this.snackBar.open('已自动登录', '确定', {duration: 3000}); |
|
||||||
}) |
|
||||||
} //if
|
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
//打开弹窗
|
|
||||||
open () { |
|
||||||
this.snackBar.open('请联系管理员', '确定', { |
|
||||||
duration: 3000 |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
@Component({ |
@Component({ |
||||||
selector: 'app-changepassword', |
selector: 'app-changepassword', |
||||||
templateUrl: './changePassword.html', |
templateUrl: './changePassword.html', |
||||||
styleUrls: ['./changepassword.scss'] |
styleUrls: ['./changepassword.scss'] |
||||||
}) |
}) |
||||||
|
|
||||||
export class ChangepasswordComponent2 implements OnInit { |
export class ChangepasswordComponent2 implements OnInit { |
||||||
|
|
||||||
constructor(private http:HttpClient,public snackBar: MatSnackBar, |
constructor(private http: HttpClient, public snackBar: MatSnackBar, |
||||||
public dialogRef: MatDialogRef<ChangepasswordComponent2>) { } |
public dialogRef: MatDialogRef<ChangepasswordComponent2>) { } |
||||||
|
|
||||||
ngOnInit() { |
ngOnInit() { |
||||||
} |
} |
||||||
errmsg :string = '' |
errmsg: string = '' |
||||||
|
|
||||||
onSubmit(e){ |
onSubmit(e) { |
||||||
this.http.put( |
this.http.put( |
||||||
'/api/Account/Password',
|
'/api/Account/Password', |
||||||
{ |
{ |
||||||
newPassword: e.newPassword, |
newPassword: e.newPassword, |
||||||
password: 'SHya119!@' |
password: 'SHya119!@' |
||||||
} |
} |
||||||
).subscribe(data=> { |
).subscribe(data => { |
||||||
this.dialogRef.close(data); |
this.dialogRef.close(data); |
||||||
const config = new MatSnackBarConfig(); |
const config = new MatSnackBarConfig(); |
||||||
config.verticalPosition = 'top'; |
config.verticalPosition = 'top'; |
||||||
config.duration = 3000 |
config.duration = 3000 |
||||||
this.snackBar.open('密码修改成功','确定',config); |
this.snackBar.open('密码修改成功', '确定', config); |
||||||
},(err) => |
}, (err) => { this.errmsg = err } |
||||||
{this.errmsg = err} |
) |
||||||
) |
} |
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,30 +1,29 @@ |
|||||||
import { ApplicationRef, ComponentFactoryResolver, ComponentRef, EmbeddedViewRef, Injectable, Injector } from '@angular/core'; |
import { ApplicationRef, ComponentFactoryResolver, ComponentRef, EmbeddedViewRef, Injectable, Injector } from '@angular/core'; |
||||||
|
|
||||||
@Injectable({ |
@Injectable({ |
||||||
providedIn: 'root' |
providedIn: 'root' |
||||||
}) |
}) |
||||||
export class SearchService { |
export class SearchService { |
||||||
constructor(private componentFactoryResolver: ComponentFactoryResolver,private applicationRef: ApplicationRef,private injector: Injector,) {} |
constructor(private componentFactoryResolver: ComponentFactoryResolver, private applicationRef: ApplicationRef, private injector: Injector,) { } |
||||||
|
|
||||||
//angular 动态组件
|
//angular 动态组件
|
||||||
appendComponentToBody(component: any) { |
appendComponentToBody<T>(component: any): ComponentRef<T> { |
||||||
//create a component reference
|
//create a component reference
|
||||||
const componentRef = this.componentFactoryResolver.resolveComponentFactory(component).create(this.injector); |
const componentRef = this.componentFactoryResolver.resolveComponentFactory(component).create(this.injector); |
||||||
|
|
||||||
// attach component to the appRef so that so that it will be dirty checked.
|
// attach component to the appRef so that so that it will be dirty checked.
|
||||||
this.applicationRef.attachView(componentRef.hostView); |
this.applicationRef.attachView(componentRef.hostView); |
||||||
|
|
||||||
// get DOM element from component
|
// get DOM element from component
|
||||||
const domElem = (componentRef.hostView as EmbeddedViewRef < any > ).rootNodes[0] as HTMLElement; |
const domElem = (componentRef.hostView as EmbeddedViewRef<any>).rootNodes[0] as HTMLElement; |
||||||
|
|
||||||
document.body.appendChild(domElem); |
document.body.appendChild(domElem); |
||||||
|
|
||||||
return componentRef; |
|
||||||
} |
|
||||||
|
|
||||||
removeComponentFromBody(componentRef: ComponentRef < any > ) { |
return componentRef as ComponentRef<T>; |
||||||
|
} |
||||||
|
|
||||||
|
removeComponentFromBody(componentRef: ComponentRef<any>) { |
||||||
this.applicationRef.detachView(componentRef.hostView); |
this.applicationRef.detachView(componentRef.hostView); |
||||||
componentRef.destroy(); |
componentRef.destroy(); |
||||||
} |
} |
||||||
|
|
||||||
} |
} |
@ -1,5 +0,0 @@ |
|||||||
import { Injector } from "@angular/core"; |
|
||||||
|
|
||||||
export declare interface Window { |
|
||||||
injector: Injector |
|
||||||
} |
|
Loading…
Reference in new issue