import { message } from '@/utils/elementUtils';
const isDev = process.env.NODE_ENV !== 'production';

const createAjaxSetting = (url, method) => ({
  url,
  type: method,
  hostKey: isDev ? 'dev' : '/',
  headers: {},
  cache: false,
  timeout: 10000,
});

// 通用请求函数
export default ({ url, method, data = {}}) => {
  return new Promise((resolve, reject) => {
    const ajaxSetting = createAjaxSetting(url, method);
    const param = data;
    Eui.Comm.postToRemote(
      ajaxSetting,
      param,
      (res) => {
        if (res.success) {
          resolve(res);
        } else {
          message.error(res.message);
          resolve(res);
        }
      },
      (err) => {
        reject(err);
        message.error('请求失败，请检查网络连接或联系管理员');
      },
    );
  });
};
