修改城市缓存信息用cookie

This commit is contained in:
lcc
2025-08-08 00:01:33 +08:00
parent 8bc4b18e4e
commit 94c99dbf6b
4 changed files with 95 additions and 20 deletions
+21 -12
View File
@@ -177,6 +177,8 @@ import api from '@/utils/api'
import { useBrandStore } from '@/stores/brand';
import { showToast } from 'vant'
import { trackEvent } from '@/utils/analytics'
// 导入 cookie 工具
import { getCookie, setCookie } from '@/utils/cookie'
const route = useRouter()
const is_first_load = ref(true)
@@ -294,21 +296,24 @@ const getDictionaryList = async (dictCode = 'carProductLabel,priceRange,productL
const getCurrentLocation = async () => {
try {
// 获取当前位置
const location = localStorage.getItem('userLocation')?JSON.parse(localStorage.getItem('userLocation')):await WechatLocation.getLocation('gcj02');
// const location = localStorage.getItem('userLocation')?JSON.parse(localStorage.getItem('userLocation')):await WechatLocation.getLocation('gcj02');
// 修改为从 cookie 获取位置信息
const location = getCookie('userLocation') || await WechatLocation.getLocation('gcj02');
console.log('当前位置:', location);
const userStore = useUserStore();
// 解析位置获取城市信息
if (location && location.latitude && location.longitude) {
if(localStorage.getItem('cityInfo')){
cityInfo.value = JSON.parse(localStorage.getItem('cityInfo'));
// if(localStorage.getItem('cityInfo')){
// 修改为从 cookie 获取城市信息
if(getCookie('cityInfo')){
// cityInfo.value = JSON.parse(localStorage.getItem('cityInfo'));
cityInfo.value = getCookie('cityInfo');
console.log('城市信息:', cityInfo.value);
if (cityInfo.value.cityId) {
userStore.setCity(cityInfo.value);
query_data.cityId = cityInfo.value.cityId;
// 获取产品列表
empty.value = false
empty.value = false
await getProductList();
}
}else{
@@ -316,16 +321,18 @@ const getCurrentLocation = async () => {
if (result.code === 200 && result.data) {
cityInfo.value = result.data;
console.log('城市信息:', cityInfo.value);
localStorage.setItem('cityInfo', JSON.stringify(cityInfo.value));
localStorage.setItem('userLocation', JSON.stringify({...location,time:new Date().getTime()}));
// localStorage.setItem('cityInfo', JSON.stringify(cityInfo.value));
// localStorage.setItem('userLocation', JSON.stringify({...location,time:new Date().getTime()}));
// 修改为使用 cookie 存储
setCookie('cityInfo', cityInfo.value,1);
setCookie('userLocation', {...location, time: new Date().getTime()},1);
// 设置城市ID到查询参数
if (cityInfo.value.cityId) {
userStore.setCity(cityInfo.value);
query_data.cityId = cityInfo.value.cityId;
// 获取产品列表
empty.value = false
empty.value = false
await getProductList();
}
}
@@ -470,7 +477,9 @@ const handleCityItem = (item) => {
cityInfo.value.cityName = item.cityName;
cityInfo.value.cityId = item.cityId;
userStore.setCity(cityInfo.value);
localStorage.setItem('cityInfo', JSON.stringify(cityInfo.value));
// localStorage.setItem('cityInfo', JSON.stringify(cityInfo.value));
// 修改为使用 cookie 存储
setCookie('cityInfo', cityInfo.value,1);
getProductList();
}
+16 -6
View File
@@ -113,6 +113,8 @@ import SideMenu from '@/components/SideMenu.vue' // 导入SideMenu组件
import { showDialog } from 'vant' // 导入Toast组件
import WechatUtils,{ WechatLocation, WechatShare } from '@/utils/wechat'
import { useUserStore } from '@/stores/user';
// 导入 cookie 工具
import { getCookie, setCookie } from '@/utils/cookie'
@@ -225,14 +227,19 @@ const cityInfo = ref({
const getCurrentLocation = async () => {
try {
// 获取当前位置
const location = localStorage.getItem('userLocation')?JSON.parse(localStorage.getItem('userLocation')):await WechatLocation.getLocation('gcj02');
// const location = localStorage.getItem('userLocation')?JSON.parse(localStorage.getItem('userLocation')):await WechatLocation.getLocation('gcj02');
// 修改为从 cookie 获取位置信息
const location = getCookie('userLocation') || await WechatLocation.getLocation('gcj02');
console.log('当前位置:', location);
const userStore = useUserStore();
// 解析位置获取城市信息
if (location && location.latitude && location.longitude) {
if(localStorage.getItem('cityInfo')){
cityInfo.value = JSON.parse(localStorage.getItem('cityInfo'));
// if(localStorage.getItem('cityInfo')){
// 修改为从 cookie 获取城市信息
if(getCookie('cityInfo')){
// cityInfo.value = JSON.parse(localStorage.getItem('cityInfo'));
cityInfo.value = getCookie('cityInfo');
console.log('城市信息:', cityInfo.value);
// if (cityInfo.value.cityId) {
// userStore.setCity(cityInfo.value);
@@ -247,8 +254,11 @@ const cityInfo = ref({
if (result.code === 200 && result.data) {
cityInfo.value = result.data;
console.log('城市信息:', cityInfo.value);
localStorage.setItem('cityInfo', JSON.stringify(cityInfo.value));
localStorage.setItem('userLocation', JSON.stringify({...location,time:new Date().getTime()}));
// localStorage.setItem('cityInfo', JSON.stringify(cityInfo.value));
// localStorage.setItem('userLocation', JSON.stringify({...location,time:new Date().getTime()}));
// 修改为使用 cookie 存储
setCookie('cityInfo', cityInfo.value,1);
setCookie('userLocation', {...location, time: new Date().getTime()},1);
// 设置城市ID到查询参数
if (cityInfo.value.cityId) {
@@ -256,7 +266,7 @@ const cityInfo = ref({
// query_data.cityId = cityInfo.value.cityId;
// 获取产品列表
empty.value = false
empty.value = false
await getProductList();
}
}
+1 -2
View File
@@ -14,7 +14,6 @@ export const useUserStore = defineStore('user', {
},
setFriendAccountId(data) {
this.friend_account_id = data
},
}
}
})
+57
View File
@@ -0,0 +1,57 @@
/**
* 设置 Cookie
* @param {string} name - Cookie 名称
* @param {any} value - Cookie 值
* @param {number} days - 过期天数,默认7天
*/
export const setCookie = (name, value, days = 7) => {
const expires = new Date();
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = `${name}=${encodeURIComponent(JSON.stringify(value))};expires=${expires.toUTCString()};path=/`;
};
/**
* 获取 Cookie
* @param {string} name - Cookie 名称
* @returns {any|null} Cookie 值或 null
*/
export const getCookie = (name) => {
const nameEQ = name + "=";
const ca = document.cookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) {
try {
return JSON.parse(decodeURIComponent(c.substring(nameEQ.length, c.length)));
} catch (e) {
return null;
}
}
}
return null;
};
/**
* 删除 Cookie
* @param {string} name - Cookie 名称
*/
export const removeCookie = (name) => {
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`;
};
/**
* 检查 Cookie 是否存在
* @param {string} name - Cookie 名称
* @returns {boolean} 是否存在
*/
export const hasCookie = (name) => {
return getCookie(name) !== null;
};
export default {
setCookie,
getCookie,
removeCookie,
hasCookie
};