import { Platform } from '@angular/cdk/platform' ; import { DOCUMENT } from '@angular/common' ; import { Inject , Injectable } from '@angular/core' ; import type { NzSafeAny } from 'ng-zorro-antd/core/types' ; export interface CookieOptions { path ? : string ; domain ? : string ; /**
     * Expiration time, `number` is seconds
     *
     * 过期时间，`number` 类型表示秒数
     */ expires ? : number | Date | string ; secure ? : boolean ; HttpOnly ? : boolean ; SameSite ? : boolean | 'lax' | 'strict' | 'none' ; } /**
 * A set of simple Cookie manipulation classes.
 *
 * 一组简单的 Cookie 操作类。
 */ @ Injectable ( { providedIn : 'root' } ) export class CookieService { private get doc ( ) : Document { return this . _doc || document ; } /**
     * Original cookie value
     *
     * 原始Cookie值
     */ get cookie ( ) : string { return this . platform . isBrowser ? this . doc . cookie : '' ; } constructor ( @ Inject ( DOCUMENT ) private _doc , private platform ) { } /**
     * Get all cookie key-value pairs
     *
     * 获取所有Cookie键值对
     */ getAll ( ) { const ret = { } ; const arr = this . cookie . split ( '; ' ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { const cookie = arr [ i ] ; const index = cookie . indexOf ( '=' ) ; if ( index > 0 ) { const name = decodeURIComponent ( cookie . substring ( 0 , index ) ) ; if ( ret [ name ] == null ) { ret [ name ] = decodeURIComponent ( cookie . substring ( index + 1 ) ) ; } } } return ret ; } /**
     * Get the value of given cookie `key`
     *
     * 获取指定 `key` 的值
     */ get ( key ) { return this . getAll ( ) [ key ] ; } /**
     * Sets a value for given cookie key
     *
     * 设置指定 Cookie 键的值
     */ put ( key , value , options ? ) { if ( ! this . platform . isBrowser ) { return ; } const opt = { path : '/' , ... options } ; if ( typeof opt . expires === 'number' ) { opt . expires = new Date ( + new Date ( ) + opt . expires * 1e3 ) ; } if ( typeof opt . expires !== 'string' ) { opt . expires = opt . expires ? opt . expires . toUTCString ( ) : '' ; } const optStr = opt as NzSafeAny ; const attributes = Object . keys ( optStr ) . filter ( k => optStr [ k ] && optStr [ k ] !== true ) . map ( k => `${ k }=${ ( optStr [ k ] as string ) . split ( ';' ) [ 0 ] }` ) . join ( ';' ) ; this . doc . cookie = `${ encodeURIComponent ( String ( key ) ) }=${ encodeURIComponent ( String ( value ) ) }${ attributes ? `; ${ attributes }` : '' }` ; } /**
     * Remove given cookie
     *
     * 移除指定 Cookie
     */ remove ( key , options ? ) { this . put ( key , undefined , options ) ; } /**
     * Remove all cookies
     *
     * 移除所有 Cookies
     */ removeAll ( ) { this . doc . cookie = '' ; } }