@qcwx_mediatom/quick-app-ad-sdk
Version:
mediatom快应用广告SDK
61 lines (55 loc) • 1.49 kB
JavaScript
// util/appCheck.js
import pkg from "@system.package"
import fetch from "@system.fetch"
/**
* 检查服务器返回的 app_list 中哪些应用包已安装
* @param {Array} appList - [{ id, bundle, bundle_ios }]
* @returns {Promise<Array<string>>} 已安装的 bundle 列表
*/
export async function filterInstalledApps(appList = []) {
const installed = []
await Promise.all(
appList.map((item) => {
return new Promise((resolve) => {
pkg.hasInstalled({
package: item.bundle,
success: (res) => {
if (res.result === true) {
installed.push(item.bundle)
}
resolve()
},
fail: () => resolve(), // 忽略失败
})
})
})
)
return installed
}
/**
* 上报已安装包名列表到服务端
* @param {Array<string>} installedBundles - 已安装包名数组
* @param {string} url - POST 提交地址
* @returns {Promise<any>} 响应结果
*/
export function reportInstalledApps(installedBundles, url) {
const data = {
installed_bundles: installedBundles,
}
return new Promise((resolve, reject) => {
fetch.fetch({
url,
method: "POST",
header: {
"Content-Type": "application/json",
},
data: JSON.stringify(data),
success: (res) => {
resolve(res.data)
},
fail: (err) => {
reject(err)
},
})
})
}