From 94c99dbf6bbad06b2a363a1a4594a8a3f8c12620 Mon Sep 17 00:00:00 2001 From: lcc <805383944@qq.com> Date: Fri, 8 Aug 2025 00:01:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9F=8E=E5=B8=82=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E4=BF=A1=E6=81=AF=E7=94=A8cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Home.vue | 33 ++++++++++++++++---------- src/pages/Item.vue | 22 ++++++++++++----- src/stores/user.js | 3 +-- src/utils/cookie.js | 57 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 src/utils/cookie.js diff --git a/src/pages/Home.vue b/src/pages/Home.vue index f5d6f23..6f64ebf 100644 --- a/src/pages/Home.vue +++ b/src/pages/Home.vue @@ -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(); } diff --git a/src/pages/Item.vue b/src/pages/Item.vue index 7989291..27ca736 100644 --- a/src/pages/Item.vue +++ b/src/pages/Item.vue @@ -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(); } } diff --git a/src/stores/user.js b/src/stores/user.js index de8f13d..244485d 100644 --- a/src/stores/user.js +++ b/src/stores/user.js @@ -14,7 +14,6 @@ export const useUserStore = defineStore('user', { }, setFriendAccountId(data) { this.friend_account_id = data - }, - + } } }) \ No newline at end of file diff --git a/src/utils/cookie.js b/src/utils/cookie.js new file mode 100644 index 0000000..4983a49 --- /dev/null +++ b/src/utils/cookie.js @@ -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 +}; \ No newline at end of file