From c4ebd842cd91ac1bcd0c592c472c90a679453300 Mon Sep 17 00:00:00 2001 From: Sjh <55341701@qq.com> Date: Mon, 6 Nov 2023 16:55:09 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=AE=8C=E5=96=84]=E8=B7=B3=E8=BD=AC=E9=A2=84?= =?UTF-8?q?=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/auth.guard.ts | 75 ++++++++++++++----- .../entry-plan/entry-plan.component.ts | 12 ++- 2 files changed, 68 insertions(+), 19 deletions(-) diff --git a/src/app/auth.guard.ts b/src/app/auth.guard.ts index b01fc58..60dfcbb 100644 --- a/src/app/auth.guard.ts +++ b/src/app/auth.guard.ts @@ -1,31 +1,70 @@ -import { Injectable } from '@angular/core'; -import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router'; +import { HttpClient } from "@angular/common/http"; +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 { + console.log("next", next); + console.log("state", state); + if ( + next.queryParams.singleSignOn && + next.queryParams.singleSignOn === "true" + ) { + //执行自动登录逻辑 + await this.onSubmit(); } + return this.checkLogin(); + } - // 路由守卫 - canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { + checkLogin(): boolean { + // 判断本地有没有token + const token = sessionStorage.getItem("token"); - return this.checkLogin(); + // 如果有token,允许访问 + if (token) { + return true; } - checkLogin(): boolean { + //如果没有token,跳转登录页 + this.router.navigate(["/login"]); + return false; + } - // 判断本地有没有token - const token = sessionStorage.getItem('token'); - - // 如果有token,允许访问 - if (token) { return true; } - - //如果没有token,跳转登录页 - this.router.navigate(['/login']); - return false; - } + onSubmit() { + return new Promise((resolve, reject) => { + this.http + .post("/api/Account/SignIn", { + name: "superadmin", + password: "SHya119!@", + }) + .subscribe( + (data: any) => { + sessionStorage.setItem("level", data.level); + sessionStorage.setItem("token", data.token); + sessionStorage.setItem("refreshToken", data.refreshToken); + sessionStorage.setItem("isDefaultPassword", data.isDefaultPassword); //是否需要修改默认密码 + sessionStorage.setItem("singleSignOn", "true"); //是否需要修改默认密码 + resolve(); + }, + (err) => { + reject(); + } + ); + }); + } } diff --git a/src/app/plan-management/entry-plan/entry-plan.component.ts b/src/app/plan-management/entry-plan/entry-plan.component.ts index f494d86..fc02318 100644 --- a/src/app/plan-management/entry-plan/entry-plan.component.ts +++ b/src/app/plan-management/entry-plan/entry-plan.component.ts @@ -370,6 +370,8 @@ export class EntryPlanComponent implements OnInit { if (this.reservePlanType && this.reservePlanType.length != 0) { reservePlanType = eval(this.reservePlanType.join("|")); } + + let singleSignOn = sessionStorage.getItem("singleSignOn"); let paramsdata: any = { CompanyName: this.companyName || "", OrganizationId: this.jsId || "", @@ -389,6 +391,14 @@ export class EntryPlanComponent implements OnInit { IsNewData: this.IsNewData, IsNewCompanyData: this.IsNewCompanyData, PlanModes: planmode, + CompanyNames: singleSignOn + ? [ + "大连路地铁站", + "东方渔人码头投资开发有限公司", + "上海孚宝港务有限公司", + "上海国际会议中心", + ] + : [], }; this.http .get("/api/Plans", { params: paramsdata }) @@ -396,7 +406,7 @@ export class EntryPlanComponent implements OnInit { this.length = data.totalCount; this.allPlanInfo = data; this.tabledataSource = data.items; - console.log('预案列表',this.tabledataSource) + console.log("预案列表", this.tabledataSource); }); }