/**
 * 设置token
 * */
import { isJSON } from '@/webPublic/one_stop_public/copy';
import { getOneStopMyInfo } from '@/webPublic/one_stop_public/utils/utils';
import { delCookie, setCookie } from '@/webPublic/one_stop_public/utils/cookie';

export function setToken(value, time) {
  if (getToken() !== value) {
    setCookie('token', value, 1);
    sessionStorage.removeItem('cacheMobile');
    localStorage.setItem('antd-pro-token-onestop', value);
    localStorage.setItem('antd-pro-token', value);
    localStorage.setItem('wisdom-school-token', value);
    return getOneStopMyInfo();
  } else {
    return new Promise((resolve, reject) => {
      resolve(true);
    });
  }
}

/**
 * 获取token
 * */
export function getToken() {
  const x = localStorage.getItem('antd-pro-token-onestop');
  if (x && x.length > 11) {
    return x;
  }
  const y = localStorage.getItem('antd-pro-token');
  if (y && y.length > 11) {
    return y;
  }

  return null;
}

/**
 * 删除token
 * */
export const delToken = () => {
  delCookie('token');
  localStorage.removeItem('antd-pro-type');
  localStorage.removeItem('antd-pro-token-onestop');
  localStorage.removeItem('antd-pro-token');
  localStorage.removeItem('wisdom-school-token');
  localStorage.removeItem('user');
  sessionStorage.removeItem('cacheMobile');
};

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

export function setFetchUrl(data) {

  return localStorage.setItem('fetch-url-data', data);
}

export function getFetchUrl() {

  return localStorage.getItem('fetch-url-data');
}

export function getType() {

  return localStorage.getItem('antd-pro-type') || '';
}

export function setType(type) {

  return localStorage.setItem('antd-pro-type', type);
}

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));
  }
}
