feat: 添加全局隐私协议管理器并适配鸿蒙元服务

1. 新增common/js/privacy.js全局隐私协议管理器,统一处理多平台隐私弹窗逻辑
2. 替换APP-HARMONY为MP-HARMONY平台判断标识,适配鸿蒙元服务
3. 在mp-html组件中增加MP-HARMONY平台兼容
4. 在App.vue中集成隐私弹窗初始化逻辑
5. 重构privacy-popup.vue,接入全局隐私管理器,优化弹窗互斥逻辑
6. 全局挂载$privacy实例到Vue原型链
This commit is contained in:
2026-07-01 09:39:04 +08:00
parent 4adaa945a1
commit 7045a05a73
6 changed files with 660 additions and 354 deletions

View File

@@ -1,12 +1,12 @@
<template>
<view>
<view class="privacy-mask" v-if="showPop">
<view class="privacy-wrap">
<template>
<view>
<view class="privacy-mask" v-if="showPop">
<view class="privacy-wrap">
<view class="privacy-title">{{ $lang('components.privacyPopup.title') }}</view>
<view class="privacy-desc">
{{ $lang('components.privacyPopup.descPrefix', { appName: appName }) }}
<text class="privacy-link" @tap="openPrivacyContract">{{ privacyContractName }}</text>
<!-- #ifdef QUICKAPP-WEBVIEW || H5 -->
<!-- #ifdef QUICKAPP-WEBVIEW || H5 || MP-HARMONY -->
<text class="privacy-link" @tap="openPrivacyService">{{ privacyServiceName }}</text>
<!-- #endif -->
{{ $lang('components.privacyPopup.descSuffix', { appName: appName }) }}
@@ -17,349 +17,404 @@
<button id="agree-btn" class="privacy-button-btn bg-agree" open-type="agreePrivacyAuthorization"
@agreeprivacyauthorization="handleAgree">{{ $lang('components.privacyPopup.agreeBtn') }}</button>
<!-- #endif -->
<!-- #ifdef WEB || H5 -->
<!-- #ifdef H5 || MP-HARMONY -->
<button id="agree-btn" class="privacy-button-btn bg-agree" @tap="handleAgree">{{ $lang('components.privacyPopup.agreeBtn') }}</button>
<!-- #endif -->
</view>
</view>
</view>
</view>
</template>
<script>
// #ifdef QUICKAPP-WEBVIEW
import has from '@system.has'
// #endif
const closeOtherPagePopUpHooks = new Set();
const privacyContractPage = '/pages_tool/agreement/contenr?type=0';
const privacyServicePage = '/pages_tool/agreement/contenr?type=1';
export default {
name: 'PrivacyPopup',
data() {
return {
agree: false,
showPop: false,
privacyAuthorization: null,
privacyResolves: new Set(),
privacyContractName: this.$lang('components.privacyPopup.defaultContractNameMp'),
appName: this.$lang('components.privacyPopup.defaultAppNameMp'),
// #ifdef WEB || H5
appName: this.$lang('components.privacyPopup.defaultAppNameH5'),
privacyContractName: this.$lang('components.privacyPopup.defaultContractNameH5'),
privacyServiceName: this.$lang('components.privacyPopup.defaultServiceNameH5'),
// #endif
}
},
mounted() {
this.listenPrivacySettingChange()
this.curPageShow()
this.proactive()
},
created() {
try {
// #ifdef MP-WEIXIN
wx.getPrivacySetting({
success: (res) => {
this.privacyContractName = res.privacyContractName
}
});
// #endif
// #ifdef QUICKAPP-WEBVIEW || H5
if (this.$util.isQuickApp() && typeof has != 'undefined' && has?.getPrivacySetting) {
has.getPrivacySetting({
success: (res) => {
if (res.privacyContractName) {
this.privacyContractName = res.privacyContractName
}
},
})
}
// #endif
} catch (e) {
console.error("=========低版本基础库==========", e)
}
},
methods: {
/**
* 统一弹窗 + 关闭其他页面的弹窗
*/
showPrivacyPopup() {
this.popUp()
this.closeOtherPagePopUp(this.disPopUp)
},
/**
* 根据是否需要授权决定弹窗或 emit agree
*/
checkAndShow(needAuthorization) {
if (uni.getStorageSync('skipPrivacyCheck')) {
uni.removeStorageSync('skipPrivacyCheck');
this.$emit('agree');
return;
}
if (needAuthorization) {
const privacyAgreed = uni.getStorageSync('privacyAgreed');
if (privacyAgreed) {
this.$emit('agree')
} else {
this.showPrivacyPopup()
}
} else {
this.$emit('agree')
}
},
getPrivacySettingByQuickApp() {
// #ifdef QUICKAPP-WEBVIEW || H5 || WEB
if (!this.$util.isQuickApp()) return;
if (typeof has != 'undefined' && has?.getPrivacySetting) {
has.getPrivacySetting({
success: (res) => {
this.checkAndShow(res.needAuthorization)
},
})
} else {
this.checkAndShow(!uni.getStorageSync('privacyAgreed'))
}
// #endif
},
/**
* 监听平台侧的隐私授权事件
*/
listenPrivacySettingChange() {
// #ifdef MP-WEIXIN
if (wx.onNeedPrivacyAuthorization) {
wx.onNeedPrivacyAuthorization((resolve) => {
if (typeof this.privacyAuthorization === 'function') {
this.privacyAuthorization(resolve)
}
})
}
// #endif
// #ifdef QUICKAPP-WEBVIEW || H5 || WEB
if (this.$util.isQuickApp() && typeof has != 'undefined' && has?.onPrivacySettingChange) {
has.onPrivacySettingChange((res) => {
this.checkAndShow(res.needAuthorization)
})
}
// #endif
},
/**
* 主动查询隐私授权状态并弹窗
*/
proactive() {
// #ifdef MP-WEIXIN
if (wx.getPrivacySetting) {
wx.getPrivacySetting({
success: (res) => {
this.checkAndShow(res.needAuthorization)
},
});
} else {
// 低版本基础库直接检查本地存储
this.checkAndShow(!uni.getStorageSync('privacyAgreed'))
}
// #endif
// #ifdef QUICKAPP-WEBVIEW
this.getPrivacySettingByQuickApp();
// #endif
// #ifdef H5 || WEB
this.checkAndShow(!uni.getStorageSync('privacyAgreed'))
// #endif
},
/**
* 初始化监听程序(页面级)
*/
curPageShow() {
closeOtherPagePopUpHooks.add(this.disPopUp)
this.privacyAuthorization = resolve => {
this.privacyResolves.add(resolve)
this.showPrivacyPopup()
}
},
/**
* 弹窗时关掉其他页面的隐私弹窗,避免重叠
*/
closeOtherPagePopUp(closePopUp) {
closeOtherPagePopUpHooks.forEach(hook => {
if (closePopUp !== hook) {
hook()
}
})
},
openPrivacyContract() {
// #ifdef MP-WEIXIN
wx.openPrivacyContract({
success(res) {},
fail(err) {}
});
// #endif
// #ifdef QUICKAPP-WEBVIEW || H5 || WEB
if (this.$util.isQuickApp()) {
if (typeof has != 'undefined' && has?.openPrivacySetting) {
has.openPrivacySetting({
success: (res) => {},
fail: (err) => {}
});
} else {
this.$util.redirectTo(privacyContractPage);
}
} else {
this.$util.redirectTo(privacyContractPage);
}
// #endif
},
openPrivacyService() {
// #ifdef QUICKAPP-WEBVIEW || H5 || WEB
if (this.$util.isQuickApp()) {
if (typeof has != 'undefined' && has?.openPrivacySetting) {
has.openPrivacySetting({
success: (res) => {},
fail: (err) => {}
});
} else {
this.$util.redirectTo(privacyServicePage);
}
} else {
this.$util.redirectTo(privacyServicePage);
}
// #endif
},
handleDisagree() {
this.privacyResolves.forEach(resolve => {
resolve({ event: 'disagree' })
})
this.privacyResolves.clear()
this.disPopUp()
uni.setStorageSync('privacyAgreed', true);
uni.showModal({
content: this.$lang('components.privacyPopup.disagreeTip'),
success: () => {
this.$emit('disagree')
}
})
},
handleAgree() {
this.privacyResolves.forEach(resolve => {
resolve({
event: 'agree',
buttonId: 'agree-btn'
})
})
this.privacyResolves.clear()
this.disPopUp()
uni.setStorageSync('privacyAgreed', true);
this.$emit('agree')
},
popUp() {
this.showPop = true;
},
disPopUp() {
this.showPop = false;
},
},
beforeDestroy() {
this.privacyResolves.clear()
closeOtherPagePopUpHooks.delete(this.disPopUp)
},
}
</script>
<style lang="scss" scoped>
.privacy-mask {
position: fixed;
z-index: 5000;
top: 0;
right: 0;
left: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.2);
display: flex;
align-items: center;
justify-content: center;
}
.privacy-wrap {
width: 632rpx;
padding: 48rpx 30rpx;
box-sizing: border-box;
background: #fff;
border-radius: 16rpx;
}
.privacy-title {
padding: 0rpx 30rpx 40rpx 30rpx;
font-weight: 700;
font-size: 36rpx;
text-align: center;
}
.privacy-desc {
font-size: 30rpx;
color: #555;
line-height: 2;
text-align: left;
padding: 0 40rpx;
}
.privacy-link {
color: #2f80ed;
}
.privacy-button-flex {
display: flex;
padding: 20rpx 40rpx;
}
.privacy-button-btn {
color: #FFF;
font-size: 30rpx;
// #ifdef QUICKAPP-WEBVIEW || H5 || WEB
font-size: 28rpx;
// #endif
font-weight: 500;
line-height: 100rpx;
text-align: center;
height: 100rpx;
border-radius: 20rpx;
border: none;
background: #07c160;
flex: 1;
margin-right: 30rpx;
justify-content: center;
}
.privacy-button-btn::after {
border: none;
}
.bg-disagree {
color: #07c160;
background: #f2f2f2;
}
.bg-agree {
margin-right: 0rpx;
}
</view>
</view>
</view>
</view>
</template>
<script>
// #ifdef QUICKAPP-WEBVIEW
import has from '@system.has'
// #endif
import PrivacyManager from '@/common/js/privacy.js';
const privacyContractPage = '/pages_tool/agreement/contenr?type=0';
const privacyServicePage = '/pages_tool/agreement/contenr?type=1';
export default {
name: 'PrivacyPopup',
data() {
return {
agree: false,
showPop: false,
privacyAuthorization: null,
privacyResolves: new Set(),
privacyContractName: this.$lang('components.privacyPopup.defaultContractNameMp'),
appName: this.$lang('components.privacyPopup.defaultAppNameMp'),
// #ifdef H5
appName: this.$lang('components.privacyPopup.defaultAppNameH5'),
privacyContractName: this.$lang('components.privacyPopup.defaultContractNameH5'),
privacyServiceName: this.$lang('components.privacyPopup.defaultServiceNameH5'),
// #endif
// #ifdef MP-HARMONY
appName: this.$lang('components.privacyPopup.defaultAppNameH5'),
privacyContractName: this.$lang('components.privacyPopup.defaultContractNameH5'),
privacyServiceName: this.$lang('components.privacyPopup.defaultServiceNameH5'),
// #endif
}
},
mounted() {
this.listenPrivacySettingChange()
this.curPageShow()
// 监听全局隐私弹窗事件(由 App.vue boot 完成后通过 PrivacyManager.show() 触发)
uni.$on('privacy:show', this.handlePrivacyShow)
// 监听关闭事件: 其他实例弹窗时通知本实例关闭
uni.$on('privacy:forceClose', this.handleForceClose)
// 补偿: 若 PrivacyManager.show() 在本组件 mounted 之前 emit 了事件
// 事件已丢失,此处通过 consumePendingShow 补偿弹窗
if (PrivacyManager.consumePendingShow()) {
this.$nextTick(() => {
this.showPrivacyPopup()
})
}
},
created() {
try {
// #ifdef MP-WEIXIN
wx.getPrivacySetting({
success: (res) => {
this.privacyContractName = res.privacyContractName
}
});
// #endif
// #ifdef QUICKAPP-WEBVIEW
if (typeof has != 'undefined' && has?.getPrivacySetting) {
has.getPrivacySetting({
success: (res) => {
if (res.privacyContractName) {
this.privacyContractName = res.privacyContractName
}
},
})
}
// #endif
// #ifdef MP-HARMONY
// 鸿蒙元服务: 隐私授权由系统管理, 无需应用层查询 privacyContractName
// #endif
} catch (e) {
console.error("=========低版本基础库==========", e)
}
},
methods: {
/**
* 接收全局隐私弹窗事件
* 由 App.vue → PrivacyManager.show() → uni.$emit('privacy:show') 触发
* 多个实例同时监听时, 只有第一个调用 consumePendingShow 成功的实例才弹窗
*/
handlePrivacyShow() {
// 互斥: 只有一个实例能消费成功
if (!PrivacyManager.consumePendingShow()) return
this.showPrivacyPopup()
},
/**
* 接收其他实例的关闭通知
* 当另一个 privacy-popup 实例弹窗时, 本实例关闭自己的弹窗
* 避免多个实例同时弹窗导致需要点击两次
*/
handleForceClose() {
this.disPopUp()
},
/**
* 统一弹窗 + 关闭其他页面的弹窗
* 通过 uni.$emit('privacy:forceClose') 通知其他实例关闭
*/
showPrivacyPopup() {
// 先通知其他实例关闭
try { uni.$emit('privacy:forceClose'); } catch (e) {}
// 再弹出本实例
this.popUp()
},
/**
* 根据是否需要授权决定弹窗或 emit agree
*/
checkAndShow(needAuthorization) {
if (uni.getStorageSync('skipPrivacyCheck')) {
uni.removeStorageSync('skipPrivacyCheck');
this.$emit('agree');
return;
}
if (needAuthorization) {
const privacyAgreed = uni.getStorageSync('privacyAgreed');
if (privacyAgreed) {
this.$emit('agree')
} else {
this.showPrivacyPopup()
}
} else {
this.$emit('agree')
}
},
getPrivacySettingByQuickApp() {
// #ifdef QUICKAPP-WEBVIEW
if (typeof has != 'undefined' && has?.getPrivacySetting) {
has.getPrivacySetting({
success: (res) => {
this.checkAndShow(res.needAuthorization)
},
})
} else {
this.checkAndShow(!uni.getStorageSync('privacyAgreed'))
}
// #endif
},
/**
* 监听平台侧的隐私授权事件
* 微信小程序: wx.onNeedPrivacyAuthorization 由当前页面实例注册
* 鸿蒙元服务: has.onPrivacySettingChange
* 与 PrivacyManager 协同:
* - 若 PrivacyManager 判断需要弹窗 → 弹自定义弹窗, resolve 存入 privacyResolves
* - 若已同意 → 直接 resolve 放行
*/
listenPrivacySettingChange() {
// #ifdef MP-WEIXIN
if (wx.onNeedPrivacyAuthorization) {
wx.onNeedPrivacyAuthorization((resolve) => {
// 检查是否已同意
if (!PrivacyManager.needShow()) {
// 已同意, 直接放行
resolve({ event: 'agree' })
} else {
// 需要弹窗, 存 resolve 等用户操作
if (typeof this.privacyAuthorization === 'function') {
this.privacyAuthorization(resolve)
} else {
// 兜底: 组件未就绪, 直接放行避免 API 卡死
resolve({ event: 'agree' })
}
}
})
}
// #endif
// #ifdef QUICKAPP-WEBVIEW
if (typeof has != 'undefined' && has?.onPrivacySettingChange) {
has.onPrivacySettingChange((res) => {
if (res.needAuthorization && PrivacyManager.needShow()) {
this.showPrivacyPopup()
}
})
}
// #endif
// #ifdef MP-HARMONY
// 鸿蒙元服务: 隐私授权由系统自动管理, 无需应用层监听
// #endif
},
/**
* 初始化监听程序(页面级)
*/
curPageShow() {
this.privacyAuthorization = resolve => {
this.privacyResolves.add(resolve)
this.showPrivacyPopup()
}
},
/**
* 弹窗时关掉其他页面的隐私弹窗,避免重叠
*/
closeOtherPagePopUp(closePopUp) {
// 通过全局事件通知所有页面关闭弹窗
try { uni.$emit('privacy:closeOthers', this._uid); } catch (e) {}
},
openPrivacyContract() {
// #ifdef MP-WEIXIN
wx.openPrivacyContract({
success(res) {},
fail(err) {}
});
// #endif
// #ifdef QUICKAPP-WEBVIEW
if (typeof has != 'undefined' && has?.openPrivacySetting) {
has.openPrivacySetting({
success: (res) => {},
fail: (err) => {}
});
} else {
this.$util.redirectTo(privacyContractPage);
}
// #endif
// #ifdef MP-HARMONY
// 鸿蒙元服务: 使用应用内协议页面
this.$util.redirectTo(privacyContractPage);
// #endif
// #ifdef H5
this.$util.redirectTo(privacyContractPage);
// #endif
},
openPrivacyService() {
// #ifdef QUICKAPP-WEBVIEW
if (typeof has != 'undefined' && has?.openPrivacySetting) {
has.openPrivacySetting({
success: (res) => {},
fail: (err) => {}
});
} else {
this.$util.redirectTo(privacyServicePage);
}
// #endif
// #ifdef MP-HARMONY
this.$util.redirectTo(privacyServicePage);
// #endif
// #ifdef H5
this.$util.redirectTo(privacyServicePage);
// #endif
},
handleDisagree() {
this.privacyResolves.forEach(resolve => {
resolve({ event: 'disagree' })
})
this.privacyResolves.clear()
// 先通知其他实例关闭, 再关闭自己, 避免叠层
try { uni.$emit('privacy:forceClose'); } catch (e) {}
this.disPopUp()
// 修复: 拒绝时不写 privacyAgreed, 允许下次再弹
// 由 PrivacyManager 管理 storage
PrivacyManager.disagree()
uni.showModal({
content: this.$lang('components.privacyPopup.disagreeTip'),
success: () => {
this.$emit('disagree')
}
})
},
handleAgree() {
this.privacyResolves.forEach(resolve => {
resolve({
event: 'agree',
buttonId: 'agree-btn'
})
})
this.privacyResolves.clear()
// 先通知其他实例关闭, 再关闭自己
try { uni.$emit('privacy:forceClose'); } catch (e) {}
this.disPopUp()
// 由 PrivacyManager 管理 storage
PrivacyManager.agree()
this.$emit('agree')
},
popUp() {
this.showPop = true;
},
disPopUp() {
this.showPop = false;
},
},
beforeDestroy() {
this.privacyResolves.clear()
// 移除全局事件监听
uni.$off('privacy:show', this.handlePrivacyShow)
uni.$off('privacy:forceClose', this.handleForceClose)
},
}
</script>
<style lang="scss" scoped>
.privacy-mask {
position: fixed;
z-index: 5000;
top: 0;
right: 0;
left: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.2);
display: flex;
align-items: center;
justify-content: center;
}
.privacy-wrap {
width: 632rpx;
padding: 48rpx 30rpx;
box-sizing: border-box;
background: #fff;
border-radius: 16rpx;
}
.privacy-title {
padding: 0rpx 30rpx 40rpx 30rpx;
font-weight: 700;
font-size: 36rpx;
text-align: center;
}
.privacy-desc {
font-size: 30rpx;
color: #555;
line-height: 2;
text-align: left;
padding: 0 40rpx;
}
.privacy-link {
color: #2f80ed;
}
.privacy-button-flex {
display: flex;
padding: 20rpx 40rpx;
}
.privacy-button-btn {
color: #FFF;
font-size: 30rpx;
/* #ifdef QUICKAPP-WEBVIEW || H5 || MP-HARMONY */
font-size: 28rpx;
/* #endif */
font-weight: 500;
line-height: 100rpx;
text-align: center;
height: 100rpx;
border-radius: 20rpx;
border: none;
background: #07c160;
flex: 1;
margin-right: 30rpx;
justify-content: center;
}
.privacy-button-btn::after {
border: none;
}
.bg-disagree {
color: #07c160;
background: #f2f2f2;
}
.bg-agree {
margin-right: 0rpx;
}
</style>