index.vue 9.0 KB

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