format.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. const _ = require('lodash')
  2. const { Province, City, Area, Street } = require('./sqlite')
  3. const cField = ['code', 'name']
  4. const fField = cField.concat('children')
  5. /**
  6. * 获取省市二级联动数据
  7. * @author https://github.com/modood
  8. * @datetime 2018-02-01 22:08
  9. */
  10. exports.getAddressPC = async () => {
  11. const res = await Province.findAll({
  12. include: [{ model: City,
  13. include: [{ model: Area }] }] })
  14. const count = res.length
  15. let index = 0
  16. // 特殊处理:出于实用性考虑,过滤没有太大用处的二级名称,
  17. // 并使用其第三级(县级)补进
  18. const f = ['市辖区', '县', '省直辖县级行政区划', '自治区直辖县级行政区划']
  19. const r = {}
  20. const rC = _.map(res, p => {
  21. index++
  22. const pd = p.dataValues
  23. const { code, name } = pd
  24. log(index, count, code, name, 2)
  25. const arr = []
  26. pd.children = _.map(_.filter(p.cities, c => {
  27. const { dataValues: { name }, areas } = c
  28. if (f.includes(name)) {
  29. arr.push(..._.map(areas, a => _.pick(a.dataValues, cField)))
  30. return false
  31. }
  32. return true
  33. }), c => _.pick(c.dataValues, cField))
  34. pd.children.push(...arr)
  35. r[pd.name] = pd.children.map(c => c.name)
  36. return _.pick(pd, fField)
  37. })
  38. return [r, rC]
  39. }
  40. /**
  41. * 获取省市区三级联动数据
  42. * @author https://github.com/modood
  43. * @datetime 2018-02-01 22:13
  44. */
  45. exports.getAddressPCA = async () => {
  46. const res = await Province.findAll({
  47. include: [{ model: City,
  48. include: [{ model: Area }] }] })
  49. // 特殊处理:中山市、东莞市、儋州市和嘉峪关市没有第三级(县级),
  50. // 嘉峪关市有第三级,但是有且只有一个(市辖区:620201),
  51. // 出于实用性考虑,使用第四级(乡级)补进
  52. const f = ['4419', '4420', '4604', '6202']
  53. const streets = await Promise.all(
  54. _.map(f, cityCode => Street.findAll({ where: { cityCode } })))
  55. const count = res.length
  56. let index = 0
  57. const r = {}
  58. const rC = _.map(res, p => {
  59. index++
  60. const pd = p.dataValues
  61. const { code, name } = pd
  62. r[pd.name] = {}
  63. log(index, count, code, name, 3)
  64. pd.children = _.map(p.cities, c => {
  65. const cd = c.dataValues
  66. const { code, name } = cd
  67. log(index, count, code, name, 3)
  68. const idx = f.indexOf(code)
  69. if (idx !== -1) {
  70. cd.children = _.map(streets[idx], s => {
  71. // 特殊处理:第四级(乡级)过滤掉“办事处”后缀
  72. const sd = s.dataValues
  73. sd.name = sd.name.replace('办事处', '')
  74. return _.pick(sd, cField)
  75. })
  76. } else {
  77. cd.children = _.map(c.areas, a =>
  78. _.pick(a.dataValues, cField))
  79. }
  80. r[pd.name][cd.name] = _.map(cd.children, i => i.name)
  81. return _.pick(cd, fField)
  82. })
  83. return _.pick(pd, fField)
  84. })
  85. return [r, rC]
  86. }
  87. /**
  88. * 获取省市区镇四级联动数据
  89. * @author https://github.com/modood
  90. * @datetime 2018-02-02 09:51
  91. */
  92. exports.getAddressPCAS = async () => {
  93. const res = await Province.findAll({
  94. include: [{ model: City,
  95. include: [{ model: Area,
  96. include: [{ model: Street }] }] }] })
  97. const count = res.length
  98. let index = 0
  99. const r = {}
  100. const rC = _.map(res, p => {
  101. index++
  102. const pd = p.dataValues
  103. const { code, name } = pd
  104. r[pd.name] = {}
  105. log(index, count, code, name, 4)
  106. pd.children = _.map(p.cities, c => {
  107. const cd = c.dataValues
  108. const { code, name } = cd
  109. r[pd.name][cd.name] = {}
  110. log(index, count, code, name, 4)
  111. cd.children = _.map(c.areas, a => {
  112. const ad = a.dataValues
  113. const { code, name } = ad
  114. log(index, count, code, name, 4)
  115. ad.children = _.map(a.streets, s => {
  116. // 特殊处理:第四级(乡级)过滤掉“办事处”后缀
  117. const sd = s.dataValues
  118. sd.name = sd.name.replace('办事处', '')
  119. return _.pick(sd, cField)
  120. })
  121. r[pd.name][cd.name][ad.name] = _.map(ad.children, s => s.name)
  122. return _.pick(ad, fField)
  123. })
  124. return _.pick(cd, fField)
  125. })
  126. return _.pick(pd, fField)
  127. })
  128. return [r, rC]
  129. }
  130. function log (index, total, code, name, type) {
  131. if (index >= 0 && index <= 9) index = `0${index}`
  132. const clen = code.length
  133. if ((type === 3 && clen === 2) ||
  134. (type === 4 && clen === 4)) code = `${code} `
  135. else if (type === 4 && clen === 2) code = `${code} `
  136. let text = ''
  137. switch (type) {
  138. case 2:
  139. text = '正在格式化省市二级联动数据'
  140. break
  141. case 3:
  142. text = '正在格式化省市区三级联动数据,请耐心等候'
  143. break
  144. case 4:
  145. text = '正在格式化省市区镇四级联动数据,该步骤比较耗时,请耐心等候'
  146. break
  147. }
  148. console.log(`[${index}/${total}]${text} ${code} ${name}`)
  149. }