index.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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, onMounted, getCurrentInstance } from 'vue';
  73. const { proxy } = getCurrentInstance()
  74. const emit = defineEmits(['update:show', 'confirm']);
  75. const hotCities = ['武汉','南京','郑州','杭州','上海','苏州','厦门','深圳','北京','广州','长沙','成都'];
  76. const cityData = ref([])
  77. const provinces = ref([]);
  78. const cities = ref([]);
  79. const areas = ref([]);
  80. const pickerValue = ref([0, 0, 0]);
  81. const selectedProvince = computed(() => provinces.value[pickerValue.value[0]] || {});
  82. const selectedCity = computed(() => cities.value[pickerValue.value[1]] || {});
  83. const selectedArea = computed(() => areas.value[pickerValue.value[2]] || {});
  84. // 确认选择
  85. const handleConfirm = () => {
  86. const result = {
  87. province: selectedProvince.value.name,
  88. city: selectedCity.value.name,
  89. area: selectedArea.value.name,
  90. provinceId: selectedProvince.value.id,
  91. cityId: selectedCity.value.id,
  92. areaId: selectedArea.value.id,
  93. };
  94. emit('confirm', result);
  95. };
  96. // picker-view 滚动时触发
  97. const handlePickerChange = (e) => {
  98. const newPickerValue = e.detail.value;
  99. const [provinceIndex, cityIndex, areaIndex] = newPickerValue;
  100. const oldProvinceIndex = pickerValue.value[0];
  101. const oldCityIndex = pickerValue.value[1];
  102. // 如果省份改变了
  103. if (provinceIndex !== oldProvinceIndex) {
  104. // 更新城市列表和地区列表,并将它们的索引重置为0
  105. cities.value = provinces.value[provinceIndex].children;
  106. areas.value = cities.value[0]?.children || [];
  107. pickerValue.value = [provinceIndex, 0, 0];
  108. }
  109. // 如果城市改变了
  110. else if (cityIndex !== oldCityIndex) {
  111. // 更新地区列表,并将地区索引重置为0
  112. areas.value = cities.value[cityIndex]?.children || [];
  113. pickerValue.value = [provinceIndex, cityIndex, 0];
  114. }
  115. // 如果只是地区改变
  116. else {
  117. pickerValue.value = newPickerValue;
  118. }
  119. };
  120. // 点击热门城市
  121. // 新方法:遍历cityData找到对应的省市区并更新pickerValue
  122. const handleHotCityClick = (cityName) => {
  123. let provinceIndex = -1;
  124. let cityIndex = -1;
  125. let found = false;
  126. // 遍历省份
  127. for (let i = 0; i < provinces.value.length; i++) {
  128. const province = provinces.value[i];
  129. // 遍历城市
  130. if (province.children && province.children.length > 0) {
  131. for (let j = 0; j < province.children.length; j++) {
  132. const city = province.children[j];
  133. // 检查城市名是否匹配(使用 startsWith 来兼容 "杭州" 和 "杭州市")
  134. if (city.name.startsWith(cityName)) {
  135. provinceIndex = i;
  136. cityIndex = j;
  137. found = true;
  138. break; // 找到城市,跳出内层循环
  139. }
  140. }
  141. }
  142. if (found) {
  143. break; // 找到城市,跳出外层循环
  144. }
  145. }
  146. // 如果找到了对应的城市
  147. if (found) {
  148. // 1. 更新城市列表,使其为选中省份的城市列表
  149. cities.value = provinces.value[provinceIndex].children;
  150. // 2. 更新区县列表,使其为选中城市的区县列表
  151. // 使用可选链 ?. 防止选中城市没有区县数据(如海南省直辖县)
  152. areas.value = cities.value[cityIndex]?.children || [];
  153. // 3. 更新 pickerValue,这会驱动 picker-view 滚动到指定位置
  154. // 区县默认选择第一个 (索引为0)
  155. pickerValue.value = [provinceIndex, cityIndex, 0];
  156. } else {
  157. // 如果在数据中找不到该热门城市,可以给一个提示
  158. uni.showToast({
  159. title: `未在数据源中找到 ${cityName}`,
  160. icon: 'none'
  161. });
  162. }
  163. };
  164. const getTreeData = () => {
  165. return new Promise(resolve=>{
  166. proxy.$api.get('/tree').then(({data:res})=>{
  167. cityData.value = dealTreeData(res.data);
  168. resolve(cityData.value)
  169. })
  170. })
  171. }
  172. const dealTreeData = (data) => {
  173. try{
  174. const nodeMap = new Map()
  175. const result = [];
  176. data.forEach(d=>{
  177. nodeMap.set(d.id,{...d,children:[]})
  178. })
  179. data.forEach(d=>{
  180. const node = nodeMap.get(d.id);
  181. const parent = nodeMap.get(d.pid)
  182. if(parent) parent.children.push(node)
  183. else result.push(node)
  184. })
  185. return result
  186. }catch(e){
  187. return []
  188. }
  189. }
  190. // 初始化数据
  191. const initialize = async () => {
  192. provinces.value = await getTreeData();
  193. const [pIndex, cIndex] = pickerValue.value;
  194. cities.value = provinces.value[pIndex]?.children || [];
  195. areas.value = cities.value[cIndex]?.children || [];
  196. };
  197. defineExpose({
  198. initialize
  199. })
  200. onMounted(()=>{
  201. initialize();
  202. })
  203. </script>
  204. <style lang="scss" scoped>
  205. .city-picker-container {
  206. .picker-content {
  207. width: 100%;
  208. background-color: #ffffff;
  209. border-radius: 0 0 24rpx 24rpx;
  210. display: flex;
  211. flex-direction: column;
  212. }
  213. }
  214. .header {
  215. padding: 36rpx 0 0 28rpx;
  216. position: relative;
  217. .title {
  218. font-family: PingFang-SC, PingFang-SC;
  219. font-weight: bold;
  220. font-size: 36rpx;
  221. color: #252525;
  222. line-height: 36rpx;
  223. }
  224. }
  225. .hot-cities-section {
  226. padding: 0 28rpx;
  227. margin-top: 40rpx;
  228. .sub-title {
  229. font-family: PingFangSC, PingFang SC;
  230. font-weight: 400;
  231. font-size: 24rpx;
  232. color: #989998;
  233. line-height: 24rpx;
  234. }
  235. .hot-cities-grid {
  236. display: grid;
  237. grid-template-columns: repeat(4, 1fr);
  238. gap: 20rpx;
  239. margin: 24rpx 0;
  240. }
  241. .hot-city-item {
  242. border-radius: 6rpx;
  243. border: 1rpx solid #E5E7EB;
  244. padding: 13rpx 0;
  245. font-family: PingFangSC, PingFang SC;
  246. font-weight: 400;
  247. font-size: 24rpx;
  248. color: #252525;
  249. line-height: 33rpx;
  250. text-align: center;
  251. }
  252. }
  253. .divider {
  254. height: 14rpx;
  255. background: #F7F7F7;
  256. }
  257. .picker-wrapper {
  258. .picker-header {
  259. display: flex;
  260. justify-content: space-around;
  261. padding: 20rpx 0;
  262. font-family: PingFang-SC, PingFang-SC;
  263. font-weight: bold;
  264. font-size: 28rpx;
  265. color: #252525;
  266. line-height: 36rpx;
  267. }
  268. }
  269. .picker-view {
  270. width: 100%;
  271. height: 380rpx;
  272. }
  273. .picker-item {
  274. display: flex;
  275. align-items: center;
  276. justify-content: center;
  277. font-size: 30rpx;
  278. color: #A4A4A4;
  279. transition: all 0.2s;
  280. &.selected-item {
  281. font-size: 32rpx;
  282. color: #252525;
  283. }
  284. }
  285. .footer {
  286. padding: 64rpx 147rpx 40rpx;
  287. .confirm-btn {
  288. width: 100%;
  289. height: 80rpx;
  290. line-height: 88rpx;
  291. background: #B7F358;
  292. border-radius: 45rpx;
  293. font-family: PingFang-SC, PingFang-SC;
  294. font-weight: bold;
  295. font-size: 28rpx;
  296. color: #252525;
  297. line-height: 80rpx;
  298. letter-spacing: 2rpx;
  299. border: none;
  300. &:after {
  301. border: none;
  302. }
  303. }
  304. }
  305. </style>