import {Injectable} from '@angular/core'; import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild, UrlTree } from '@angular/router'; import {AuthService} from "../../core/services/auth/auth-service"; import {MenuUtilsService} from "../../core/services/menu/menu-utils.service"; import {ApiAnon} from '../../core/services/http/apiAnon'; import {ResultModel} from '../../models/result-model'; import {map} from 'rxjs/operators'; import {User} from '../../models/system/user'; @Injectable({ providedIn: 'root', }) export class WechatGuard implements CanActivate, CanActivateChild { constructor(private authService: AuthService, private menuUtilsService: MenuUtilsService, private apiAnon: ApiAnon, private router: Router) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise { const url: string = state.url; return this.checkLogin(url); } canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise { return this.canActivate(route, state); } async checkLogin(url: string): Promise { // Store the attempted URL for redirecting this.authService.redirectUrl = url; // 需要等待authService完成自己的加载 try { await this.authService.ready(); if (this.authService.isLogin()) { // 如果authService判断是登陆状态,直接返回true return true; } else { const wechat_callback_code = this._getQueryString("code"); if (wechat_callback_code) { const res = await this.apiAnon.post('wx/token', wechat_callback_code) .pipe( map((resp: any) => resp as ResultModel) ) .toPromise(); if (res && res.code === 1) { const _user = new User(); _user.id = res.result.id; _user.name = res.result.name; _user.accessToken = res.result.accessToken; _user.resourceList = res.result.resourceList; _user.office = res.result.office; _user.roleList = res.result.sysRoleInfoDtos; _user.wechatInfo = res.result.wechatInfo; await this.authService.setAccessToken(_user); console.log("在路由改变前,守卫中:" + location.href); return true; } else if (res && res.code === 0) { const _user = new User(); _user.accessToken = ""; _user.wechatInfo = res.result.wechatInfo; await this.authService.setAccessToken(_user); return this.router.parseUrl("register"); } else { return this.router.parseUrl("error;errorMsg=抱歉,您的微信账号无法直接登陆应用!原因是:与多个账号关联!"); } } else { // 进行获取地址并重定向等待回调 const currentUri = window.location.href; // const currentUri = "http://f.199.m2platform.cn/#/m2/about-me/"; // 重定向到微信认证入口,并设定当前页面为返回页面 this.getAuthUri(currentUri).subscribe(value => { if (value && value.code === 1) { window.location.href = value.result; } }); return false; } } } catch (e) { return this.router.parseUrl("error;errorMsg=抱歉,您的微信账号无法直接登陆应用!"); } } getAuthUri(redirectUri: string) { return this.apiAnon.post("wx/oauth-req", redirectUri).pipe( map((resp: any) => resp as ResultModel)); } private _getQueryString(name) { const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 构造一个含有目标参数的正则表达式对象 const result = window.location.search.substr(1).match(reg); // 对querystring匹配目标参数 if (result != null) { return decodeURIComponent(result[2]); } else { return null; } } }