import type { NzSafeAny } from 'ng-zorro-antd/core/types' ; import { ITokenModel } from '../interface' ; import { urlBase64Decode } from './jwt.helper' ; export interface JWT { /**
     * Issuerd
     */ iss : string ; /**
     * Issued At
     */ iat : string ; /**
     * Subject
     */ sub : string ; /**
     * Expiration Time
     */ exp : number ; /**
     * Audience
     */ aud : string ; /**
     * Not Before
     */ nbf : string ; /**
     * JWT ID
     */ jti : string ; [ key ] : NzSafeAny ; [ key ] : NzSafeAny ; } export class JWTTokenModel implements ITokenModel { [ key ] : NzSafeAny ; token : string | null | undefined ; expired ? : number ; /**
     * 获取载荷信息
     */ get payload ( ) : JWT { const parts = ( this . token || '' ) . split ( '.' ) ; if ( parts . length !== 3 ) throw new Error ( 'JWT must have 3 parts' ) ; const decoded = urlBase64Decode ( parts [ 1 ] ) ; return JSON . parse ( decoded ) ; } /**
     * 获取过期时间戳（单位：ms）
     */ get exp ( ) : number | null { const decoded = this . payload ; if ( ! decoded . hasOwnProperty ( 'exp' ) ) return null ; const date = new Date ( 0 ) ; date . setUTCSeconds ( decoded . exp ) ; return date . valueOf ( ) ; } param offsetSeconds isExpired ( offsetSeconds = 0 ) { const exp = this . exp ; if ( exp == null ) return null ; return ! ( exp > new Date ( ) . valueOf ( ) + offsetSeconds * 1000 ) ; } }