async handleDownload(row) { try { const res = await this.$api.downloadFileById(row.id); if (res.status === 200) { const fileUrl = window.URL.createObjectURL(new Blob([res.data])); const link = document.createElement('a');//创建a标签 link.style.display = 'none'; //将a标签隐藏 link.href = fileUrl; link.setAttribute('download','' ); link.target = '_blank'; link.click(); } else { console.log('Something went wrong'); } } catch (error) { console.error('Error occurred while downloading file:', error); } },这段代码中,responseType:'blob这行代码写哪
时间: 2024-01-09 15:05:44 浏览: 154
在你调用`this.$api.downloadFileById(row.id)`时,应该把`responseType: 'blob'`作为配置项传入。例如:
```
const res = await this.$api.downloadFileById(row.id, { responseType: 'blob' });
```
这样可以确保返回的响应数据是一个 Blob 对象。
相关问题
把这两个页面使用Composition API重构要求用vue2
将 `ProjectMaterialStatistics.vue` 和 `ProjectMaterialDetails.vue` 使用 Composition API 进行重构,并且保证在 Vue 2 中运行,可以按照以下步骤进行:
### 1. 安装 `@vue/composition-api`
首先,需要安装 `@vue/composition-api` 插件,以便在 Vue 2 中使用 Composition API。
```bash
npm install @vue/composition-api
```
### 2. 修改主文件引入插件
在 `main.js` 或者 `main.ts` 文件中引入并注册 `@vue/composition-api`。
```javascript
import Vue from 'vue';
import App from './App.vue';
import '@vue/composition-api';
Vue.config.productionTip = false;
new Vue({
render: h => h(App),
}).$mount('#app');
```
### 3. 重构 `ProjectMaterialStatistics.vue`
#### 原始代码结构
原始代码使用了 Options API,我们需要将其转换为 Composition API。
#### 重构后的代码
```vue
<template>
<a-spin :spinning="spinning" tip="报表同步中,请稍等">
<a-card :bordered="false">
<!-- 查询区域 -->
<div class="table-page-search-wrapper">
<a-form layout="inline" @keyup.enter.native="searchQuery">
<a-row :gutter="24">
<a-col>
<my-fast-date-picker
:radioList="[{
value: 2,
text: '月'
}, {
value: 3,
text: '年'
}]"
:radioVal="2"
:currName="'近半年'"
:showRadioGroup="true"
:fastTime="fastTime"
@change="change"
>
<template v-slot:text>时间:</template>
</my-fast-date-picker>
</a-col>
<template v-if="toggleSearchStatus">
<a-col :xl="6" :lg="7" :md="8" :sm="24">
<a-form-item label="项目编号:">
<a-input placeholder="请输入项目编号" allow-clear v-model="queryParam.projectCode"></a-input>
</a-form-item>
</a-col>
<a-col :xl="6" :lg="7" :md="8" :sm="24">
<a-form-item label="项目名称">
<a-select
allow-clear
show-search
v-model="queryParam.projectName_Code"
placeholder="请输入项目名称"
style="width: 100%"
:show-arrow="false"
:filter-option="false"
:not-found-content="null"
@search="projectCodeSearch"
@change="projectCodeChange"
>
<a-select-option v-for="d in projectNameList" :key="d.id">{{ d.text }}</a-select-option>
</a-select>
</a-form-item>
</a-col>
<a-col :xl="6" :lg="7" :md="8" :sm="24">
<a-form-item label="物资">
<a-input placeholder="请输入物资编号/名称" allow-clear v-model="queryParam.keyWord"></a-input>
</a-form-item>
</a-col>
<a-col :xl="6" :lg="7" :md="8" :sm="24">
<a-form-item label="厂商">
<j-search-select-tag
placeholder="请选择厂商"
v-model="queryParam.producerId"
dict="biz_product_vendor,abbreviation,id,del_flag = 0 and status = 1"
/>
</a-form-item>
</a-col>
<a-col :xl="6" :lg="7" :md="8" :sm="24">
<a-form-item label="规格型号">
<a-input placeholder="请输入规格型号" allow-clear v-model="queryParam.specification"></a-input>
</a-form-item>
</a-col>
</template>
<a-col :xl="6" :lg="7" :md="8" :sm="24">
<span style="float: left; overflow: hidden;" class="table-page-search-submitButtons">
<a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
<a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
<a @click="handleToggleSearch" style="margin-left: 8px">{{ toggleSearchStatus ? '收起' : '展开' }}
<a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
</a>
</span>
</a-col>
</a-row>
</a-form>
</div>
<!-- 查询区域-END -->
<!-- 操作按钮区域 -->
<div class="table-operator">
<div>
<a-button type="primary" icon="download" @click="handleExportXlsx()">导出</a-button>
</div>
</div>
<!-- table区域-begin -->
<div>
<a-table
ref="table"
size="middle"
:scroll="{ x: true }"
bordered
row-key="uniqueId"
:columns="columns"
:data-source="dataSource"
:pagination="ipagination"
:loading="loading"
class="j-table-force-nowrap"
@change="handleTableChange"
>
<span slot="receivingFreezeAmount">
领用冻结
<a-tooltip>
<template slot="title">
项目领用申请审核通过后,待出库的数量。
</template>
<a-icon type="info-circle" />
</a-tooltip>
</span>
<span slot="receivingFreezeAmount_title">
预算余量
<a-tooltip>
<template slot="title">
预算余量=预算总数-领用冻结-领用总数+领用归还
</template>
<a-icon type="info-circle" />
</a-tooltip>
</span>
<span slot="requisitionAmount_title">
领用总数
<a-tooltip>
<template slot="title">
项目盘盈更新领用余料时,会自动产生领用增补单(不受预算限制),可能导致领用总数超过预算总数。
</template>
<a-icon type="info-circle" />
</a-tooltip>
</span>
<span slot="budgetTotalAmount" slot-scope="text, record">
<a-tooltip>
<template slot="title">
<div class="tipItem">
<div>预算编号</div>
<div>预算数</div>
</div>
<div v-for="(value, key) in record.budgetDetail" class="tipItem" v-show="key !== '小计'">
<div>{{ key }}</div>
<div>{{ value }}</div>
</div>
<div class="tipItem">
<div>小记</div>
<div>{{ text }}</div>
</div>
</template>
<span style="color: blue">{{ text }}</span>
</a-tooltip>
</span>
<span slot="budgetSurplusAmount" slot-scope="text, record">
<span :style="{ color: record.budgetSurplusAmount <= projectBudgetMarginWarning ? 'red' : 'black' }">
{{ text }}
</span>
</span>
<span slot="inventoryAmount" slot-scope="text, record">
<span :style="{ color: record.inventoryAmount <= record.budgetSurplusAmount ? 'red' : 'black' }">
{{ text }}
</span>
</span>
</a-table>
</div>
</a-card>
</a-spin>
</template>
<script>
import { reactive, computed, onMounted, watch } from '@vue/composition-api';
import { getAction, postAction } from '@/api/manage';
import MyFastDatePicker from '@/components/date/MyFastDatePicker';
import '@/assets/less/TableExpand.less';
import { mixinDevice } from '@/utils/mixin';
import { JeecgListMixin } from '@/mixins/JeecgListMixin';
export default {
name: 'ProjectMaterialStatistics',
components: {
MyFastDatePicker,
},
setup(props, { root }) {
const state = reactive({
projectNameList: [],
projectName_Code: '',
projectBudgetMarginWarning: 0,
expandedRowKeys: [],
workorderProjectList: [],
description: '营业报表管理页面',
fastTime: ['本月', '上月', '近半年'],
noCreate: true,
hasUpdateTime: true,
configOrderSubTypeList: [],
configOrderSubTypeTemp: undefined,
allDepartList: [],
hallCodeDataList: [],
spinning: false,
checkFlag: true,
updateTime: '',
url: {
list: '/web/projectUseMaterial/list',
exportXlsxUrl: '/web/projectUseMaterial/export',
exportPdfUrl: '/web/bizBusinessRecord/exportpdf',
exportDetailUrl: '/web/bizBusinessRecord/exportDetailExcel',
},
keyList: ['saleNum', 'saleName', 'terminalTypeName', 'userTypeName', 'configOrderMainTypeName', 'configOrderSubTypeName'],
ipagination: {
current: 1,
pageSize: 20,
pageSizeOptions: ['20', '50', '80'],
showTotal(total, range) {
return `${range[0]}-${range[1]} 共${total}条`;
},
showQuickJumper: true,
showSizeChanger: true,
total: 0,
},
columns: [
{ title: '项目编号', align: 'center', dataIndex: 'projectCode', customRender: (text, row, index) => renderMergedCells(text, row, index) },
{ title: '项目名称', align: 'center', dataIndex: 'projectName', customRender: (text, row, index) => renderMergedCells(text, row, index) },
{ title: '物资编号', align: 'center', dataIndex: 'materialCode' },
{ title: '物资名称', align: 'center', dataIndex: 'materialName' },
{ title: '厂商', align: 'center', dataIndex: 'producerId_dictText' },
{ title: '规格型号', align: 'center', dataIndex: 'specification' },
{ title: '单位', align: 'center', dataIndex: 'unit' },
{ title: '预算总数', align: 'center', dataIndex: 'budgetTotalAmount', scopedSlots: { customRender: 'budgetTotalAmount' } },
{ align: 'center', slots: { title: 'receivingFreezeAmount' }, dataIndex: 'receivingFreezeAmount' },
{ align: 'center', slots: { title: 'requisitionAmount_title' }, dataIndex: 'receivingTotalAmount' },
{ title: '领用余料', align: 'center', dataIndex: 'receivingSurplusAmount' },
{ title: '领料使用', align: 'center', dataIndex: 'receivingUseAmount' },
{ align: 'center', dataIndex: 'budgetSurplusAmount', slots: { title: 'receivingFreezeAmount_title' }, scopedSlots: { customRender: 'budgetSurplusAmount' } },
{ title: '库存可用数', align: 'center', dataIndex: 'inventoryAmount', scopedSlots: { customRender: 'inventoryAmount' } },
],
queryParam: {
projectCode: '',
keyWord: '',
producerId: '',
specification: '',
projectName_Code: '',
projectName: '',
},
toggleSearchStatus: false,
dataSource: [],
loading: false,
});
const renderMergedCells = (value, row, index) => {
const obj = { children: value, attrs: {} };
if (index === 0 || row.projectName !== state.dataSource[index - 1].projectName) {
let rowSpan = 1;
for (let i = index + 1; i < state.dataSource.length; i++) {
if (state.dataSource[i].projectName === row.projectName) {
rowSpan++;
} else {
break;
}
}
obj.attrs.rowSpan = rowSpan;
} else {
obj.attrs.rowSpan = 0;
}
return obj;
};
const projectCodeSearch = async (value) => {
const res = await getAction('/web/projectRecord/searchMainProject', { order: 'desc', pageNo: 1, pageSize: 100, keyword: value });
state.projectNameList = res.result.map(item => ({ id: item.id, value: item.projectCode, text: item.projectName }));
};
const projectCodeChange = (value) => {
const obj = state.projectNameList.filter(item => item.id === value);
if (obj.length > 0) {
state.exportParams.projectName = obj[0].text;
state.queryParam.projectName = obj[0].text;
} else {
state.exportParams.projectName = '';
state.queryParam.projectName = '';
}
};
const change = (val1, val2, val3) => {
state.exportParams.dateTpye = val3 === 2 ? 1 : 2;
state.queryParam.beginDate = val1;
state.queryParam.endDate = val2;
state.queryParam.hiddenZeroTotalAmountFlag = state.checkFlag ? 1 : 0;
};
const searchQuery = () => {
state.loadData(1);
};
const searchReset = () => {
state.queryParam.projectCode = '';
state.queryParam.keyWord = '';
state.queryParam.producerId = '';
state.queryParam.specification = '';
state.queryParam.projectName_Code = '';
state.queryParam.projectName = '';
state.loadData(1);
};
const handleToggleSearch = () => {
state.toggleSearchStatus = !state.toggleSearchStatus;
};
const handleExportXlsx = () => {
// 导出逻辑
};
const handleTableChange = (pagination, filters, sorter) => {
// 表格变化逻辑
};
const loadData = async (page) => {
state.loading = true;
try {
const res = await getAction(state.url.list, { ...state.queryParam, page });
state.dataSource = res.result.records;
state.ipagination.total = res.result.total;
} finally {
state.loading = false;
}
};
onMounted(() => {
loadData(1);
postAction('/web/sysCompany/queryLoginCompany').then(res => {
state.projectBudgetMarginWarning = res.result.projectBudgetMarginWarning || 0;
});
});
阅读全文