let openinstallPlugin = null
|
let initialized = false
|
let wakeUpRegistered = false
|
|
/**
|
* 安全获取 openinstall 原生插件实例,非 APP 或插件不存在时返回空。
|
*/
|
export const getOpeninstallPlugin = () => {
|
// #ifdef APP-PLUS
|
if (openinstallPlugin) {
|
return openinstallPlugin
|
}
|
try {
|
openinstallPlugin = uni.requireNativePlugin('openinstall-plugin')
|
} catch (error) {
|
console.log('openinstall 插件不可用', error)
|
openinstallPlugin = null
|
}
|
return openinstallPlugin
|
// #endif
|
// #ifndef APP-PLUS
|
return null
|
// #endif
|
}
|
|
/**
|
* 初始化 openinstall 插件,避免多次启动重复初始化。
|
*/
|
export const initOpeninstall = () => {
|
const plugin = getOpeninstallPlugin()
|
if (!plugin || initialized) {
|
return false
|
}
|
try {
|
plugin.init()
|
initialized = true
|
return true
|
} catch (error) {
|
console.log('openinstall 初始化失败', error)
|
return false
|
}
|
}
|
|
/**
|
* 注册 openinstall 拉起参数监听,用于已安装 APP 的扫码唤醒场景。
|
*/
|
export const registerOpeninstallWakeUp = (callback) => {
|
const plugin = getOpeninstallPlugin()
|
if (!plugin || wakeUpRegistered || typeof callback !== 'function') {
|
return false
|
}
|
try {
|
plugin.registerWakeUp((result) => {
|
callback(result)
|
})
|
wakeUpRegistered = true
|
return true
|
} catch (error) {
|
console.log('openinstall 拉起监听注册失败', error)
|
return false
|
}
|
}
|
|
/**
|
* 读取 openinstall 安装参数,用于未安装 APP 下载后首次打开场景。
|
*/
|
export const getOpeninstallInstallData = (callback) => {
|
const plugin = getOpeninstallPlugin()
|
if (!plugin || typeof callback !== 'function') {
|
return false
|
}
|
try {
|
plugin.getInstall(8, (result) => {
|
callback(result)
|
})
|
return true
|
} catch (error) {
|
console.log('openinstall 安装参数获取失败', error)
|
return false
|
}
|
}
|
|
/**
|
* 注册成功后上报 openinstall,便于后台统计注册转化。
|
*/
|
export const reportOpeninstallRegister = () => {
|
const plugin = getOpeninstallPlugin()
|
if (!plugin) {
|
return false
|
}
|
try {
|
plugin.reportRegister()
|
return true
|
} catch (error) {
|
console.log('openinstall 注册上报失败', error)
|
return false
|
}
|
}
|