Browse Source

[新增]免登录设置

test-assets
邵佳豪 1 year ago
parent
commit
f8cebca2c8
  1. 107
      src/app/auth.guard.ts
  2. 2
      src/app/pages/home/home.component.html
  3. 43
      src/app/pages/home/home.component.ts
  4. 112
      src/app/pages/records/records-nav/records-nav.component.ts

107
src/app/auth.guard.ts

@ -1,35 +1,96 @@
import { Component, OnInit, Inject } from '@angular/core';
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { HttpClient } from "@angular/common/http";
import { Component, OnInit, Inject } from "@angular/core";
import { Injectable } from "@angular/core";
import {
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
Router,
} from "@angular/router";
@Injectable({
providedIn: 'root'
providedIn: "root",
})
export class AuthGuard implements CanActivate {
constructor(private http: HttpClient, private router: Router) {}
constructor(private router: Router) {
// 路由守卫
async canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Promise<boolean> {
console.log("路由守卫", next);
if (
next.queryParams.singleSignOn &&
next.queryParams.singleSignOn === "true"
) {
await this.autoLogin();
}
return this.checkLogin();
}
// 路由守卫
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
// console.log('路由守卫',next.data)
// if(next.data.permission == 'xxxx'){
// return true;
// }
return this.checkLogin();
}
checkLogin(): boolean {
// 判断本地有没有token
const token = sessionStorage.getItem("token");
checkLogin(): boolean {
// 判断本地有没有token
const token = sessionStorage.getItem('token');
// 如果有token,允许访问
if (token) {
return true;
}
// 如果有token,允许访问
if (token) { return true; }
//如果没有token,跳转登录页
this.router.navigate(["/login"]);
//如果没有token,跳转登录页
this.router.navigate(['/login']);
return false;
}
return false;
}
autoLogin() {
return new Promise<void>((resolve, reject) => {
this.http
.post("/api/TokenAuth/Authenticate", {
userNameOrEmailAddress: "superadmin",
password: "Admin119119119",
})
.subscribe(
(data: any) => {
sessionStorage.setItem("token", data.result.accessToken);
sessionStorage.setItem(
"encryptedAccessToken",
data.result.encryptedAccessToken
);
this.http
.get("/api/services/app/Session/GetCurrentLoginInformations")
.subscribe(
async (data: any) => {
console.log("GetCurrentLoginInformations", data.result);
sessionStorage.setItem(
"userdata",
JSON.stringify(data.result.user)
);
sessionStorage.setItem(
"userdataOfgasstation",
JSON.stringify(data.result.user)
);
sessionStorage.setItem(
"isDefaultPassword",
JSON.stringify(data.result.user.isDefaultPassword)
);
sessionStorage.setItem(
"isPasswordExpired",
JSON.stringify(data.result.user.isPasswordExpired)
);
sessionStorage.setItem("isGasStation", "false");
resolve();
},
(err) => {
reject();
}
);
},
(err) => {
reject();
}
);
});
}
}

2
src/app/pages/home/home.component.html

@ -16,7 +16,7 @@
</li>
</ng-container>
<ng-template #elseTemplate>
<li *ngFor="let item of menuList1" (click)="routerChange(item)"
<li *ngFor="let item of menuList1" (click)="routerChange(item)"
[ngClass]="{'router-link-active': item.name == selectedItem}">
{{item.name}}
</li>

43
src/app/pages/home/home.component.ts

