diff --git a/src/components/PopGetAllowance.vue b/src/components/PopGetAllowance.vue index 1244628..439e5dc 100644 --- a/src/components/PopGetAllowance.vue +++ b/src/components/PopGetAllowance.vue @@ -196,8 +196,10 @@ const bindPostCoupon = async () => { couponId: props.coupons.coupon_id, cityId: props.cityInfo.cityId }) - if (result.code === 200) { + if(result.data && result.data.Authorization){ //重新设置token + localStorage.setItem('token', result.data.Authorization) + } handleSuccess() } else { showToast(result.message || '补贴领取失败') diff --git a/src/pages/Item.vue b/src/pages/Item.vue index 9e0891e..50f264b 100644 --- a/src/pages/Item.vue +++ b/src/pages/Item.vue @@ -1,39 +1,42 @@ @@ -111,11 +120,11 @@ import PopGetAllowance from '@/components/PopGetAllowance.vue' import PopStores from '@/components/PopStores.vue' import SideMenu from '@/components/SideMenu.vue' // 导入SideMenu组件 import { showDialog, showToast } from 'vant' // 导入Toast组件 -import WechatUtils,{ WechatAuth,WechatLocation, WechatShare } from '@/utils/wechat' -import { useUserStore } from '@/stores/user'; +import WechatUtils, { WechatAuth, WechatLocation, WechatShare } from '@/utils/wechat' +import { useUserStore } from '@/stores/user' // 导入 cookie 工具 -import { getCookie, setCookie } from '@/utils/cookie' import { getCurrentInstance } from 'vue' +import { getUserLocation, getCityInfo, cacheLocationInfo } from '@/utils/location' const { proxy } = getCurrentInstance() @@ -130,12 +139,14 @@ const loading = ref(true) // 定义tabs激活状态(初始值根据业务需求设置,示例为'1') const content_active = ref('1') -const count_time = ref(0); +const count_time = ref(0) const content_list = ref([]) const banner_list = ref([]) const btn_info = ref({}) +//是否绑定手机号 +const bindMobile = ref(false) // 获取产品详情 @@ -145,6 +156,9 @@ async function getProductDetail() { const res = await api.wechatAPI.getProductDetail(productId.value) if (res.code === 200 && res.data) { productDetail.value = res.data + if (productDetail.value.bindMobile) { + bindMobile.value = true + } // 更新轮播图数据 banner_list.value = productDetail.value.imgs.map((img, index) => ({ img, @@ -152,10 +166,10 @@ async function getProductDetail() { })) // count_time.value = res.data.timeEnd - count_time.value = Number(30 * 24*60*60*1000) + count_time.value = Number(30 * 24 * 60 * 60 * 1000) btn_info.value = productDetail.value.coupons.btn_info - + // 更新内容标签页数据 content_list.value = [ { @@ -185,13 +199,13 @@ async function getProductDetail() { } } -function handleSuccess(){ +function handleSuccess() { showDialog({ title: '领取成功', message: '稍后会有电话客服与您联系,\n协助您完成补贴申领。', confirmButtonColor: '#f84803' }).then(() => { - }); + }) getProductDetail() } @@ -199,13 +213,12 @@ function handleSuccess(){ // 页面加载时获取产品详情 onMounted(async () => { let appid = localStorage.getItem('app_id') - if(appid) { - const wechat = await WechatUtils.init(appid); + if (appid) { + const wechat = await WechatUtils.init(appid) // 隐藏分享 - await WechatShare.hideAllNonBaseMenuItem(); + await WechatShare.hideAllNonBaseMenuItem() } - // 初始化完成后获取位置信息 - await getCurrentLocation(); + await getCurrentLocation() getProductDetail() }) @@ -225,58 +238,19 @@ const cityInfo = ref({ /** * 获取当前位置并解析城市编码 */ - const getCurrentLocation = async () => { +const getCurrentLocation = async () => { try { - // 获取当前位置 - // 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')){ - // 修改为从 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 - // await getProductList(); - // }} - }else{ - const result = await api.wechatAPI.getLocationCity(location.latitude, location.longitude); - 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()})); - // 修改为使用 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 - await getProductList(); - } - } - } + /// 微信环境或非微信环境未获取到cityId时使用定位逻辑 + const location = await getUserLocation(); + console.log('经纬度:', location); + if (!(location && location.latitude && location.longitude)) { + return; } + cityInfo.value = await getCityInfo(location); } catch (error) { - console.error('获取位置或解析城市失败:', error); + console.error('获取位置或解析城市失败:', error) } -}; +} const showPop = ref(false) @@ -284,8 +258,9 @@ const showPop = ref(false) function handleShowPop(val) { showPop.value = val } + function onCountFinish(e) { - console.log('onCountFinish' ,e); + console.log('onCountFinish', e) } // 补充门店弹窗控制变量 @@ -295,26 +270,32 @@ const showStorePop = ref(false) function handleShowStorePop(val) { showStorePop.value = val } -function showEnrollPop(){ + +function showEnrollPop() { //友盟统计 trackEvent('coupon', 'CLK', { Key_CouponId: productDetail.value.coupons.coupon_id, Key_CouponName: productDetail.value.coupons.title, Key_Brand: productDetail.value.brandName, Key_Series: productDetail.value.seriesName - }); + }) // showPop.value=true - if(WechatAuth.isWechatBrowser()){ - showPop.value=true - }else{ + if (WechatAuth.isWechatBrowser()) { + if (bindMobile.value) { + getCoupon() + } else { + showPop.value = true + } + } else { if (proxy && proxy.$auth) { proxy.$auth.require(() => { // 登录成功后执行的代码 - getCoupon(); + getCoupon() }) } } } + const isSubmitting = ref(false) const getCoupon = async () => { @@ -346,6 +327,7 @@ const getCoupon = async () => { margin: 0 4px; color: #f00; } + .count-down-block { display: inline-block; width: 44px; @@ -356,6 +338,7 @@ const getCoupon = async () => { background-color: #f00; border-radius: 8px; } + .loading-center { position: absolute; top: 50%; @@ -365,7 +348,7 @@ const getCoupon = async () => { \ No newline at end of file