/**
 * window.localStorage 浏览器永久缓存
 */
export const localStorage = {
  // 设置永久缓存
  set(key: string, val: any) {
    window.localStorage.setItem(key, JSON.stringify(val));
  },
  // 获取永久缓存
  get(key: string) {
    const json: any = window.localStorage.getItem(key);
    //return JSON.parse(json);
    return json;
  },
  // 移除永久缓存
  remove(key: string) {
    window.localStorage.removeItem(key);
  },
  // 移除全部永久缓存
  clear() {
    window.localStorage.clear();
  }
};

export const localStorageTime = { 
  setExpires(key: string, val: any, expires: number) {
    const storage = {
      value:val,
      expires: expires ? new Date().getTime() + expires * 60 * 1000 : new Date().getTime() + 8 * 60 * 60 * 1000
    }
    console.log('expires', storage.expires)
    window.localStorage.setItem(key, JSON.stringify(storage));
  },
  getExpires(key: string) {
    let storages = window.localStorage.getItem(key);
    if(storages) {
      const stData = JSON.parse(storages);
      const currentTime = new Date().getTime();
      if(currentTime > stData.expires) {
        window.localStorage.removeItem(key);
        storages = null;
        return null;
      }else {
        return stData.value
      }
    }else {
      return null;
    }  
  }
}

