XuqmGroup-HarmonySDK/xuqm-sdk/src/main/ets/update/UpdateSDK.ets

72 行
2.1 KiB
Plaintext

2026-04-21 22:07:29 +08:00
import bundleManager from '@ohos.bundle.bundleManager'
import request from '@ohos.request'
import common from '@ohos.app.ability.common'
import { HttpClient } from '../core/HttpClient'
import type { AppVersionInfo, RnBundleInfo } from '../core/Types'
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>(
`/api/v1/updates/app/check?appKey=${appKey}&versionCode=${currentVersionCode}&platform=harmony`
)
if (data.latestVersionCode <= currentVersionCode) {
return { hasUpdate: false }
}
return { hasUpdate: true, info: data }
}
static async checkRnUpdate(
appKey: string,
bundleName: string,
currentBundleVersion: number
): Promise<RnUpdateResult> {
const data = await HttpClient.get<RnBundleInfo>(
`/api/v1/rn/update/check?appKey=${appKey}&bundleName=${bundleName}&bundleVersion=${currentBundleVersion}`
)
if (data.bundleVersion <= currentBundleVersion) {
return { hasUpdate: false }
}
return { hasUpdate: true, info: data }
}
static async downloadRnBundle(
context: common.UIAbilityContext,
downloadUrl: string,
destFilename: string
): Promise<string> {
const destPath = context.cacheDir + '/' + destFilename
await new Promise<void>((resolve, reject) => {
request.downloadFile(context, {
url: downloadUrl,
filePath: destPath,
}, (err, task) => {
if (err) { reject(err); return }
task.on('complete', resolve)
task.on('fail', (error: number) => reject(new Error(`Download failed: ${error}`)))
})
})
if (SDKContext.getConfig().debug) {
console.log('[UpdateSDK] RN bundle downloaded to', destPath)
}
return destPath
}
}