index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. * 输出 JSON 文件
  52. * @Author https://github.com/modood
  53. * @DateTime 2016-10-08 17:16
  54. */
  55. function outputJSON () {
  56. fs.writeFileSync(path.resolve(__dirname, 'dist/provinces.json'), JSON.stringify(getProvinces()))
  57. fs.writeFileSync(path.resolve(__dirname, 'dist/cities.json'), JSON.stringify(getCities()))
  58. fs.writeFileSync(path.resolve(__dirname, 'dist/areas.json'), JSON.stringify(getAreas()))
  59. fs.writeFileSync(path.resolve(__dirname, 'dist/streets.json'), JSON.stringify(getStreets()))
  60. console.log('It\'s saved!')
  61. }
  62. module.exports = {
  63. getProvinces,
  64. getCities,
  65. getAreas,
  66. getStreets,
  67. outputJSON,
  68. }