hytex
V2EX  ›  问与答

现在所有的列表接口都要添加导出,请问有什么好的解决方案吗?

  •  
  •   hytex · Mar 19, 2021 · 1329 views
    This topic created in 1970 days ago, the information mentioned may be changed or developed.

    今天加需求所有的列表接口都需要做导出 excel 功能
    导出简单,但是顶不住列表接口多,请问有什么方案或者方法可以尽量少的编写,然后适用到全部或者大部分列表接口吗? 谢谢。

    3 replies    2021-03-20 14:57:05 +08:00
    KouShuiYu
        1
    KouShuiYu  
       Mar 19, 2021
    我的做法:
    + 写了一个把返回的结果转 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));
    }
    ```
    baibaibaibai
        2
    baibaibaibai  
       Mar 20, 2021
    datatable->excel
    hytex
        3
    hytex  
    OP
       Mar 20, 2021
    @KouShuiYu 谢谢谢谢,不过我需要的后端的处理方法。前端这样子,我也去试试。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1001 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 19:05 · PVG 03:05 · LAX 12:05 · JFK 15:05
    ♥ Do have faith in what you're doing.