fix:修正优化首页、个人中心页面广告图url跳转

This commit is contained in:
2025-12-15 17:37:52 +08:00
parent d12986b85e
commit 9d4e229312
8 changed files with 197 additions and 29 deletions
+53 -9
View File
@@ -292,6 +292,7 @@
const noticeText = ref('')
const bannerImages = ref([]) // 首页广告图片列表
const bannerImageList = ref([]) // 完整的广告配置列表(包含链接信息)
// 将语言代码转换为后端接受的格式
const convertLanguageCode = (lang) => {
@@ -373,11 +374,12 @@
console.log('首页广告响应:', res)
if (res.statusCode === 200 && res.data.code === 200 && res.data.data) {
// 使用 files 字段(图片列表
const files = res.data.data.files || []
if (files.length > 0) {
bannerImages.value = files
console.log('首页广告加载成功,图片数量:', files.length)
// 使用 imageList 字段(包含图片和链接信息
const imageList = res.data.data.imageList || []
if (imageList.length > 0) {
bannerImageList.value = imageList
// 提取图片URL用于展示
bannerImages.value = imageList.map(item => item.imageUrl)
} else {
console.warn('未获取到广告图片')
}
@@ -391,10 +393,52 @@
// 处理首页广告点击
const handleBannerClick = (index) => {
console.log('点击首页广告:', index, bannerImages.value[index])
// 可以根据需要添加跳转逻辑
// 例如跳转到合作加盟页面
uni.navigateTo({ url: '/pages/join/index' })
if (!bannerImageList.value || !bannerImageList.value[index]) {
console.warn('未找到对应的广告配置')
return
}
const config = bannerImageList.value[index]
console.log('点击首页广告:', index, config)
// 根据链接类型进行跳转
if (config.linkType === 'miniapp' && config.appId) {
// 跳转到外部小程序
// #ifdef MP-WEIXIN
uni.navigateToMiniProgram({
appId: config.appId,
path: config.linkUrl || '',
success: () => {
console.log('跳转小程序成功')
},
fail: (err) => {
console.error('跳转小程序失败:', err)
uni.showToast({
title: '跳转失败',
icon: 'none'
})
}
})
// #endif
// #ifndef MP-WEIXIN
uni.showToast({
title: '请在微信小程序中使用',
icon: 'none'
})
// #endif
} else if (config.linkType === 'external' && config.linkUrl) {
// 跳转到外部链接(H5页面)
uni.navigateTo({
url: `/pages/webview/index?url=${encodeURIComponent(config.linkUrl)}`
})
} else if (config.linkType === 'internal' && config.linkUrl) {
// 跳转到内部页面
uni.navigateTo({
url: config.linkUrl
})
} else {
console.log('无有效的跳转配置')
}
}
// 查询最近的活动
+54 -8
View File
@@ -159,6 +159,7 @@ import {
const showMenuItem = ref(false)
const bannerImages = ref([]) // 广告图片列表
const bannerImageList = ref([]) // 完整的广告配置列表(包含链接信息)
// 将语言代码转换为后端接受的格式
const convertLanguageCode = (lang) => {
@@ -192,11 +193,13 @@ import {
console.log('个人中心广告响应:', res)
if (res.statusCode === 200 && res.data.code === 200 && res.data.data) {
// 使用 files 字段(图片列表
const files = res.data.data.files || []
if (files.length > 0) {
bannerImages.value = files
console.log('个人中心广告加载成功,图片数量:', files.length)
// 使用 imageList 字段(包含图片和链接信息
const imageList = res.data.data.imageList || []
if (imageList.length > 0) {
bannerImageList.value = imageList
// 提取图片URL用于展示
bannerImages.value = imageList.map(item => item.imageUrl)
console.log('个人中心广告加载成功,图片数量:', imageList.length)
}
} else {
console.warn('获取个人中心广告失败:', res.data?.msg || '未知错误')
@@ -208,9 +211,52 @@ import {
// 处理广告点击
const handleBannerClick = (index) => {
console.log('点击广告:', index)
// 可以根据需要添加跳转逻辑
// 例如:navigateTo('/pages/join/index')
if (!bannerImageList.value || !bannerImageList.value[index]) {
console.warn('未找到对应的广告配置')
return
}
const config = bannerImageList.value[index]
console.log('点击广告:', index, config)
// 根据链接类型进行跳转
if (config.linkType === 'miniapp' && config.appId) {
// 跳转到外部小程序
// #ifdef MP-WEIXIN
uni.navigateToMiniProgram({
appId: config.appId,
path: config.linkUrl || '',
success: () => {
console.log('跳转小程序成功')
},
fail: (err) => {
console.error('跳转小程序失败:', err)
uni.showToast({
title: '跳转失败',
icon: 'none'
})
}
})
// #endif
// #ifndef MP-WEIXIN
uni.showToast({
title: '请在微信小程序中使用',
icon: 'none'
})
// #endif
} else if (config.linkType === 'external' && config.linkUrl) {
// 跳转到外部链接(H5页面)
uni.navigateTo({
url: `/pages/webview/index?url=${encodeURIComponent(config.linkUrl)}`
})
} else if (config.linkType === 'internal' && config.linkUrl) {
// 跳转到内部页面
uni.navigateTo({
url: config.linkUrl
})
} else {
console.log('无有效的跳转配置')
}
}
// 页面加载时初始化
+78
View File
@@ -0,0 +1,78 @@
<template>
<view class="webview-container">
<!-- web-view 组件用于显示外部网页 -->
<web-view :src="webUrl" @message="handleMessage" @load="handleLoad" @error="handleError"></web-view>
</view>
</template>
<script setup>
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue';
import { useI18n } from '@/utils/i18n.js'
const { t: $t } = useI18n()
// 网页链接
const webUrl = ref('')
// 页面加载时获取传入的 URL 参数
onLoad((options) => {
if (options.url) {
// 解码 URL
webUrl.value = decodeURIComponent(options.url)
console.log('加载外部链接:', webUrl.value)
} else {
uni.showToast({
title: $t('common.invalidUrl') || '无效的链接',
icon: 'none',
duration: 2000
})
// 2秒后返回上一页
setTimeout(() => {
uni.navigateBack()
}, 2000)
}
})
// 网页加载完成
const handleLoad = (e) => {
console.log('网页加载完成:', e)
}
// 网页加载错误
const handleError = (e) => {
console.error('网页加载错误:', e)
uni.showToast({
title: $t('common.loadFailed') || '加载失败',
icon: 'none'
})
}
// 接收网页消息(web-view 向小程序发送消息)
const handleMessage = (e) => {
console.log('收到网页消息:', e)
// 可以根据需要处理网页发来的消息
}
</script>
<script>
export default {
// 支持分享
onShareAppMessage() {
const $t = this.$t || ((key) => key)
return {
title: $t('share.title') || '分享',
path: '/pages/index/index'
}
}
}
</script>
<style lang="scss" scoped>
.webview-container {
width: 100vw;
height: 100vh;
overflow: hidden;
}
</style>