index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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
  29. :value="pickerValue"
  30. :immediate-change="true"
  31. @change="handlePickerChange"
  32. @pickstart="handlePickerStart"
  33. class="picker-view"
  34. >
  35. <!-- 省份列 -->
  36. <picker-view-column>
  37. <view
  38. class="picker-item"
  39. :class="{ 'selected-item': pickerValue[0] === index }"
  40. v-for="(province, index) in provinces"
  41. :key="province.name"
  42. >
  43. {{ province.name }}
  44. </view>
  45. </picker-view-column>
  46. <!-- 城市列 -->
  47. <picker-view-column>
  48. <view
  49. class="picker-item"
  50. :class="{ 'selected-item': pickerValue[1] === index }"
  51. v-for="(city, index) in cities"
  52. :key="city.name"
  53. >
  54. {{ city.name }}
  55. </view>
  56. </picker-view-column>
  57. <!-- 区县列 -->
  58. <picker-view-column>
  59. <view
  60. class="picker-item"
  61. :class="{ 'selected-item': pickerValue[2] === index }"
  62. v-for="(area, index) in areas"
  63. :key="area.name"
  64. >
  65. {{ area.name }}
  66. </view>
  67. </picker-view-column>
  68. </picker-view>
  69. </view>
  70. <!-- 确认按钮 -->
  71. <view class="footer adfacjb">
  72. <button class="reset-btn" @click="handleReset">重置</button>
  73. <button class="confirm-btn" @click="handleConfirm">确定</button>
  74. </view>
  75. </view>
  76. </view>
  77. </template>
  78. <script setup>
  79. import { ref, watch, computed, onMounted, nextTick, getCurrentInstance } from 'vue';
  80. const { proxy } = getCurrentInstance()
  81. const emit = defineEmits(['update:show', 'confirm', 'reset']);
  82. const hotCities = ['武汉','南京','郑州','杭州','上海','苏州','厦门','深圳','北京','广州','长沙','成都'];
  83. const cityData = ref([])
  84. const provinces = ref([]);
  85. const cities = ref([]);
  86. const areas = ref([]);
  87. const pickerValue = ref([0, 0, 0]);
  88. const hotCityTarget = ref(null);
  89. let pickerUpdateVersion = 0;
  90. const selectedProvince = computed(() => provinces.value[pickerValue.value[0]] || {});
  91. const selectedCity = computed(() => cities.value[pickerValue.value[1]] || {});
  92. const selectedArea = computed(() => areas.value[pickerValue.value[2]] || {});
  93. // 确认选择
  94. const handleConfirm = () => {
  95. const result = {
  96. province: selectedProvince.value.name,
  97. city: selectedCity.value.name,
  98. area: selectedArea.value.name,
  99. provinceId: selectedProvince.value.id,
  100. cityId: selectedCity.value.id,
  101. areaId: selectedArea.value.id,
  102. };
  103. emit('confirm', result);
  104. };
  105. // 重置区域选择并清除外层页面的区域筛选
  106. const handleReset = () => {
  107. pickerUpdateVersion++;
  108. hotCityTarget.value = null;
  109. pickerValue.value = [0, 0, 0];
  110. cities.value = provinces.value[0]?.children || [];
  111. areas.value = cities.value[0]?.children || [];
  112. emit('reset');
  113. };
  114. const handlePickerStart = () => {
  115. pickerUpdateVersion++;
  116. hotCityTarget.value = null;
  117. };
  118. // picker-view 滚动时触发
  119. const handlePickerChange = (e) => {
  120. const newPickerValue = e.detail.value;
  121. if (hotCityTarget.value) {
  122. const isTarget = hotCityTarget.value.every((value, index) => value === newPickerValue[index]);
  123. if (!isTarget) return;
  124. }
  125. const [provinceIndex, cityIndex, areaIndex] = newPickerValue;
  126. const oldProvinceIndex = pickerValue.value[0];
  127. const oldCityIndex = pickerValue.value[1];
  128. // 如果省份改变了
  129. if (provinceIndex !== oldProvinceIndex) {
  130. // 更新城市列表和地区列表,并将它们的索引重置为0
  131. cities.value = provinces.value[provinceIndex].children;
  132. areas.value = cities.value[0]?.children || [];
  133. pickerValue.value = [provinceIndex, 0, 0];
  134. }
  135. // 如果城市改变了
  136. else if (cityIndex !== oldCityIndex) {
  137. // 更新地区列表,并将地区索引重置为0
  138. areas.value = cities.value[cityIndex]?.children || [];
  139. pickerValue.value = [provinceIndex, cityIndex, 0];
  140. }
  141. // 如果只是地区改变
  142. else {
  143. pickerValue.value = newPickerValue;
  144. }
  145. };
  146. // 点击热门城市
  147. // 新方法:遍历cityData找到对应的省市区并更新pickerValue
  148. const handleHotCityClick = (cityName) => {
  149. let provinceIndex = -1;
  150. let cityIndex = -1;
  151. let found = false;
  152. // 遍历省份
  153. for (let i = 0; i < provinces.value.length; i++) {
  154. const province = provinces.value[i];
  155. // 遍历城市
  156. if (province.children && province.children.length > 0) {
  157. for (let j = 0; j < province.children.length; j++) {
  158. const city = province.children[j];
  159. // 检查城市名是否匹配(使用 startsWith 来兼容 "杭州" 和 "杭州市")
  160. if (city.name.startsWith(cityName)) {
  161. provinceIndex = i;
  162. cityIndex = j;
  163. found = true;
  164. break; // 找到城市,跳出内层循环
  165. }
  166. }
  167. }
  168. if (found) {
  169. break; // 找到城市,跳出外层循环
  170. }
  171. }
  172. // 如果找到了对应的城市
  173. if (found) {
  174. const updateVersion = ++pickerUpdateVersion;
  175. hotCityTarget.value = [provinceIndex, cityIndex, 0];
  176. // 先切换省份和城市数据源,让 picker-view 完成列数据刷新。
  177. cities.value = provinces.value[provinceIndex].children;
  178. areas.value = cities.value[0]?.children || [];
  179. pickerValue.value = [provinceIndex, 0, 0];
  180. // 城市列刷新后再定位目标城市,避免非省会城市被回退到索引 0。
  181. nextTick(() => {
  182. if (updateVersion !== pickerUpdateVersion) return;
  183. areas.value = cities.value[cityIndex]?.children || [];
  184. pickerValue.value = [provinceIndex, cityIndex, 0];
  185. });
  186. } else {
  187. // 如果在数据中找不到该热门城市,可以给一个提示
  188. uni.showToast({
  189. title: `未在数据源中找到 ${cityName}`,
  190. icon: 'none'
  191. });
  192. }
  193. };
  194. const getTreeData = () => {
  195. return new Promise(resolve=>{
  196. proxy.$api.get('/tree').then(({data:res})=>{
  197. cityData.value = dealTreeData(res.data);
  198. resolve(cityData.value)
  199. })
  200. })
  201. }
  202. const dealTreeData = (data) => {
  203. try{
  204. const nodeMap = new Map()
  205. const result = [];
  206. data.forEach(d=>{
  207. nodeMap.set(d.id,{...d,children:[]})
  208. })
  209. data.forEach(d=>{
  210. const node = nodeMap.get(d.id);
  211. const parent = nodeMap.get(d.pid)
  212. if(parent) parent.children.push(node)
  213. else result.push(node)
  214. })
  215. return result
  216. }catch(e){
  217. return []
  218. }
  219. }
  220. // 初始化数据
  221. const initialize = async () => {
  222. provinces.value = await getTreeData();
  223. const [pIndex, cIndex] = pickerValue.value;
  224. cities.value = provinces.value[pIndex]?.children || [];
  225. areas.value = cities.value[cIndex]?.children || [];
  226. };
  227. defineExpose({
  228. initialize
  229. })
  230. onMounted(()=>{
  231. initialize();
  232. })
  233. </script>
  234. <style lang="scss" scoped>
  235. .city-picker-container {
  236. .picker-content {
  237. width: 100%;
  238. background-color: #ffffff;
  239. border-radius: 0 0 24rpx 24rpx;
  240. display: flex;
  241. flex-direction: column;
  242. }
  243. }
  244. .header {
  245. padding: 36rpx 0 0 28rpx;
  246. position: relative;
  247. .title {
  248. font-family: PingFang-SC, PingFang-SC;
  249. font-weight: bold;
  250. font-size: 36rpx;
  251. color: #252525;
  252. line-height: 36rpx;
  253. }
  254. }
  255. .hot-cities-section {
  256. padding: 0 28rpx;
  257. margin-top: 40rpx;
  258. .sub-title {
  259. font-family: PingFangSC, PingFang SC;
  260. font-weight: 400;
  261. font-size: 24rpx;
  262. color: #989998;
  263. line-height: 24rpx;
  264. }
  265. .hot-cities-grid {
  266. display: grid;
  267. grid-template-columns: repeat(4, 1fr);
  268. gap: 20rpx;
  269. margin: 24rpx 0;
  270. }
  271. .hot-city-item {
  272. border-radius: 6rpx;
  273. border: 1rpx solid #E5E7EB;
  274. padding: 13rpx 0;
  275. font-family: PingFangSC, PingFang SC;
  276. font-weight: 400;
  277. font-size: 24rpx;
  278. color: #252525;
  279. line-height: 33rpx;
  280. text-align: center;
  281. }
  282. }
  283. .divider {
  284. height: 14rpx;
  285. background: #F7F7F7;
  286. }
  287. .picker-wrapper {
  288. .picker-header {
  289. display: flex;
  290. justify-content: space-around;
  291. padding: 20rpx 0;
  292. font-family: PingFang-SC, PingFang-SC;
  293. font-weight: bold;
  294. font-size: 28rpx;
  295. color: #252525;
  296. line-height: 36rpx;
  297. }
  298. }
  299. .picker-view {
  300. width: 100%;
  301. height: 380rpx;
  302. }
  303. .picker-item {
  304. display: flex;
  305. align-items: center;
  306. justify-content: center;
  307. font-size: 30rpx;
  308. color: #A4A4A4;
  309. transition: all 0.2s;
  310. &.selected-item {
  311. font-size: 32rpx;
  312. color: #252525;
  313. }
  314. }
  315. .footer {
  316. padding: 64rpx 24rpx 40rpx;
  317. .reset-btn,
  318. .confirm-btn {
  319. height: 80rpx;
  320. line-height: 88rpx;
  321. background: #B7F358;
  322. border-radius: 45rpx;
  323. font-family: PingFang-SC, PingFang-SC;
  324. font-weight: bold;
  325. font-size: 28rpx;
  326. color: #252525;
  327. line-height: 80rpx;
  328. letter-spacing: 2rpx;
  329. border: none;
  330. &:after {
  331. border: none;
  332. }
  333. }
  334. .reset-btn {
  335. width: 222rpx;
  336. background: #FFFFFF;
  337. border: 1rpx solid #252525;
  338. }
  339. .confirm-btn {
  340. width: calc(100% - 252rpx);
  341. }
  342. }
  343. </style>