/** * 同步微信小程序物流公司,生成初始化shipCompany数据到本地src/data/shipCompany.json中 */ import { WechatSDK, WechatMpInstance } from 'oak-external-sdk'; import fs from 'fs'; const appId = process.argv[2] || ''; const appSecret = process.argv[3] || ''; type ShipCompany = { id: string; name: string; abbr: string; wechatMpName: string; } function outputData(output: ShipCompany[]) { fs.writeFileSync(`${__dirname}/../src/data/shipCompany.json`, JSON.stringify(output)); console.log(`输出shipCompany.json完成,共输出了${output.length}行数据`); } async function main() { if (appId && appId !== '' && appSecret && appSecret !== '') { console.log(`准备输出appId为${appId},appSecret为${appSecret}的小程序请求的运力列表`); const wechatInstance = WechatSDK.getInstance( appId, 'wechatMp', appSecret, ) as WechatMpInstance; const result = await wechatInstance.getDeliveryList(); const { errcode, delivery_list, count } = result; const output = [] as ShipCompany[]; if (errcode === 0) { const deliveryList = delivery_list?.map((ele: { delivery_id: string, delivery_name: string, }) => { if (ele.delivery_id === 'FEDEX') { //处理name:FEDEX联邦(国内件),id:FEDEX与name:Fedex,id:Fedex两者id重复问题 return { id: ele.delivery_id + 'CN', name: ele.delivery_name, abbr: ele.delivery_id, wechatMpName: ele.delivery_id, } as ShipCompany } else { return { id: ele.delivery_id, name: ele.delivery_name, abbr: ele.delivery_id, wechatMpName: ele.delivery_id, } as ShipCompany } }); output.push(...deliveryList); outputData(output); return; } else { console.error(`请求小程序接口失败,返回码为${errcode}`) } } else { console.log('请输入小程序appId与appSecret') } } main().then(() => console.log("success"));