//文件下载
var fs = require("fs");
var path = require("path");
var request = require("request");
var download = function (item) {
let url="https://geo.datav.aliyun.com/areas_v3/bound/100000_full.json"
// `https://geo.datav.aliyun.com/areas_v3/bound/${item.cid}_full.json`
request(`https://geo.datav.aliyun.com/areas_v3/bound/${item.cid}_full.json`, function (error, response, body) {
if (!error && response.statusCode == 200) {
// console.log(body);
body = 'export default' + body
fs.writeFile(`D:/map/${item.key}.ts`, body, 'utf8', function (err) {
//如果err=null,表示文件下载成功,否则失败
if (err)
console.log('写文件出错了,错误是:' + err);
else
console.log('ok');
})
}
})
}
var citys = [
{
"key": "beijing",
"cid": "110000"
},
{
"key": "tianjin",
"cid": "120000"
},
{
"key": "hebei",
"cid": "130000"
},
{
"key": "shanxi",
"cid": "140000"
},
{
"key": "neimenggu",
"cid": "150000"
},
{
"key": "liaoning",
"cid": "210000"
},
{
"key": "jilin",
"cid": "220000"
},
{
"key": "heilongjiang",
"cid": "230000"
},
{
"key": "shanghai",
"cid": "310000"
},
{
"key": "jiangsu",
"cid": "320000"
},
{
"key": "zhejiang",
"cid": "330000"
},
{
"key": "anhui",
"cid": "340000"
},
{
"key": "fujian",
"cid": "350000"
},
{
"key": "jiangxi",
"cid": "360000"
},
{
"key": "shandong",
"cid": "370000"
},
{
"key": "henan",
"cid": "410000"
},
{
"key": "hubei",
"cid": "420000"
},
{
"key": "hunan",
"cid": "430000"
},
{
"key": "guangdong",
"cid": "440000"
},
{
"key": "guangxi",
"cid": "450000"
},
{
"key": "chongqing",
"cid": "500000"
},
{
"key": "sichuan",
"cid": "510000"
},
{
"key": "guizhou",
"cid": "520000"
},
{
"key": "yunnan",
"cid": "530000"
},
{
"key": "xizang",
"cid": "540000"
},
{
"key": "shaanxi",
"cid": "610000"
},
{
"key": "gansu",
"cid": "620000"
},
{
"key": "qinghai",
"cid": "630000"
},
{
"key": "ningxia",
"cid": "640000"
},
{
"key": "xinjiang",
"cid": "650000"
},
{
"key": "taiwan",
"cid": "710000"
},
{
"key": "xianggang",
"cid": "810000"
},
{
"key": "aomen",
"cid": "820000"
}
];
console.log(citys.length)
let china={
"key": "china",
"cid": "100000"
}
// download(china)
citys.forEach(function (item) {
download(item)
})
fs基本使用
最新推荐文章于 2024-09-29 13:58:38 发布