import { isJSON } from '@/H5Public/utils/authority';

export function getCurrentUser() {
  const d = sessionStorage.getItem('currentUser');
  if (d && isJSON(d)) {
    return JSON.parse(d);
  } else {
    return {};
  }
}

export function setCurrentUser(v){
  if(v && typeof v === 'object'){
    sessionStorage.setItem('currentUser', JSON.stringify(v));
  }
}

export function setUserInfo(data) {
  localStorage.setItem('user', JSON.stringify(data));
}

export const getUserInfo = () => {
  let x = localStorage.getItem('user');
  if (isJSON(x)) {
    x = JSON.parse(x);
    if (!x.groupsId) {
      x.groupsId = 'pub';
      x.differenceBetweenServerAndClientTime = 0;
    }
    return x;
  } else {
    return {
      groupsId: 'pub',
      differenceBetweenServerAndClientTime: 0,
    };
  }
};