@ -118,35 +118,7 @@ export class HomeComponent implements OnInit {
isProd: boolean;
ngOnInit(): void {
this.isProd = this.patternService.isProd;
this.location.subscribe((event) => {
if (event.url == "/homepage") {
sessionStorage.setItem("selectedMenu", "首页");
this.selectedItem = "首页";
} else if (event.url == "/plan" || event.url == "/plan/petrolStation") {
sessionStorage.setItem("selectedMenu", "数字油站");
this.selectedItem = "数字油站";
} else if (
event.url == "/todaywarning" ||
event.url == "/todaywarning/petrolStation"
) {
sessionStorage.setItem("selectedMenu", "今日预警");
this.selectedItem = "今日预警";
} else if (event.url.indexOf("/records_nav") != -1) {
if (event.url == "/records_nav") {
this.router.navigate(["/records_nav/all"]);
} else if (event.url == "/records_nav/petrolStation") {
this.router.navigate(["/records_nav/petrolStation/all"]);
}
sessionStorage.setItem("selectedMenu", "预警记录");
this.selectedItem = "预警记录";
} else if (
event.url == "/license" ||
event.url == "/license/petrolStation"
) {
sessionStorage.setItem("selectedMenu", "证照管理");
this.selectedItem = "证照管理";
}
});
this.initSelectedItem(this.router);
let a = sessionStorage.getItem("userdata");
this.userMenu = JSON.parse(a).menus;
@ -190,6 +162,7 @@ export class HomeComponent implements OnInit {
this.router.events
.pipe(filter((event) => event instanceof NavigationEnd))
.subscribe((event: any) => {
console.log('走这里Elma44',event)
//管理者进入油站页面
if (event.url.indexOf("petrolStation") != -1 && !this.isGasStation) {
//控制返回按钮显示
@ -243,6 +216,18 @@ export class HomeComponent implements OnInit {
this.getWarningSwitch();
}
initSelectedItem(event) {
// console.log(666, event);
if (event.url.indexOf("/homepage") !== -1) {
sessionStorage.setItem("selectedMenu", "首页");
this.selectedItem = "首页";
} else if (event.url.indexOf("/todaywarning") !== -1) {
sessionStorage.setItem("selectedMenu", "今日预警");
this.selectedItem = "今日预警";
} else if (event.url.indexOf("/records_nav") != -1) {
this.selectedItem = "预警记录";
}
}
isWarningVoice = true; //预警声音
isWarningWindow = true; //预警弹窗
/**

112
src/app/pages/records/records-nav/records-nav.component.ts

@ -1,80 +1,86 @@
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { IsShowEchartsService } from 'src/app/service/isShowEcharts.service';
import {Location} from "@angular/common";
import { Component, OnInit } from "@angular/core";
import { Router } from "@angular/router";
import { IsShowEchartsService } from "src/app/service/isShowEcharts.service";
import { Location } from "@angular/common";
@Component({
selector: 'app-records-nav',
templateUrl: './records-nav.component.html',
styleUrls: ['./records-nav.component.scss']
selector: "app-records-nav",
templateUrl: "./records-nav.component.html",
styleUrls: ["./records-nav.component.scss"],
})
export class RecordsNavComponent implements OnInit {
constructor(
private router: Router,
private isShowEcharts: IsShowEchartsService,
private location: Location
) {}
constructor(private router: Router, private isShowEcharts: IsShowEchartsService,private location: Location) { }
isEcharts = true
menuList = ['预警类型统计', '卸油统计', '证照预警统计']
tap=[]
userMenu = []
menu=[]
selectedMenu
isEcharts = true;
menuList = ["预警类型统计", "卸油统计", "证照预警统计"];
tap = [];
userMenu = [];
menu = [];
selectedMenu;
ngOnInit(): void {
let a= sessionStorage.getItem('userdata')
this.userMenu=JSON.parse(a).menus
console.log(this.userMenu);
if (this.router.url.indexOf("oliunloadinglist") !== -1) {
console.log('走这个列里看看看看')
this.selectedMenu = "卸油统计";
}else{
this.selectedMenu = this.menu[0];
}
let a = sessionStorage.getItem("userdata");
this.userMenu = JSON.parse(a).menus;
for (let index = 0; index < this.userMenu.length; index++) {
let a=this.userMenu[index].name
this.tap.push(a)
let a = this.userMenu[index].name;
this.tap.push(a);
}
console.log(this.tap);
if (this.userMenu.length==0) {
this.menu=this.menuList
}else{
if (this.userMenu.length == 0) {
this.menu = this.menuList;
} else {
for (let index = 0; index < this.menuList.length; index++) {
for (let k = 0; k < this.tap.length; k++) {
if (this.tap[k]==this.menuList[index]) {
this.menu.push(this.tap[k])
console.log( this.menu);
if (this.tap[k] == this.menuList[index]) {
this.menu.push(this.tap[k]);
console.log(this.menu);
}
}
}
}
this.selectedMenu = this.menu[0]
this.routerChange()
// this.selectedMenu = this.menu[0];
this.routerChange();
}
selectMenu(item) {
if (this.selectedMenu == item) {
return
return;
}
this.isEcharts = true
this.selectedMenu = item
this.routerChange()
this.isEcharts = true;
this.selectedMenu = item;
this.routerChange();
}
routerChange() {
if (sessionStorage.getItem('isGasStation') == 'false') {
if (this.selectedMenu == '预警类型统计') {
this.router.navigate(['/records_nav/all'])
} else if (this.selectedMenu == '卸油统计') {
this.router.navigate(['/records_nav/oliunloadinglist'])
} else if (this.selectedMenu == '证照预警统计') {
this.router.navigate(['/records_nav/warningstatisticslist'])
if (sessionStorage.getItem("isGasStation") == "false") {
if (this.selectedMenu == "预警类型统计") {
this.router.navigate(["/records_nav/all"]);
} else if (this.selectedMenu == "卸油统计") {
console.log(888888888888888888888888)
this.router.navigate(["/records_nav/oliunloadinglist"]);
} else if (this.selectedMenu == "证照预警统计") {
this.router.navigate(["/records_nav/warningstatisticslist"]);
}
} else {
if (this.selectedMenu == '预警类型统计') {
this.router.navigate(['/records_nav/petrolStation/all'])
} else if (this.selectedMenu == '卸油统计') {
this.router.navigate(['/records_nav/petrolStation/oliunloadinglist'])
} else if (this.selectedMenu == '证照预警统计') {
this.router.navigate(['/records_nav/petrolStation/warningstatisticslist'])
if (this.selectedMenu == "预警类型统计") {
this.router.navigate(["/records_nav/petrolStation/all"]);
} else if (this.selectedMenu == "卸油统计") {
this.router.navigate(["/records_nav/petrolStation/oliunloadinglist"]);
} else if (this.selectedMenu == "证照预警统计") {
this.router.navigate([
"/records_nav/petrolStation/warningstatisticslist",
]);
}
}
}
}

Loading…
Cancel
Save