From 07e41c56ba0d76e1dfdb31914ff3c7b22d92c72d Mon Sep 17 00:00:00 2001 From: lccsw <805383944@qq.com> Date: Tue, 9 Dec 2025 16:48:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BE=AE=E4=BF=A1=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=94=A8=E6=88=B7=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Home.vue | 56 +++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/src/pages/Home.vue b/src/pages/Home.vue index 99e8d89..16fd2f0 100644 --- a/src/pages/Home.vue +++ b/src/pages/Home.vue @@ -357,45 +357,51 @@ const getDictionaryList = async (dictCode = 'carProductLabel,priceRange,productL * @returns {Promise} */ const getCurrentLocation = async () => { + // 非微信环境从URL获取cityId + const urlParams = new URLSearchParams(window.location.search) + const urlCityId = urlParams.get('cityId') try { // 检查是否为微信浏览器 - const isWechat = WechatAuth.isWechatBrowser(); + const isWechat = WechatAuth.isWechatBrowser() if (!isWechat) { - // 非微信环境从URL获取cityId - const urlParams = new URLSearchParams(window.location.search); - const urlCityId = urlParams.get('cityId'); - if (urlCityId) { try { - const cityResult = await api.get('/auto/config/city',{cityId:urlCityId}); + const cityResult = await api.get('/auto/config/city', { cityId: urlCityId }) if (cityResult.code === 200 && cityResult.data) { - cacheLocationInfo("", cityResult.data); - await handleCityInfo(cityResult.data); - return; + cacheLocationInfo('', cityResult.data) + await handleCityInfo(cityResult.data) + return } } catch (cityError) { - console.error('通过cityId获取城市信息失败:', cityError); + console.error('通过cityId获取城市信息失败:', cityError) } } } // 微信环境或非微信环境未获取到cityId时使用定位逻辑 - const location = await getUserLocation(); - console.log('当前位置:', location); + const location = await getUserLocation() + console.log('当前位置:', location) if (!(location && location.latitude && location.longitude)) { - return; + return } - const cityInfoData = await getCityInfo(location); + const cityInfoData = await getCityInfo(location) if (cityInfoData && cityInfoData.cityId) { - cacheLocationInfo(location, cityInfoData); - await handleCityInfo(cityInfoData); + cacheLocationInfo(location, cityInfoData) + await handleCityInfo(cityInfoData) } } catch (error) { - console.error('获取位置或解析城市失败:', error); + console.error('获取位置或解析城市失败:', error) + if (urlCityId) { + const cityResult = await api.get('/auto/config/city', { cityId: urlCityId }) + if (cityResult.code === 200 && cityResult.data) { + cacheLocationInfo('', cityResult.data) + await handleCityInfo(cityResult.data) + } + } } } /** @@ -403,15 +409,15 @@ const getCurrentLocation = async () => { * @param {Object} cityInfoData 城市信息数据 */ const handleCityInfo = async (cityInfoData) => { - cityInfo.value = cityInfoData; - console.log('城市信息:', cityInfoData); + cityInfo.value = cityInfoData + console.log('城市信息:', cityInfoData) - const userStore = useUserStore(); - userStore.setCity(cityInfoData); - query_data.cityId = cityInfoData.cityId; + const userStore = useUserStore() + userStore.setCity(cityInfoData) + query_data.cityId = cityInfoData.cityId - empty.value = false; - await getProductList(); + empty.value = false + await getProductList() } /** * 获取产品列表 @@ -550,7 +556,7 @@ const handleCityItem = (item) => { // localStorage.setItem('cityInfo', JSON.stringify(cityInfo.value)); // 修改为使用 cookie 存储 // setCookie('cityInfo', cityInfo.value, 1) - cacheLocationInfo("", cityInfo.value); + cacheLocationInfo('', cityInfo.value) getCityBrands() getProductList() }