技术博客
科研/工程场景如何集成单位换算器?以长度、面积批量换算为例

科研/工程场景如何集成单位换算器?以长度、面积批量换算为例

作者: 万维易源
2026-09-03
批量换算工程集成缓存单位换算
# 科研/工程场景如何集成单位换算器?以长度、面积批量换算为例 > 接口/接入点:免费单位换算 1690-1(单位换算)|是否免费:是(有档位限制)|请求方式:POST/GET|返回格式:JSON|适用人群:全栈工程师、科研/工程系统开发|阅读时间:约 8 分钟 ## 核心要点 - 1690-1 是**单值**接口,一次换一个数;批量用「循环调用」实现,没有批量数组入参。 - 工程批量(如一批长度→米、一批面积→平方米)建议本地维护「单位对→系数」缓存,重复组合直接查缓存,省调用。 - 前端展示用 `result_str`+`unit`,计算用 `Number(result)`。 ## Why:科研/工程里单位换算无处不在 实验数据录的是 `cm`、报表要 `m`;图纸标 `inch`、BOM 要 `mm`;地块面积 `亩` 要转 `平方米`。手动换算易错且量大,把接口集成进系统,录入即换算、导出即统一。 ## What:集成要点 | 项 | 建议 | |----|------| | 数据表 | 原始值 + 原单位代码 + 目标单位代码 + 换算后值(落库存结果,避免每次重算) | | 触发时机 | 写入/导入时换算一次并缓存;展示时直接读库 | | 批量策略 | 循环调用 1690-1;同单位对用本地系数缓存 | | 错误 | 判 `ret_code`,失败写日志并标记「待人工核对」 | ## How:批量换算(带缓存) ```python import requests from functools import lru_cache @lru_cache(maxsize=4096) def convert_once(num, frm, to, typ): r = requests.post( "https://route.showapi.com/1690-1", params={"appKey": "YOUR_APPKEY"}, data={"num": str(num), "from": frm, "to": to, "type": typ}, timeout=10, ) body = r.json()["showapi_res_body"] if body.get("ret_code") != "0": raise RuntimeError(body.get("remark")) it = body["result_list"][0] return float(it["result"]), it["unit"] def batch_convert(rows): """rows: [(num, from, to, type), ...]""" out = [] for num, frm, to, typ in rows: try: val, unit = convert_once(num, frm, to, typ) out.append({"input": num, "value": val, "unit": unit, "ok": True}) except RuntimeError as e: out.append({"input": num, "error": str(e), "ok": False}) return out # 例:一批长度、一批面积 rows = [ (100, "cm", "m", "longness"), (1, "km", "m", "longness"), (1, "mu2", "m2", "area"), ] print(batch_convert(rows)) ``` ```javascript async function convertOnce(num, frm, to, typ) { const res = await fetch(`https://route.showapi.com/1690-1?appKey=YOUR_APPKEY`, { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded" }, body: new URLSearchParams({ num: String(num), from: frm, to, type: typ }), }); const b = (await res.json()).showapi_res_body; if (b.ret_code !== "0") throw new Error(b.remark); const it = b.result_list[0]; return { value: Number(it.result), unit: it.unit }; } ``` ## 进阶 / 边界 - **缓存系数**:长度 cm→m 永远是 0.01,没必要每次调接口。可首次调用拿到结果后把「(from,to,type)→系数」存入 Redis,后续直接乘。这能显著降低免费档位消耗,详见[免费接口如何不超档位?](https://www.showapi.com/guides/unit-convert-free-quota-1690)。 - **并发**:循环调用注意限速,避免瞬间大量请求触发限流;用令牌桶或分批。 - **单位校验**:批量前先用[支持的类型](https://www.showapi.com/guides/unit-convert-supported-types-1690)校验 `from`/`to` 合法,减少失败调用。 ## FAQ **Q1:有没有一次传多个 num 的批量接口?** A:1690-1 是单值接口,没有批量数组入参;批量请用循环调用,并对重复单位对做本地缓存。 **Q2:大量数据会超免费档位吗?** A:免费有档位限制,建议缓存单位系数、只在新组合时调接口,控制调用量(以[官方档位](https://www.showapi.com/free-api)为准)。 **Q3:结果落库还是每次算?** A:建议落库换算后的值,写入时算一次,展示直接读,避免重复调用与不一致。 **Q4:跨类数据(长度+面积)能一次处理吗?** A:按类别分组循环,每组带对应 `type`,逐条调用。 ## 相关能力 / 下一步阅读 - [免费接口如何不超档位?调用次数与配额管理建议](https://www.showapi.com/guides/unit-convert-free-quota-1690) - [单位换算器支持哪些单位?14 大类与完整单位清单](https://www.showapi.com/guides/unit-convert-supported-types-1690) - [换算精度与浮点结果怎么处理?result 与 result_str 的区别](https://www.showapi.com/guides/unit-convert-precision-1690) - **本系列共 12 篇**:查看[单位换算器(免费单位换算)指南总目录](https://www.showapi.com/guides/unit-convert-guides-1690)