order.ts 3.86 KB
import axios from 'axios';
import { defHttp } from '/@/utils/http/axios';
import { useUserStoreWithOut } from '/@/store/modules/user';
import { useOrderStoreWithOut } from '/@/store/modules/order';

enum Api {
  ORDER_CREATE = '/order/erp/order/add',
  UPDATE = '/order/erp/order/edit',
  ORDER = '/order/erp/order/list_by_page',
  FIELD_AUTH = '/order/erp/order/field_unlock_apply',
  EXPORT = '/order/erp/order/export',
  UPLOAD = '/api/localStorage/upload',

  DICT_INIT = '/order/erp/dictionary/get_all',
  DICT_ADD = '/order/erp/dictionary/add',
  DICT_UPDATE = '/order/erp/dictionary/edit',
  DICT_DELETE = '/order/erp/dictionary/delete',
  DICT_LIST = '/order/erp/dictionary/list_by_page',

  ANALYSIS = '/order/erp/profit/analysis',
}

export const orderCreate = async (data: any) => {
  const res = await defHttp.post<any>({ url: Api.ORDER_CREATE, data }, { message: '保存成功' });
  return res;
};

export const orderUpdate = async (data: any) => {
  const res = await defHttp.post<any>({ url: Api.UPDATE, data });
  return res;
};

export const orderAuth = (data: any) =>
  defHttp.post<any>({ url: Api.FIELD_AUTH, data }, { message: '操作成功' });

export const orderAnalysis = async (data: any) => {
  const res = await defHttp.post<any>({ url: Api.ANALYSIS, data });
  return res;
};

export const orderExport = async (data: any = {}) => {
  // const res = await defHttp.post<any>({ url: Api.EXPORT, data });
  const userStore = useUserStoreWithOut();

  const token = userStore.getToken;

  axios({
    url: '/basic-api' + Api.EXPORT,
    method: 'post',
    responseType: 'blob',
    headers: { Authorization: `Bearer ${token}` },
    data,
  })
    .then((response) => {
      // 创建一个新的 Blob 对象,它包含了服务器响应的数据(即你的 Excel 文件)
      const blob = new Blob([response.data]); // Excel 的 MIME 类型
      const downloadUrl = window.URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = downloadUrl;
      a.download = '订单.xlsx'; // 你可以为文件命名
      document.body.appendChild(a);
      a.click(); // 模拟点击操作来下载文件
      URL.revokeObjectURL(downloadUrl); // 释放掉 blob 对象所占用的内存
      document.body.removeChild(a);
    })
    .catch((error) => {
      // 处理错误
      console.error('导出错误', error);
    });
};

export const getInitDictData = async () => {
  const res = await defHttp.post({
    url: Api.DICT_INIT,
  });

  return res;
};

export async function uploadImg(params, onUploadProgress: (progressEvent: ProgressEvent) => void) {
  const res = await defHttp.uploadFile(
    {
      url: Api.UPLOAD,
      onUploadProgress,
    },
    {
      file: params.file,
      data: { name: params.file.name },
    },
  );
  return Promise.resolve({ data: { thumbUrl: res.data } });
}

export const getOrderList = async (params: DemoParams) => {
  const res = await defHttp.post<DemoListGetResultModel>({
    url: Api.ORDER,
    params,
    headers: {
      // @ts-ignore
      ignoreCancelToken: true,
    },
  });
  const orderStore = useOrderStoreWithOut();
  orderStore.setTotal(res.total);
  return new Promise((resolve) => {
    resolve({
      items: res.records,
      total: res.total,
    });
  });
};

export const dictCreate = async (data: any) => {
  const res = await defHttp.post<any>({ url: Api.DICT_ADD, data }, { message: '保存成功' });
  return res;
};

export const dictUpdate = async (data: any) => {
  const res = await defHttp.post<any>({ url: Api.DICT_UPDATE, data }, { message: '保存成功' });
  return res;
};

export const dictDelete = async (data: any) => {
  const res = await defHttp.post<any>({ url: Api.DICT_DELETE, data }, { message: '保存成功' });
  return res;
};

export const dictList = async (data: any) => {
  const res = await defHttp.post<any>({ url: Api.DICT_LIST, data: { ...data, pageSize: 1000 } });
  return res;
};