index.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <view class="city-picker-container">
  3. <view class="picker-content">
  4. <view class="header">
  5. <text class="title">城市地区</text>
  6. </view>
  7. <view class="hot-cities-section">
  8. <view class="sub-title">热门城市</view>
  9. <view class="hot-cities-grid">
  10. <view
  11. class="hot-city-item"
  12. v-for="city in hotCities"
  13. :key="city"
  14. @click="handleHotCityClick(city)"
  15. >
  16. {{ city }}
  17. </view>
  18. </view>
  19. </view>
  20. <view class="divider"></view>
  21. <!-- 选择器 -->
  22. <view class="picker-wrapper">
  23. <view class="picker-header">
  24. <text>省份</text>
  25. <text>城市</text>
  26. <text>区县</text>
  27. </view>
  28. <picker-view :value="pickerValue" @change="handlePickerChange" class="picker-view">
  29. <!-- 省份列 -->
  30. <picker-view-column>
  31. <view
  32. class="picker-item"
  33. :class="{ 'selected-item': pickerValue[0] === index }"
  34. v-for="(province, index) in provinces"
  35. :key="province.name"
  36. >
  37. {{ province.name }}
  38. </view>
  39. </picker-view-column>
  40. <!-- 城市列 -->
  41. <picker-view-column>
  42. <view
  43. class="picker-item"
  44. :class="{ 'selected-item': pickerValue[1] === index }"
  45. v-for="(city, index) in cities"
  46. :key="city.name"
  47. >
  48. {{ city.name }}
  49. </view>
  50. </picker-view-column>
  51. <!-- 区县列 -->
  52. <picker-view-column>
  53. <view
  54. class="picker-item"
  55. :class="{ 'selected-item': pickerValue[2] === index }"
  56. v-for="(area, index) in areas"
  57. :key="area.name"
  58. >
  59. {{ area.name }}
  60. </view>
  61. </picker-view-column>
  62. </picker-view>
  63. </view>
  64. <!-- 确认按钮 -->
  65. <view class="footer">
  66. <button class="confirm-btn" @click="handleConfirm">确定</button>
  67. </view>
  68. </view>
  69. </view>
  70. </template>
  71. <script setup>
  72. import { ref, watch, computed } from 'vue';
  73. import { cityData, hotCities } from './city-data.js';
  74. const emit = defineEmits(['update:show', 'confirm']);
  75. // --- Data ---
  76. const provinces = ref(cityData);
  77. const cities = ref([]);
  78. const areas = ref([]);
  79. const pickerValue = ref([0, 0, 0]);
  80. const selectedProvince = computed(() => provinces.value[pickerValue.value[0]] || {});
  81. const selectedCity = computed(() => cities.value[pickerValue.value[1]] || {});
  82. const selectedArea = computed(() => areas.value[pickerValue.value[2]] || {});
  83. // 确认选择
  84. const handleConfirm = () => {
  85. const result = {
  86. province: selectedProvince.value.name,
  87. city: selectedCity.value.name,
  88. area: selectedArea.value.name,
  89. };
  90. emit('confirm', result);
  91. };
  92. // picker-view 滚动时触发
  93. const handlePickerChange = (e) => {
  94. const newPickerValue = e.detail.value;
  95. const [provinceIndex, cityIndex, areaIndex] = newPickerValue;
  96. const oldProvinceIndex = pickerValue.value[0];
  97. const oldCityIndex = pickerValue.value[1];
  98. // 如果省份改变了
  99. if (provinceIndex !== oldProvinceIndex) {
  100. // 更新城市列表和地区列表,并将它们的索引重置为0
  101. cities.value = provinces.value[provinceIndex].children;
  102. areas.value = cities.value[0]?.children || [];
  103. pickerValue.value = [provinceIndex, 0, 0];
  104. }
  105. // 如果城市改变了
  106. else if (cityIndex !== oldCityIndex) {
  107. // 更新地区列表,并将地区索引重置为0
  108. areas.value = cities.value[cityIndex]?.children || [];
  109. pickerValue.value = [provinceIndex, cityIndex, 0];
  110. }
  111. // 如果只是地区改变
  112. else {
  113. pickerValue.value = newPickerValue;
  114. }
  115. };
  116. // 点击热门城市
  117. // 新方法:遍历cityData找到对应的省市区并更新pickerValue
  118. const handleHotCityClick = (cityName) => {
  119. let provinceIndex = -1;
  120. let cityIndex = -1;
  121. let found = false;
  122. // 遍历省份
  123. for (let i = 0; i < provinces.value.length; i++) {
  124. const province = provinces.value[i];
  125. // 遍历城市
  126. if (province.children && province.children.length > 0) {
  127. for (let j = 0; j < province.children.length; j++) {
  128. const city = province.children[j];
  129. // 检查城市名是否匹配(使用 startsWith 来兼容 "杭州" 和 "杭州市")
  130. if (city.name.startsWith(cityName)) {
  131. provinceIndex = i;
  132. cityIndex = j;
  133. found = true;
  134. break; // 找到城市,跳出内层循环
  135. }
  136. }
  137. }
  138. if (found) {
  139. break; // 找到城市,跳出外层循环
  140. }
  141. }
  142. // 如果找到了对应的城市
  143. if (found) {
  144. // 1. 更新城市列表,使其为选中省份的城市列表
  145. cities.value = provinces.value[provinceIndex].children;
  146. // 2. 更新区县列表,使其为选中城市的区县列表
  147. // 使用可选链 ?. 防止选中城市没有区县数据(如海南省直辖县)
  148. areas.value = cities.value[cityIndex]?.children || [];
  149. // 3. 更新 pickerValue,这会驱动 picker-view 滚动到指定位置
  150. // 区县默认选择第一个 (索引为0)
  151. pickerValue.value = [provinceIndex, cityIndex, 0];
  152. } else {
  153. // 如果在数据中找不到该热门城市,可以给一个提示
  154. uni.showToast({
  155. title: `未在数据源中找到 ${cityName}`,
  156. icon: 'none'
  157. });
  158. }
  159. };
  160. // 初始化数据
  161. const initialize = () => {
  162. const [pIndex, cIndex] = pickerValue.value;
  163. cities.value = provinces.value[pIndex]?.children || [];
  164. areas.value = cities.value[cIndex]?.children || [];
  165. };
  166. // 初始化
  167. initialize();
  168. defineExpose({
  169. initialize
  170. })
  171. </script>
  172. <style lang="scss" scoped>
  173. .city-picker-container {
  174. .picker-content {
  175. width: 100%;
  176. background-color: #ffffff;
  177. border-radius: 0 0 24rpx 24rpx;
  178. display: flex;
  179. flex-direction: column;
  180. }
  181. }
  182. .header {
  183. padding: 36rpx 0 0 28rpx;
  184. position: relative;
  185. .title {
  186. font-family: PingFang-SC, PingFang-SC;
  187. font-weight: bold;
  188. font-size: 36rpx;
  189. color: #252525;
  190. line-height: 36rpx;
  191. }
  192. }
  193. .hot-cities-section {
  194. padding: 0 28rpx;
  195. margin-top: 40rpx;
  196. .sub-title {
  197. font-family: PingFangSC, PingFang SC;
  198. font-weight: 400;
  199. font-size: 24rpx;
  200. color: #989998;
  201. line-height: 24rpx;
  202. }
  203. .hot-cities-grid {
  204. display: grid;
  205. grid-template-columns: repeat(4, 1fr);
  206. gap: 20rpx;
  207. margin: 24rpx 0;
  208. }
  209. .hot-city-item {
  210. border-radius: 6rpx;
  211. border: 1rpx solid #E5E7EB;
  212. padding: 13rpx 0;
  213. font-family: PingFangSC, PingFang SC;
  214. font-weight: 400;
  215. font-size: 24rpx;
  216. color: #252525;
  217. line-height: 33rpx;
  218. text-align: center;
  219. }
  220. }
  221. .divider {
  222. height: 14rpx;
  223. background: #F7F7F7;
  224. }
  225. .picker-wrapper {
  226. .picker-header {
  227. display: flex;
  228. justify-content: space-around;
  229. padding: 20rpx 0;
  230. font-family: PingFang-SC, PingFang-SC;
  231. font-weight: bold;
  232. font-size: 28rpx;
  233. color: #252525;
  234. line-height: 36rpx;
  235. }
  236. }
  237. .picker-view {
  238. width: 100%;
  239. height: 380rpx;
  240. }
  241. .picker-item {
  242. display: flex;
  243. align-items: center;
  244. justify-content: center;
  245. font-size: 30rpx;
  246. color: #A4A4A4;
  247. transition: all 0.2s;
  248. &.selected-item {
  249. font-size: 32rpx;
  250. color: #252525;
  251. }
  252. }
  253. .footer {
  254. padding: 64rpx 147rpx 40rpx;
  255. .confirm-btn {
  256. width: 100%;
  257. height: 80rpx;
  258. line-height: 88rpx;
  259. background: #B7F358;
  260. border-radius: 45rpx;
  261. font-family: PingFang-SC, PingFang-SC;
  262. font-weight: bold;
  263. font-size: 28rpx;
  264. color: #252525;
  265. line-height: 80rpx;
  266. letter-spacing: 2rpx;
  267. border: none;
  268. &:after {
  269. border: none;
  270. }
  271. }
  272. }
  273. </style>