2026-04-21 22:07:29 +08:00
|
|
|
import bundleManager from '@ohos.bundle.bundleManager'
|
|
|
|
|
import common from '@ohos.app.ability.common'
|
2026-04-29 15:46:39 +08:00
|
|
|
import request from '@ohos.request'
|
|
|
|
|
import type { BusinessError } from '@ohos.base'
|
2026-04-21 22:07:29 +08:00
|
|
|
import { HttpClient } from '../core/HttpClient'
|
2026-04-29 19:08:12 +08:00
|
|
|
import type { AppVersionInfo, InstalledRnBundleInfo, RnBundleInfo } from '../core/Types'
|
2026-04-21 22:07:29 +08:00
|
|
|
import { SDKContext } from '../core/SDKContext'
|
|
|
|
|
|
|
|
|
|
export interface AppUpdateResult {
|
|
|
|
|
hasUpdate: boolean
|
|
|
|
|
info?: AppVersionInfo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface RnUpdateResult {
|
|
|
|
|
hasUpdate: boolean
|
|
|
|
|
info?: RnBundleInfo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class UpdateSDK {
|
|
|
|
|
static async checkAppUpdate(appKey: string): Promise<AppUpdateResult> {
|
|
|
|
|
const bundleInfo = bundleManager.getBundleInfoForSelfSync(
|
|
|
|
|
bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT
|
|
|
|
|
)
|
|
|
|
|
const currentVersionCode = bundleInfo.versionCode
|
|
|
|
|
|
|
|
|
|
const data = await HttpClient.get<AppVersionInfo>(
|
2026-04-29 15:46:39 +08:00
|
|
|
`/api/v1/updates/app/check?appKey=${appKey}&versionCode=${currentVersionCode}&platform=HARMONY`
|
2026-04-21 22:07:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (data.latestVersionCode <= currentVersionCode) {
|
|
|
|
|
return { hasUpdate: false }
|
|
|
|
|
}
|
|
|
|
|
return { hasUpdate: true, info: data }
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-29 15:46:39 +08:00
|
|
|
static async openAppMarket(context: common.UIAbilityContext, url?: string): Promise<void> {
|
|
|
|
|
if (!url) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
await context.openLink(url, { appLinkingOnly: false })
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 22:07:29 +08:00
|
|
|
static async checkRnUpdate(
|
|
|
|
|
appKey: string,
|
|
|
|
|
bundleName: string,
|
2026-04-29 19:08:12 +08:00
|
|
|
currentBundleVersion?: number,
|
2026-04-29 15:46:39 +08:00
|
|
|
packageName?: string
|
2026-04-21 22:07:29 +08:00
|
|
|
): Promise<RnUpdateResult> {
|
2026-04-29 19:08:12 +08:00
|
|
|
const localBundleVersion = currentBundleVersion ?? await SDKContext.getRnBundleVersion(bundleName) ?? 0
|
2026-04-21 22:07:29 +08:00
|
|
|
const data = await HttpClient.get<RnBundleInfo>(
|
2026-04-29 19:08:12 +08:00
|
|
|
`/api/v1/rn/update/check?appKey=${appKey}&bundleName=${bundleName}&bundleVersion=${localBundleVersion}${packageName ? `&packageName=${encodeURIComponent(packageName)}` : ''}`
|
2026-04-21 22:07:29 +08:00
|
|
|
)
|
|
|
|
|
|
2026-04-29 19:08:12 +08:00
|
|
|
if (data.bundleVersion <= localBundleVersion) {
|
2026-04-21 22:07:29 +08:00
|
|
|
return { hasUpdate: false }
|
|
|
|
|
}
|
2026-04-29 15:46:39 +08:00
|
|
|
if (packageName && data.packageMatched === false) {
|
|
|
|
|
return { hasUpdate: false, info: data }
|
|
|
|
|
}
|
2026-04-21 22:07:29 +08:00
|
|
|
return { hasUpdate: true, info: data }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async downloadRnBundle(
|
|
|
|
|
context: common.UIAbilityContext,
|
|
|
|
|
downloadUrl: string,
|
2026-04-29 19:08:12 +08:00
|
|
|
destFilename: string,
|
|
|
|
|
bundleName?: string,
|
|
|
|
|
bundleInfo?: RnBundleInfo,
|
2026-04-21 22:07:29 +08:00
|
|
|
): Promise<string> {
|
|
|
|
|
const destPath = context.cacheDir + '/' + destFilename
|
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
|
request.downloadFile(context, {
|
|
|
|
|
url: downloadUrl,
|
|
|
|
|
filePath: destPath,
|
2026-04-29 15:46:39 +08:00
|
|
|
}, (err: BusinessError | null, task: request.DownloadTask) => {
|
2026-04-21 22:07:29 +08:00
|
|
|
if (err) { reject(err); return }
|
|
|
|
|
task.on('complete', resolve)
|
|
|
|
|
task.on('fail', (error: number) => reject(new Error(`Download failed: ${error}`)))
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-04-29 19:08:12 +08:00
|
|
|
if (bundleName && bundleInfo) {
|
|
|
|
|
const installedInfo: InstalledRnBundleInfo = {
|
|
|
|
|
bundleVersion: bundleInfo.bundleVersion,
|
|
|
|
|
packageName: bundleInfo.packageName,
|
|
|
|
|
minCommonVersion: bundleInfo.minCommonVersion,
|
|
|
|
|
installedAt: Date.now(),
|
|
|
|
|
}
|
|
|
|
|
await SDKContext.setRnBundleInfo(bundleName, installedInfo)
|
|
|
|
|
}
|
2026-04-21 22:07:29 +08:00
|
|
|
if (SDKContext.getConfig().debug) {
|
|
|
|
|
console.log('[UpdateSDK] RN bundle downloaded to', destPath)
|
|
|
|
|
}
|
|
|
|
|
return destPath
|
|
|
|
|
}
|
2026-04-29 19:08:12 +08:00
|
|
|
|
|
|
|
|
static async rememberRnBundleInfo(
|
|
|
|
|
bundleName: string,
|
|
|
|
|
bundleVersion: number,
|
|
|
|
|
packageName?: string,
|
|
|
|
|
minCommonVersion?: string,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
await SDKContext.setRnBundleInfo(bundleName, {
|
|
|
|
|
bundleVersion,
|
|
|
|
|
packageName,
|
|
|
|
|
minCommonVersion,
|
|
|
|
|
installedAt: Date.now(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async getInstalledRnBundleInfo(bundleName: string): Promise<InstalledRnBundleInfo | null> {
|
|
|
|
|
return SDKContext.getRnBundleInfo(bundleName)
|
|
|
|
|
}
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|