RegionPicker.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <view class="region-picker" @click="showPicker">
  3. <view class="picker-display">
  4. <text :class="['region-text', selectedText ? '' : 'placeholder']">
  5. {{ selectedText || placeholder }}
  6. </text>
  7. <u-icon class="arrow" name='arrow-right' size="18"></u-icon>
  8. </view>
  9. <picker mode="multiSelector" :range="range" range-key="name" :value="indexes" @change="onPickerChange"
  10. @columnchange="onColumnChange" :disabled="disabled">
  11. <view class="picker-mask"></view>
  12. </picker>
  13. </view>
  14. </template>
  15. <script setup>
  16. import {
  17. ref,
  18. computed,
  19. watch,
  20. nextTick,
  21. onMounted
  22. } from 'vue'
  23. import areaData from '@/utils/area-data.js'
  24. const props = defineProps({
  25. province: {
  26. type: String,
  27. default: ''
  28. },
  29. city: {
  30. type: String,
  31. default: ''
  32. },
  33. district: {
  34. type: String,
  35. default: ''
  36. },
  37. placeholder: {
  38. type: String,
  39. default: '省市区'
  40. },
  41. disabled: {
  42. type: Boolean,
  43. default: false
  44. }
  45. })
  46. const emit = defineEmits(['change', 'update:province', 'update:city', 'update:district'])
  47. // 省市区列表
  48. const provinces = ref([])
  49. const cities = ref([])
  50. const districts = ref([])
  51. // 选中索引
  52. const provinceIndex = ref(0)
  53. const cityIndex = ref(0)
  54. const districtIndex = ref(0)
  55. // picker range
  56. const range = computed(() => [provinces.value, cities.value, districts.value])
  57. const indexes = computed(() => [provinceIndex.value, cityIndex.value, districtIndex.value])
  58. // 显示文本
  59. const selectedText = computed(() => {
  60. if (provinceIndex.value >= 0 && provinces.value[provinceIndex.value]) {
  61. const p = provinces.value[provinceIndex.value]?.name || ''
  62. const c = cities.value[cityIndex.value]?.name || ''
  63. const d = districts.value[districtIndex.value]?.name || ''
  64. return `${p} ${c} ${d}`.trim()
  65. }
  66. return ''
  67. })
  68. // 初始化省份数据
  69. function initProvinces() {
  70. provinces.value = areaData.filter(item => item.type === 0).map(item => ({
  71. code: item.code,
  72. name: item.name
  73. }))
  74. if (provinces.value.length > 0) {
  75. provinceIndex.value = 0
  76. // 默认加载第一个省份的城市
  77. updateCities(provinces.value[0].code)
  78. }
  79. }
  80. // 根据省份代码更新城市列表
  81. function updateCities(provinceCode) {
  82. cities.value = areaData.filter(item => item.parent_code === provinceCode && item.type === 1).map(item => ({
  83. code: item.code,
  84. name: item.name
  85. }))
  86. if (cities.value.length > 0) {
  87. cityIndex.value = 0
  88. updateDistricts(cities.value[0].code)
  89. } else {
  90. cities.value = []
  91. districts.value = []
  92. }
  93. }
  94. // 根据城市代码更新区县列表
  95. function updateDistricts(cityCode) {
  96. districts.value = areaData.filter(item => item.parent_code === cityCode && item.type === 2).map(item => ({
  97. code: item.code,
  98. name: item.name
  99. }))
  100. if (districts.value.length > 0) {
  101. districtIndex.value = 0
  102. } else {
  103. districts.value = []
  104. }
  105. }
  106. // 根据省市区名称设置索引(用于回显)
  107. function setIndexesByNames(provinceName, cityName, districtName) {
  108. if (!provinceName || provinces.value.length === 0) return
  109. // 查找省份
  110. const provinceItem = provinces.value.find(p => p.name === provinceName)
  111. if (!provinceItem) return
  112. provinceIndex.value = provinces.value.indexOf(provinceItem)
  113. // 更新城市列表(同步执行)
  114. updateCities(provinceItem.code)
  115. // 等待城市列表更新后(由于 updateCities 是同步,但 cities 已更新),设置城市索引
  116. if (cityName) {
  117. const cityItem = cities.value.find(c => c.name === cityName)
  118. if (cityItem) {
  119. cityIndex.value = cities.value.indexOf(cityItem)
  120. // 更新区县列表
  121. updateDistricts(cityItem.code)
  122. // 等待区县列表更新后设置区县索引
  123. if (districtName) {
  124. const districtItem = districts.value.find(d => d.name === districtName)
  125. if (districtItem) {
  126. districtIndex.value = districts.value.indexOf(districtItem)
  127. }
  128. }
  129. }
  130. }
  131. }
  132. // 列变化
  133. function onColumnChange(e) {
  134. const {
  135. column,
  136. value
  137. } = e.detail
  138. if (column === 0) {
  139. provinceIndex.value = value
  140. const provinceCode = provinces.value[value].code
  141. updateCities(provinceCode)
  142. } else if (column === 1) {
  143. cityIndex.value = value
  144. const cityCode = cities.value[value].code
  145. updateDistricts(cityCode)
  146. }
  147. }
  148. // 确认选择
  149. function onPickerChange(e) {
  150. const values = e.detail.value
  151. provinceIndex.value = values[0]
  152. cityIndex.value = values[1]
  153. districtIndex.value = values[2]
  154. const province = provinces.value[provinceIndex.value]
  155. const city = cities.value[cityIndex.value]
  156. const district = districts.value[districtIndex.value]
  157. emit('update:province', province?.name || '')
  158. emit('update:city', city?.name || '')
  159. emit('update:district', district?.name || '')
  160. emit('change', {
  161. province: province?.name || '',
  162. city: city?.name || '',
  163. district: district?.name || '',
  164. provinceCode: province?.code || '',
  165. cityCode: city?.code || '',
  166. districtCode: district?.code || ''
  167. })
  168. }
  169. function showPicker() {}
  170. // 监听外部传入的省市区名称变化(用于编辑回显)
  171. watch(
  172. () => [props.province, props.city, props.district],
  173. ([newProvince, newCity, newDistrict]) => {
  174. if (newProvince && provinces.value.length > 0) {
  175. setIndexesByNames(newProvince, newCity, newDistrict)
  176. }
  177. }, {
  178. immediate: true,
  179. deep: true
  180. }
  181. )
  182. onMounted(() => {
  183. initProvinces()
  184. })
  185. </script>
  186. <style scoped lang="less">
  187. .region-picker {
  188. position: relative;
  189. width: 100%;
  190. height: 100rpx;
  191. /* 与 input 高度一致 */
  192. }
  193. .picker-display {
  194. display: flex;
  195. align-items: center;
  196. justify-content: space-between;
  197. height: 100%;
  198. /* 与输入框背景一致 */
  199. border-radius: 8rpx;
  200. /* 圆角一致 */
  201. font-size: 28rpx;
  202. color: #333;
  203. .region-text {
  204. flex: 1;
  205. font-size: 28rpx;
  206. line-height: 100rpx;
  207. color: #333;
  208. &.placeholder {
  209. color: #999;
  210. }
  211. }
  212. .arrow {
  213. margin-left: 16rpx;
  214. color: #999;
  215. font-size: 28rpx;
  216. }
  217. }
  218. .region-text {
  219. flex: 1;
  220. white-space: nowrap;
  221. overflow: hidden;
  222. text-overflow: ellipsis;
  223. }
  224. .region-text.placeholder {
  225. color: #999;
  226. }
  227. .picker-mask {
  228. position: absolute;
  229. top: 0;
  230. left: 0;
  231. width: 100%;
  232. height: 100%;
  233. opacity: 0;
  234. }
  235. </style>