// 添加缓存
import store from '../../store/index.js'
import $common from './common.js'
import wsRequest from './websocket.js'
// import InitializationData from './InitializationData.js'
const addSetCache = function(data) {
  uni.setStorageSync('oldTime', oldTime());
  uni.setStorageSync('userInformationData', data)
  new wsRequest("http://47.94.108.8:18018/ws/" + data.id, 5000)
}

const selectSetCache = function() {
  if (judgeTime()) { //判断缓存是否过期，过期后就清空
    uni.removeStorageSync('oldTime')
    uni.removeStorageSync('userInformationData')
    $common.redToUrl('/pages/page/logon/logon');
  } else {
    let userInformationData = uni.getStorageSync('userInformationData');
    new wsRequest("http://47.94.108.8:18018/ws/" + userInformationData.id, 5000)
    // 获取最新的并更新
    store.commit("userInfor", userInformationData)
    $common.switchToUrl('/pages/index/index');
    // InitializationData.initalDataFun()
  }
}

//判断缓存是否过期
const judgeTime = function() {
  let nowTime = Date.now();
  let oldTime = uni.getStorageSync('oldTime');
  let userInformationData = uni.getStorageSync('userInformationData');
  console.log(userInformationData.status)
  if(userInformationData.status != 1){
	  return true;
  }
  if (oldTime && nowTime < oldTime) {
    return false;
  }
  return true;
}
const oldTime = function() { //返回以现在为准的一周后的时间
  return Date.now() + 604800000; //毫秒(一周)
}
export default {
  addSetCache,
  judgeTime,
  selectSetCache
}
