index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict'
  2. const fs = require('fs')
  3. const path = require('path')
  4. const division = require('./data/division.json')
  5. const street = require('./data/street.json')
  6. /**
  7. * 获取省份数据
  8. * @Author https://github.com/modood
  9. * @DateTime 2016-10-08 15:50
  10. */
  11. function getProvinces () {
  12. return division.filter(a => a[4] === '000000').map(a => { return { code: a[0], name: a[1] } })
  13. }
  14. /**
  15. * 获取城市数据
  16. * @Author https://github.com/modood
  17. * @DateTime 2016-10-08 17:58
  18. */
  19. function getCities () {
  20. return division.filter(a => a[2]).map(a => { return { code: a[0], name: a[2], parent_code: a[4] } })
  21. }
  22. /**
  23. * 获取区县数据
  24. * @Author https://github.com/modood
  25. * @DateTime 2016-10-08 18:10
  26. */
  27. function getAreas () {
  28. return division.filter(a => a[3]).map(a => { return { code: a[0], name: a[3], parent_code: a[4] } })
  29. }
  30. /**
  31. * 获取乡镇(街道)数据
  32. * @Author https://github.com/modood
  33. * @DateTime 2016-10-09 15:08
  34. */
  35. function getStreets () {
  36. return street.map(a => { return { code: a[0], name: a[1], pinyin: a[3], parent_code: a[2]} })
  37. }
  38. /**
  39. * 获取省市二级联动数据
  40. * @Author https://github.com/modood
  41. * @DateTime 2016-10-09 15:25
  42. */
  43. function getAddressPC () {
  44. const doc = {}
  45. const provinces = getProvinces()
  46. const cities = getCities()
  47. provinces.forEach(p => doc[p.name] = cities.filter(c => p.code === c.parent_code).map(c => c.name))
  48. return doc
  49. }
  50. /**
  51. * 获取省市区三级联动数据
  52. * @Author https://github.com/modood
  53. * @DateTime 2016-10-09 16:00
  54. */
  55. function getAddressPCA () {
  56. const doc = {}
  57. const provinces = getProvinces()
  58. const cities = getCities()
  59. const areas = getAreas()
  60. provinces.forEach(p => {
  61. doc[p.name] = {}
  62. cities.filter(c => p.code === c.parent_code).forEach(c => {
  63. doc[p.name][c.name] = areas.filter(a => c.code === a.parent_code).map(a => a.name)
  64. })
  65. })
  66. return doc
  67. }
  68. /**
  69. * 获取省市区镇四级联动数据
  70. * @Author https://github.com/modood
  71. * @DateTime 2016-10-09 16:09
  72. */
  73. function getAddressPCAS () {
  74. const doc = {}
  75. const provinces = getProvinces()
  76. const cities = getCities()
  77. const areas = getAreas()
  78. const streets = getStreets()
  79. provinces.forEach(p => {
  80. doc[p.name] = {}
  81. cities.filter(c => p.code === c.parent_code).forEach(c => {
  82. doc[p.name][c.name] = {}
  83. areas.filter(a => c.code === a.parent_code).forEach(a => {
  84. doc[p.name][c.name][a.name] = streets.filter(s => a.code === s.parent_code).map(s => s.name)
  85. })
  86. })
  87. })
  88. return doc
  89. }
  90. /**
  91. * 输出 JSON 文件
  92. * @Author https://github.com/modood
  93. * @DateTime 2016-10-08 17:16
  94. */
  95. function outputJSON () {
  96. fs.writeFileSync(path.resolve(__dirname, 'dist/provinces.min.json'), JSON.stringify(getProvinces()))
  97. fs.writeFileSync(path.resolve(__dirname, 'dist/cities.min.json'), JSON.stringify(getCities()))
  98. fs.writeFileSync(path.resolve(__dirname, 'dist/areas.min.json'), JSON.stringify(getAreas()))
  99. fs.writeFileSync(path.resolve(__dirname, 'dist/streets.min.json'), JSON.stringify(getStreets()))
  100. fs.writeFileSync(path.resolve(__dirname, 'dist/address2.min.json'), JSON.stringify(getAddressPC()))
  101. fs.writeFileSync(path.resolve(__dirname, 'dist/address3.min.json'), JSON.stringify(getAddressPCA()))
  102. fs.writeFileSync(path.resolve(__dirname, 'dist/address4.min.json'), JSON.stringify(getAddressPCAS()))
  103. console.log('It\'s saved!')
  104. }
  105. module.exports = {
  106. outputJSON,
  107. provinces: require(path.resolve(__dirname, 'dist/provinces.min.json')),
  108. cities: require(path.resolve(__dirname, 'dist/cities.min.json')),
  109. areas: require(path.resolve(__dirname, 'dist/areas.min.json')),
  110. streets: require(path.resolve(__dirname, 'dist/streets.min.json')),
  111. address2: require(path.resolve(__dirname, 'dist/address2.min.json')),
  112. address3: require(path.resolve(__dirname, 'dist/address3.min.json')),
  113. address4: require(path.resolve(__dirname, 'dist/address4.min.json')),
  114. }