import { getCurrentUser, getToken, getUserInfo } from '@/webPublic/one_stop_public/utils/token';
import gg5xxxxx from 'js-md5';
import { getIsBei_Dian } from '@/webPublic/zyd_public/utils/utils';
import { getUrlInfo } from '@/webPublic/one_stop_public/DetailForAudit/utils';

const isTest = getUrlInfo().test;

export function getVisitorToken(){
  let token = getToken();
  const visitor_client = sessionStorage.getItem('visitor_client') === 'true' || !!getUrlInfo().visitor;
  if(visitor_client && (token === 'null' || !token)){ // 访客token 欢哥喊加的
    token = '704d693b-cc68-49a7-8888-4acc73b5ca73';
  }
  return token;
}


  export function getHeaders(url = '', headersApi = undefined) {
    let token = getVisitorToken();
    const currentInfo = getCurrentUser() || {};
  const user = getUserInfo() || {};
  const userId = user.id || currentInfo.xgUserId;
  const timestamp = user.differenceBetweenServerAndClientTime ? new Date().getTime() + user.differenceBetweenServerAndClientTime : new Date().getTime();
  const secretString = token + userId + timestamp;
  const res = {
    headers: {},
  };

  if(url && typeof url === 'string' && url.indexOf('oauthPub=true') > -1){ // 公共接口不需要token
    return res;
  }
  if (!token || token === 'null') {
    return res;
  } else {
    res.headers.Authorization = `bearer ${token}`; // 这个必须要带上
  }

  // if (getIsBei_Dian() && userId) {
    // res.headers.Authorization = `bearer ${token}`;
    // res.headers.auth = gg5xxxxx(secretString);
    // res.headers.timestamp = timestamp;
    // if (headersApi) {
    //   res.headers.apis = headersApi;
    // }
  // }
  return res;
}
