try { const { Pool, Client } = require('pg') const request = require('superagent'); const Hex = require('crypto-js/enc-hex'); const MD5 = require('crypto-js/md5'); const XLSX = require('xlsx') const path = require('path') const fs = require("fs"); // 连接数据库 const pool = new Pool({ user: 'postgres', host: '10.8.30.32', database: 'highways4good', password: '123', port: 5432, }) let appid = '20200917000567738'; let key = 'xXm4jsuuD38JIkkhEcK6'; const getAnswer = async (query) => { let start = (new Date()).getTime(); let salt = start; let str1 = appid + query + salt + key; let sign = Hex.stringify(MD5(str1)); console.log(`翻译:${query}`); let answer = await request.get('http://api.fanyi.baidu.com/api/trans/vip/translate').timeout(1000 * 30).query({ q: query, appid: appid, salt: salt, from: 'zh', to: 'en', sign: sign }); if (answer.body.error_code) { console.warn(answer.body); throw '百度不给力,快快debug' } let rslt = answer.body.trans_result; // let upperCaseRslt = rslt[0].dst.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase()).replace(/ /g, ''); // let upperCaseRslt = rslt[0].dst.toUpperCase().replace(/ /g, '_'); // let upperCaseRslt = rslt[0].dst.toLowerCase().replace(/ /g, '_'); let upperCaseRslt = rslt[0].dst.replace(/\//g, ' ').replace(/'/g, '').replace(/\s{2,}/g, ''); console.log(`翻译结果:${upperCaseRslt}`); while (((new Date()).getTime() - start) < (1000 / 8)) {//每s只能调用10次 continue; } return upperCaseRslt } const fun = async () => { // note: we don't try/catch this because if connecting throws an exception // we don't need to dispose of the client (it will be undefined) const client = await pool.connect() try { await client.query('BEGIN') const fileList = [ // { // path: './data/道路/乡道第三方.xls', // n: '道路', // tableName: 'road' // }, // { // path: './data/运政/车辆/危货/江西昌海运输有限公司危货车辆信息表.xlsx', // n: '运政车辆', // tableName: 'municipal_vehicle' // }, // { // path: './data/运政/业户/危货/江西昌海运输有限公司危货业户信息表.xlsx', // n: '运政业户', // tableName: 'municipal_business' // } ] for (let f of fileList) { console.log(`读取 ${f.path}`); // 读取数据文件 let workbook = XLSX.readFile(path.join(__dirname, f.path)) let firstSheetName = workbook.SheetNames[0]; let worksheet = workbook.Sheets[firstSheetName]; let res = XLSX.utils.sheet_to_json(worksheet); console.log(res[0]); let dataEx = res[0]; transResult = '' sqlResult = '' transResolveResult = '' sql = ` -- ${f.n} create table if not exists "${f.tableName}" ( id serial not null ); create unique index if not exists ${f.tableName}_id_uindex on ${f.tableName} (id); alter table ${f.tableName} add constraint ${f.tableName}_pk primary key (id); ` let upperEngTArr = [] for (let t in dataEx) { const engT = await getAnswer(t); let upperEngT = engT .replace(/( |^)[a-z]/g, (L) => L.toUpperCase()) .replace(/ /g, '_') transResult += `"${t}" : "${upperEngT .replace(/_/g, '') .replace(/( |^)[A-Z]/g, (L) => L.toLowerCase()) }", \n ` sqlResult += `"${t}" : "${engT.trim().replace(/ /g, '_').replace(/( |^)[A-Z]/g, (L) => L.toLowerCase())}", \n ` transResolveResult += ` "${upperEngT .replace(/_/g, '') .replace(/( |^)[A-Z]/g, (L) => L.toLowerCase()) }":"${t}",\n ` sql += ` alter table ${f.tableName} add ${upperEngT} varchar(1024); comment on column ${f.tableName}.${upperEngT} is '${t}'; ` } fs.writeFileSync(`${f.n}_字段对应.json`, `{${transResult}}`, 'utf-8'); fs.writeFileSync(`${f.n}_数据脚本对应.sql`, sql, 'utf-8'); fs.writeFileSync(`${f.n}_数据库表对应.json`, `{${sqlResult}}`, 'utf-8'); fs.writeFileSync(`${f.n}_数据字段对应.json`, `{${transResolveResult}}`, 'utf-8'); } // await client.query('ROLLBACK') await client.query('COMMIT') console.log('执行完毕~') } catch (e) { await client.query('ROLLBACK') console.log('执行错误~') throw e } finally { client.release(); } } fun() } catch (error) { console.error(error) }