-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
74 lines (59 loc) · 1.69 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
let axios = require("axios");
let fs = require("fs");
let day = require('dayjs');
let _ = require('lodash');
let jsonFormat = require('json-format');
let defaultMapKey = { QQMapKey:'' };
let QQMapKey = ''
try{
QQMapKey = require('./QQMapKey.json').QQMapKey;
}catch(e){
fs.writeFileSync(__dirname + "/QQMapKey.json",jsonFormat(defaultMapKey));
}
if(!QQMapKey){
console.error('请在 QQMapKey.json 中填入腾讯地图开发密钥');
process.exit()
}
let url = "http://apis.map.qq.com/ws/district/v1/list?key=" + QQMapKey;
let oldChinaCityCode = {
updateDate:'',
codeMap:{}
};
try{
oldChinaCityCode = require('./districtsQQmap.json')
}catch(e){
oldChinaCityCode = {
updateDate:'',
codeMap:{}
};
}
let chinaCityCode = _.cloneDeep(oldChinaCityCode);
axios.get(url).then(({data,headers})=>{
// data = res.data;
if(data.status != 0){
throw Error(data.message)
}
chinaCityCode.updateDate = day(headers.date).format('YYYY-MM-DD HH:mm:ss')
let { result:[province,city,zone] } = data;
province.forEach((item)=>{
chinaCityCode.codeMap[item.id] = item.fullname;
})
city.forEach((item)=>{
chinaCityCode.codeMap[item.id] = item.fullname;
})
zone.forEach((item)=>{
chinaCityCode.codeMap[item.id] = item.fullname;
})
if(!_.isEqual(oldChinaCityCode.codeMap,chinaCityCode.codeMap)){
fs.writeFile(__dirname + "/districtsQQmap.min.json", JSON.stringify(chinaCityCode), (err) => {
if (err) throw err;
});
fs.writeFile(__dirname + "/districtsQQmap.json",jsonFormat(chinaCityCode), (err) => {
if (err) throw err;
});
}else{
console.log('行政区域信息没有更新');
}
}).catch((error)=>{
console.log(error)
})