61 lines
2.6 KiB
TypeScript
61 lines
2.6 KiB
TypeScript
Component({
|
||
data: {
|
||
innerShow: false,
|
||
privacyContractName: '《用户隐私保护指引》'
|
||
},
|
||
lifetimes: {
|
||
attached() {
|
||
// 注册隐私协议监听器
|
||
const _this = this;
|
||
if (wx.onNeedPrivacyAuthorization) {
|
||
wx.onNeedPrivacyAuthorization((resolve, eventInfo) => {
|
||
console.log('触发隐私协议弹窗', eventInfo);
|
||
const needsResolve = eventInfo.referrer === 'api'; // API调用触发需要手动resolve
|
||
|
||
_this.resolvePrivacyAuthorization = resolve; // 保存 resolve 函数
|
||
|
||
if (wx.getPrivacySetting) {
|
||
wx.getPrivacySetting({
|
||
success: (res) => {
|
||
console.log('getPrivacySetting success', res);
|
||
// 无论是否需要(needAuthorization), 只要触发了onNeedPrivacyAuthorization就证明被拦截了(或者需要再次确认)
|
||
// 通常 res.needAuthorization 为 true
|
||
_this.setData({
|
||
innerShow: true,
|
||
privacyContractName: res.privacyContractName || _this.data.privacyContractName
|
||
});
|
||
}
|
||
})
|
||
}
|
||
})
|
||
}
|
||
},
|
||
},
|
||
methods: {
|
||
handleDisagree() {
|
||
this.setData({ innerShow: false });
|
||
// 如果有保存的 resolve,调用并报错拒绝(虽然 API 主要是要 resolve({ buttonId: 'agree-btn', event: 'agree' }),拒绝通常不调 resolve 或调 fail)
|
||
// 实际上用户拒绝后,该次 API 调用应该失败
|
||
if (this.resolvePrivacyAuthorization) {
|
||
// this.resolvePrivacyAuthorization({ event: 'disagree' }); // 某些场景下可能需要
|
||
this.resolvePrivacyAuthorization = null;
|
||
}
|
||
},
|
||
handleAgree(e: any) {
|
||
console.log('用户同意隐私协议');
|
||
// 按钮 open-type="agreePrivacyAuthorization" 会自动触发同意逻辑
|
||
// 这里的 bindagreeprivacyauthorization 回调用于 UI 反馈
|
||
this.setData({ innerShow: false });
|
||
|
||
// 如果是由 API 触发的(如 getClipboardData),可以通过保存的 resolve 告知系统
|
||
// 但通常 open-type 按钮点击后系统会自动重试之前的 API 调用,或者在 button 上绑定 resolve
|
||
if (this.resolvePrivacyAuthorization) {
|
||
this.resolvePrivacyAuthorization({ buttonId: 'agree-btn', event: 'agree' });
|
||
this.resolvePrivacyAuthorization = null;
|
||
}
|
||
},
|
||
openPrivacyContract() {
|
||
wx.openPrivacyContract({})
|
||
}
|
||
}
|
||
}) |