const privacyAuthorizationHandlers = [] let hasBoundPrivacyAuthorizationListener = false function getActivePrivacyAuthorizationHandler() { return privacyAuthorizationHandlers.length ? privacyAuthorizationHandlers[privacyAuthorizationHandlers.length - 1] : null } export function bindPrivacyAuthorizationListener(handler) { if (typeof handler !== 'function') { return } const existedIndex = privacyAuthorizationHandlers.indexOf(handler) if (existedIndex !== -1) { privacyAuthorizationHandlers.splice(existedIndex, 1) } privacyAuthorizationHandlers.push(handler) // #ifdef MP-WEIXIN if (hasBoundPrivacyAuthorizationListener || typeof wx === 'undefined' || !wx.onNeedPrivacyAuthorization) { return } wx.onNeedPrivacyAuthorization((resolve) => { const activeHandler = getActivePrivacyAuthorizationHandler() if (typeof activeHandler === 'function') { activeHandler(resolve) } }) hasBoundPrivacyAuthorizationListener = true // #endif } export function unbindPrivacyAuthorizationListener(handler) { const existedIndex = privacyAuthorizationHandlers.lastIndexOf(handler) if (existedIndex !== -1) { privacyAuthorizationHandlers.splice(existedIndex, 1) } } export function getPrivacyAuthorizationStatus() { // #ifdef MP-WEIXIN return new Promise((resolve) => { if (typeof wx === 'undefined' || !wx.getPrivacySetting) { resolve({ needAuthorization: false }) return } wx.getPrivacySetting({ success: (res) => { resolve({ ...res, needAuthorization: !!(res && res.needAuthorization) }) }, fail: () => { resolve({ needAuthorization: false }) } }) }) // #endif // #ifndef MP-WEIXIN return Promise.resolve({ needAuthorization: false }) // #endif } export function openWechatPrivacyContract() { // #ifdef MP-WEIXIN return new Promise((resolve, reject) => { if (typeof wx === 'undefined' || !wx.openPrivacyContract) { resolve() return } wx.openPrivacyContract({ success: resolve, fail: reject }) }) // #endif // #ifndef MP-WEIXIN return Promise.resolve() // #endif }