KouShuiYu
2021-03-19 18:36:56 +08:00
我的做法:
+ 写了一个把返回的结果转 table 文件的函数
+ 利用原请求函数返回文件
代码差不多长这样
```js
// 允许不分页
async index() {
const { ctx, service } = this;
const mixedParams = this.getMixedParams();
ctx.noPaging = ctx.noPaging || !!mixedParams.noPaging;
const rules = {
...optionalRules({
page: RULES.page,
limit: RULES.limit,
ids: RULES.intArray,
advIds: RULES.intArray,
advOfferIds: RULES.strArray,
manauls: RULES.manaulsTypeEnum,
}),
};
const error = ctx.parameter.validate(rules, mixedParams);
if (error) {
return ctx.error(ctx.ERROR.ILLEGAL_PARAMETER, error);
}
const { page, limit } = mixedParams;
const resList = await service.offer.list(mixedParams);
return ctx.ok(Object.assign(resList, { page, limit }));
}
async download() {
const { ctx } = this;
ctx.noPaging = true;
const result = await this.index();
if (!result || result.code !== 0) {
return ctx.error(result);
}
const fromatRows = result.data.rows.map(val => {
return val;
});
return ctx.file('offer.xlsx', rows2XlsxBuffer(fromatRows));
}
```