district.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import twData from './taiwan.json'
  2. import { formatCity } from './filterCity'
  3. export default {
  4. /**
  5. * 按省的城市编码排序
  6. */
  7. orderByCityCode(a, b) {
  8. return parseInt(a.value) - parseInt(b.value)
  9. },
  10. /**
  11. * 自定义app数据格式
  12. * @param {Array} addressList 高德地图api返回的城市数据
  13. * @param {boolean} noFilter 是否过滤省市区
  14. * @param {boolean} allProvice 是否显示全省
  15. * @param {boolean} allCity 是否显示全市
  16. * @param {boolean} allCountry 是否显示全国
  17. */
  18. buildAppJSONData({
  19. addressList,
  20. noFilter = false,
  21. allProvince = false,
  22. allCity = false,
  23. allCountry = false,
  24. }) {
  25. let array = []
  26. get(addressList, array, noFilter)
  27. array.forEach(item => {
  28. if (item.value === '710000') {
  29. if (item.children.length === 0) {
  30. item.children = twData
  31. }
  32. }
  33. })
  34. array.sort(this.orderByCityCode) // 加入台湾区域,并按省份城市代码排序
  35. const specialCitys = ['北京', '上海', '天津', '重庆', '香港', '澳门']
  36. if (array.length > 0) {
  37. array.forEach(item => {
  38. if (allCity) {
  39. item.children.forEach(city => {
  40. city.children.unshift({
  41. value: '0', label: '全市', children: []
  42. })
  43. })
  44. }
  45. if (allProvince) {
  46. if (specialCitys.indexOf(item.label) === -1) {
  47. item.children.unshift({
  48. value: '0', label: '全省', children: [
  49. {value: '0', label: '全省', children: []}
  50. ]
  51. })
  52. }
  53. }
  54. })
  55. if (allCountry) {
  56. array.unshift({
  57. value: '0', label: '全国', children: [
  58. {value: '0', label: '全国', children: [
  59. {value: '0', label: '全国', children: []}
  60. ]}
  61. ]
  62. })
  63. }
  64. }
  65. return array
  66. function get(districts, children, noFilter) {
  67. districts.forEach((item, index) => {
  68. children.push({
  69. value: noFilter === true ? item.adcode : item.name,
  70. label: noFilter === true ? formatCity(item.name) : item.name
  71. })
  72. if (item.districts.length != 0) {
  73. children[children.length - 1].children = []
  74. if (item.adcode === item.districts[0].adcode) {
  75. children[children.length - 1].children = [
  76. {
  77. value: noFilter === true ? item.adcode : item.name,
  78. label: noFilter === true ? formatCity(item.name) : item.name
  79. }
  80. ]
  81. } else {
  82. get(item.districts, children[children.length - 1].children, noFilter)
  83. }
  84. } else {
  85. // 特别处理香港,市区统一
  86. if (parseInt(item.adcode) > 810000 && parseInt(item.adcode) < 820000) {
  87. children[children.length - 1].children = [
  88. {
  89. value: noFilter === true ? item.adcode : item.name,
  90. label: noFilter === true ? formatCity(item.name) : item.name
  91. }
  92. ]
  93. }
  94. // 特别处理澳门,市区统一
  95. if (parseInt(item.adcode) > 820000 && parseInt(item.adcode) < 830000) {
  96. children[children.length - 1].children = [
  97. {
  98. value: noFilter === true ? item.adcode : item.name,
  99. label: noFilter === true ? formatCity(item.name) : item.name
  100. }
  101. ]
  102. }
  103. // 特别处理台湾
  104. if (parseInt(item.adcode) === 710000) {
  105. children[children.length - 1].children = []
  106. }
  107. }
  108. })
  109. }
  110. },
  111. getGaoDeData(key) {
  112. if (key) {
  113. return new Promise((resolve, reject) => {
  114. uni.request({
  115. method: 'get',
  116. url: `https://restapi.amap.com/v3/config/district?subdistrict=3&key=${key}`,
  117. success: res => {
  118. if (res.statusCode === 200) {
  119. // 构造匹配app,otms框架的城市JSON文件
  120. // const data = this.buildAppJSONData(, false, false)
  121. resolve(res.data.districts[0].districts)
  122. } else {
  123. reject(res)
  124. }
  125. },
  126. fail: res => {
  127. reject(res)
  128. }
  129. })
  130. })
  131. } else {
  132. uni.showModal({
  133. title: '提示',
  134. content: '请输入高德key'
  135. })
  136. }
  137. }
  138. }