Commit 3d41ea99 authored by spc's avatar spc

fixed

parent f076ef5b
<template>
<view class="webview-container">
<!-- Webview内容 -->
<web-view :src="webviewUrl" @message="getMessage" class="webview-content"></web-view>
</view>
</template>
<script setup>
import { ref, watch, nextTick } from 'vue'
import { useUserStore } from '@/stores/user.js'
import { useGlobalStore } from '../../stores/global'
import { fetchSyWebviewJSON } from '../../api/sywebview'
import { onLoad, onShow, onShareAppMessage, onShareTimeline, onPageScroll } from "@dcloudio/uni-app";
import { jump, JumpType } from '../../utils'
import md from '../../md';
// 响应式数据
const statusBarHeight = ref(0)
const webviewUrl = ref('')
const share = ref(null) // 分享数据
const pageOptions = ref({})
const registered = ref(undefined)
// 获取用户store
const userStore = useUserStore()
const globalStore = useGlobalStore()
// 检查用户是否注册
function getRegistered() {
return !!(userStore.userInfo && userStore.userInfo?.memberId !== "not_login")
}
// 处理返回逻辑
const handleBack = () => {
try {
uni.navigateBack({
delta: 1,
fail: () => {
// 如果返回失败,跳转到首页
uni.redirectTo({
url: '/pages/index/index'
})
}
})
} catch (error) {
console.log('返回失败:', error)
uni.redirectTo({
url: '/pages/index/index'
})
}
}
// 登录获取 cuk
const ensureAutoLogin = async () => {
await new Promise(resolve => {
userStore.syWxAutoLogin(resolve)
})
}
// 接收webview消息
const getMessage = (e) => {
console.log('=== Webview 消息接收开始 ===')
console.log('完整事件对象:', e)
console.log('事件详情:', e.detail)
// 尝试多种方式获取数据
let messageData = null
// 方式1:从 e.detail.data 获取
if (e.detail && e.detail.data) {
messageData = e.detail.data
console.log('从 e.detail.data 获取到数据:', messageData)
}
if (messageData) {
console.log('消息数据类型:', typeof messageData)
console.log('消息数据是否为数组:', Array.isArray(messageData))
share.value = messageData[messageData.length - 1]
}
}
// 初始化 webview URL
const initWebviewUrl = async () => {
const res = await fetchSyWebviewJSON()
console.log('res', res)
if (!res.data.url) {
uni.redirectTo({
url: '/pages/index/index'
})
return
}
let baseUrl = res.data.url
const cuk = globalStore.cuk
const openId = globalStore.openId
const unionId = globalStore.unionId
const options = pageOptions.value
// scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
const scene = options.scene ? decodeURIComponent(decodeURIComponent(options.scene)) : null
const cleanOptions = { ...options }
delete cleanOptions.scene
console.warn("scene", scene)
// 将 options 的所有参数原样作为查询参数传给 H5,并补齐 cuk
const params = { ...cleanOptions }
if (cuk && !params.cuk) params.cuk = cuk
// 添加用户信息参数
if (openId && !params.openId) params.openId = openId
if (unionId && !params.unionId) params.unionId = unionId
// 添加 memberId(如果已注册)
if (userStore.userInfo?.memberId && userStore.userInfo.memberId !== "not_login") {
params.crmId = userStore.userInfo.memberId
}
const paramStr = Object.keys(params)
.filter(key => params[key] !== undefined && params[key] !== null && params[key] !== '')
.map(key => `${key}=${encodeURIComponent(params[key])}`)
.join('&')
// p 渠道参数
// openId 用户openid
// unionId 用户unionid
// cuk
// shareToken 分享人token
// pt 分享海报人token
// 示例:
// https://25niansuyuan.feihe.com/projects/Firmus/dev/index?p=xmh&openId=用户openid&unionId=用户unionid&&pt=yzhd123&shareToken=yzhd123&cuk=12121
if (scene) {
if (scene[0] === '?') {
baseUrl += scene
} else {
baseUrl += `?${scene}`
}
}
webviewUrl.value = paramStr
? baseUrl + (baseUrl.includes('?') ? '&' : '?') + paramStr
: baseUrl
console.log('Webview页面加载,URL:', webviewUrl.value, paramStr, cuk, openId, unionId)
}
// 监听注册状态变化,初始化 webview URL
watch(() => registered.value, (newVal, oldVal) => {
if (!pageOptions.value.needLogin || newVal) {
webviewUrl.value = ''
nextTick(() => {
initWebviewUrl()
})
}
})
// 页面显示时处理授权逻辑
onShow(async () => {
const oldVal = registered.value
await ensureAutoLogin()
await userStore.loadUserInfo()
registered.value = getRegistered()
if (oldVal === undefined) {
// 首次加载
if (pageOptions.value.needLogin && !registered.value) {
// 需要登录但未注册,跳转到注册页
jump({
type: JumpType.INNER,
url: '/pages/activity/register',
})
} else if (!pageOptions.value.needLogin) {
// 不需要登录,直接初始化 URL
if (!webviewUrl.value) {
await initWebviewUrl()
}
}
} else {
// 授权状态变化:如果从已注册变为未注册(授权失败),显示提示并返回
if (registered.value === false) {
uni.showToast({
title: "授权失败,即将返回",
icon: "none",
});
setTimeout(() => {
handleBack()
}, 1000)
}
}
})
// 页面参数接收
onLoad(async (options) => {
pageOptions.value = options
console.log('页面参数:', options)
wx.showShareMenu({
withShareTicket: true,
menus: ['shareAppMessage', 'shareTimeline']// shareAppMessage表示"发送给朋友"按钮,shareTimeline表示"分享到朋友圈"按钮
})
console.warn("wx", wx.showShareMenu)
// 获取状态栏高度
const systemInfo = uni.getSystemInfoSync()
statusBarHeight.value = systemInfo.statusBarHeight || 0
})
// 分享功能
onShareAppMessage((options) => {
console.log('分享数据:', share.value)
console.log('分享选项:', options)
md.sensorLogTake({
xcxClick: "suyuan202508H5Click",
pageName: "星妈会分享页",
buttonName: "星妈会分享出",
});
if (share.value) {
const shareurl = share.value.shareUrl
const title = share.value.shareTitle
const imageUrl = share.value.shareImageUrl
console.log('使用分享数据:1', { title, shareurl, imageUrl })
return {
title: title,
path: shareurl || '/pages/index/index',
imageUrl: imageUrl || '',
success: function (res) {
console.log('分享成功:', res)
//星妈会埋点方法,用户分享成功后触发事件
},
fail: function (res) {
console.log('分享失败:', res)
},
complete: function (res) {
console.log('分享完成:', res)
}
}
}
// 如果没有分享数据,返回默认分享
return {
path: '/pages/index/index'
}
})
onShareTimeline((options) => {
console.log('分享朋友圈数据:', share.value)
md.sensorLogTake({
xcxClick: "suyuan202508H5Click",
pageName: "星妈会分享页",
buttonName: "星妈会分享出",
});
if (share.value) {
const shareurl = share.value.shareUrl
const title = share.value.shareTitle
const imageUrl = share.value.shareImageUrl
console.log('使用分享数据:', { title, shareurl, imageUrl })
return {
title: title || '分享',
path: shareurl || '/pages/index/index',
imageUrl: imageUrl || '',
success: function (res) {
//星妈会埋点方法,用户分享成功后触发事件
console.log('分享成功:', res)
},
fail: function (res) {
console.log('分享失败:', res)
},
complete: function (res) {
console.log('分享完成:', res)
}
}
}
// 如果没有分享数据,返回默认分享
return {
path: '/pages/index/index'
}
})
</script>
<style lang="less" scoped>
.webview-container {
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
background-color: #fff;
}
.webview-content {
flex: 1;
width: 100%;
}
</style>
......@@ -106,15 +106,15 @@ export const useGlobalStore = defineStore('global', {
/**
* 清除认证信息(注销时使用)
*/
clearAuthData() {
this.cuk = null;
this.openId = null;
this.unionId = null;
// clearAuthData() {
// this.cuk = null;
// this.openId = null;
// this.unionId = null;
// 清除本地存储
uni.removeStorageSync('cuk');
uni.removeStorageSync('openId');
uni.removeStorageSync('unionId');
},
// // 清除本地存储
// uni.removeStorageSync('cuk');
// uni.removeStorageSync('openId');
// uni.removeStorageSync('unionId');
// },
},
});
\ No newline at end of file
......@@ -73,11 +73,11 @@ this.hasShownPopup = false
/**
* 清除首页信息(注销时使用)
*/
clearHomeData() {
this.homeInfo = null;
this.isLogin = false;
this.babyExistence = false;
this.hasShownPopup = false;
},
// clearHomeData() {
// this.homeInfo = null;
// this.isLogin = false;
// this.babyExistence = false;
// this.hasShownPopup = false;
// },
},
});
......@@ -385,16 +385,16 @@ export const useUserStore = defineStore("userInfo", {
/**
* 清除用户信息(注销时使用)
*/
clearUserData() {
this.userInfo = null;
this.babyInfo = null;
this.memberInfo = null;
this.babyNickCache = [];
this.cepingjieguoInfo = null;
// clearUserData() {
// this.userInfo = null;
// this.babyInfo = null;
// this.memberInfo = null;
// this.babyNickCache = [];
// this.cepingjieguoInfo = null;
// 清除本地存储
uni.removeStorageSync('memberId');
uni.removeStorageSync('loginStatus');
},
// // 清除本地存储
// uni.removeStorageSync('memberId');
// uni.removeStorageSync('loginStatus');
// },
},
});
\ No newline at end of file
......@@ -88,13 +88,13 @@ export default {
uni.hideLoading();
if (res.ok) {
// 清除本地缓存的用户信息
const userStore = useUserStore();
const globalStore = useGlobalStore();
const homeStore = useHomeStore();
// const userStore = useUserStore();
// const globalStore = useGlobalStore();
// const homeStore = useHomeStore();
userStore.clearUserData();
globalStore.clearAuthData();
homeStore.clearHomeData();
// userStore.clearUserData();
// globalStore.clearAuthData();
// homeStore.clearHomeData();
console.log('用户信息已清除');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment