export const resourceCode = import.meta.env.VUE_APP_RESOURCE_CODE;
export const moduleCode = import.meta.env.VUE_APP_MODULE_CODE || '';
let $http;

export const createRequest = (vm) => {
  $http = vm.$http;
};

export const request = async (options) => {
  if (!$http) {
    console.error('请求实例未创建！');
    return;
  }
  // 相应拦截
  const onResponse = (res) => {
    console.log('onResponse', onResponse);
    return res;
  };

  // 请求拦截
  const onRequest = (options) => {
    const copyOptions = JSON.parse(JSON.stringify(options));
    // copyOptions.url = 'portal-source/' + resourceCode + moduleCode + copyOptions.url
    return copyOptions;
  };

  // 请求前
  const opt = onRequest(options);
  // 发请求
  const res = await $http(opt);
  // 请求后
  return onResponse(res);
};
