param key param value param expireIn export function setLocalStorage ( key , value , expireIn = 0 ) { localStorage . setItem ( key , typeof value === 'object' ? `obj-${ JSON . stringify ( value ) }` : `str-${ value }` ) ; if ( expireIn > 0 ) { const expirationTime = Date . now ( ) + expireIn * 1000 ; localStorage . setItem ( `${ key }_expirationTime` , `${ expirationTime }` ) ; } else { localStorage . removeItem ( `${ key }_expirationTime` ) ; } } /**
 * 从 localStorage 中取出一个值，过期的值会被清除
 */ export function getLocalStorage ( key ) { const value = localStorage . getItem ( key ) ; const expirationTime = localStorage . getItem ( `${ key }_expirationTime` ) ; if ( expirationTime && Date . now ( ) > parseInt ( expirationTime , 10 ) ) { localStorage . removeItem ( key ) ; return null ; } if ( ! value ) { return null ; } if ( value . startsWith ( 'obj-' ) ) { return JSON . parse ( value . slice ( 4 ) ) ; } if ( value . startsWith ( 'str-' ) ) { return value . slice ( 4 ) ; } return value ; } /**
 * 从 localStorage 中删除一个值，对应的 ${key}_expirationTime 也会被删除
 */ export function removeLocalStorage ( key ) { localStorage . removeItem ( key ) ; localStorage . removeItem ( `${ key }_expirationTime` ) ; }