liuying
2024-11-26 dc7a24a919d03843969900e775b5c3935cad453f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { exportExcel } from "/@/utils/exportExcelForDetail";
import { formatUtcToData } from "/@/utils/formate";
import { ElMessage } from "element-plus";
import { ref } from "vue";
/***
 * 
 前端导出:因elementui没有提供方法获取表头字段:使用summary-method动态获取table表头,用于导出excell
    1)  
     <!-- 按照搜索条件导出 -->
    <el-button  plain icon="el-icon-upload2" @click="handleExportExcell" class="btn-export">导出</el-button>
 
 
    2) :summary-method="getExportTitle" show-summary class="tableBox">
 
    3) import { ExcellTableDataExport} from "@/hooks/exportTableDataExcell";
    4)  // getTabelData 方法中添加
          // 导出参数
          formExport.value = JSON.parse(JSON.stringify(formModel.value)) 
    5)
    // ------------封装导出功能1--前端导出--根据搜索条件导出---------------
    const jsonExcellTableExport = {
        titleName: "用户信息表", //导出表格名称
        interfaceListName:getUsersList,//导出接口名称
        changeTitleName:((arr:any)=>{ // 需要前端转义的字段
          arr.forEach((item:any)=>{
            item.status =( item.status  == 0 ? '启用' : '冻结') 
          })
        })
    }
    const { getExportTitle , handleExportExcell, formExport} = ExcellTableDataExport(jsonExcellTableExport)
   // --------------end 封装导出功能1--根据搜索条件导出-----------------
 
 ***/
export function ExcellTableDataExport(_paramObj: any) {
  const {
    titleName, //导出表格名称
    interfaceListName, //导出接口名称
    changeTitleName //需要前端转中文
  } = _paramObj;
  const formExport = ref({
    PageSize: 10,
    Page: 1,
    OrderType:""
  });
  const entozhExcell:any = {
    // id: "序号"
  };
  // 获取表格标题
  const getExportTitle = (param: any) => {
    const { columns } = param;
    columns.forEach((column: any, index: any) => {
      if (column.label && column.label != "操作" && column.label != "序号") {
        entozhExcell[column.property] = column.label;
      }
    });
    // console.log(entozhExcell);
    return "";
  };
 
  const handleExportExcell = () => {
    const { PageSize, PageNo, OrderType, ...rest } = formExport.value;
    // const arrZ: any = Object.values(rest).join("");
    // if (arrZ.length <= 0) {
    //   ElMessage.warning(`请输入查询条件!`);
    //   return;
    // }
    formExport.value.PageNo = 1
    formExport.value.PageSize = 100000
 
    interfaceListName(formExport.value)
      .then((res: { code?: any; message?: any; data?: any }) => {
        if (res.code == 200) {
          const { data } = res;
          const result = data;
          if (changeTitleName) {
            changeTitleName(result.rows);
          }
          exportExcel(
            result.rows,
            entozhExcell,
            "xlsx",
            `${titleName} ${formatUtcToData(
              new Date() + "",
              "YYYY-MM-DD hh:mm:ss"
            )}`
          );
        } else {
          ;
        }
      })
      .catch((err: any) => console.log(err));
  };
  return {
    getExportTitle,
    handleExportExcell,
    formExport,
    entozhExcell
  };
}
 
 
/**
 * 
 *  后端导出 - 获取所有单号
 *    
  1) 
  <!-- 按照查询条件导出 -->
  <el-button  plain icon="el-icon-upload2" @click="handExportOrders" class="btn-export">导出</el-button>
  2)   import { backExportOrders} from "@/hooks/exportTableDataExcell";
  3) // getTabelData 方法中添加
      // 导出参数
      formExport.value = JSON.parse(JSON.stringify(formModel.value)) 
  4) 
     // ------------封装导出功能2-- 获取所有单号--根据搜索条件导出---------------
   const jsonBackExportOrders = {
        title:'PO单', //导出文件名称
        paramName:'PurchaseNo', //导出接口参数名称
        interfaceListName:getPurchasePage,//查询所有单号接口名称
        interfaceListNameExport:puchaseExport, //后端导出接口名称entozhTitle:{
           id: "序号",
          orderNo: "单号",
        }
    }
    const { handExportOrders ,formExport} = backExportOrders(jsonBackExportOrders)
   // --------------end 封装导出功能2-- 获取所有单号--根据搜索条件导出-----------------
 
 */
export function backExportOrders(_paramObj: any) {
  const {
    title,
    paramName,
    interfaceListName, //导出接口名称
    interfaceListNameExport,
    entozhTitle
  } = _paramObj;
  // 查询参数
  const formExport = ref({
    PageSize: 1,
    PageNo: 10,
    pagesize: 1,
    pageno: 10,
    MaterialTypeStaus: "",
    OrderType:""
  });
  // 导出方法
  const handExportOrders = () => {
    const { PageSize, PageNo, pagesize, pageno, MaterialTypeStaus, OrderType, ...rest } =
      formExport.value;
    // const arrZ: any = Object.values(rest).join("");
    // console.log(arrZ);
    // if (arrZ.length <= 0) {
    //   ElMessage.warning(`请输入查询条件!`);
    //   return;
    // }
    formExport.value.PageSize = 1000;
    formExport.value.PageNo = 1;
    // 查询列表接口
    interfaceListName({
      ...formExport.value
    })
      .then((res: { code?: any; message?: any; data?: any }) => {
        if (res.code == 200) {
          const { data } = res;
          const result = data;
          if (result.rows.length <= 0) {
            ElMessage.warning(`没有需要导出的数据!`);
            return;
          }
          let arr = [];
          if (paramName == "PurchaseNo") {
            arr = result.rows.reduce((curr: any, item: any) => {
              curr.push(item.purchaseNo);
              return curr;
            }, []);
          }else{
            arr = result.rows.reduce((curr: any, item: any) => {
              curr.push(item.orderNo);
              return curr;
            }, []);
          }
          const paramAll:any ={}
          paramAll[paramName] = arr
          // 导出接口
          if(arr.length<=0){
            ElMessage.warning(`单号不能为空!`);
            return;
          }
          interfaceListNameExport(paramAll)
            .then((res: any) => {
              if(entozhTitle){  //后端导出接口,返回list非文件,需要传入中文表头
                exportExcel(res.data, entozhTitle, "xlsx", `${title}${formatUtcToData(new Date()+'','YYYY-MM-DD hh:mm:ss')}`);
              }else{ //后端导出返回文件
                const link = document.createElement("a"); //创建a标签
                const blob = new Blob([res], {
                  type: "application/vnd.ms-excel"
                }); // response就是接口返回的文件流
                const objectUrl = URL.createObjectURL(blob);
                link.href = objectUrl;
                link.download = `${title}导出${formatUtcToData(
                  new Date().toString(),
                  "YYYY-MM-DD hh:mm:ss"
                )}`; // 自定义文件名
                link.click(); // 下载文件
                URL.revokeObjectURL(objectUrl); // 释放内存
              }
              
            })
            .catch((err: any) => ElMessage.error(JSON.stringify(err)));
        } else {
          ;
        }
      })
      .catch((err: any) => console.log(err));
  };
  return {
    handExportOrders,
    formExport
  };
}