必须要用真机来调试,用模拟器的话就会跳转失败。
TypeScript
/**
* 跳转应用商店更新页
* @param context UIAbility上下文
* 用真机调试
* 微信:com.tencent.wechat 高德:com.amap.hmapp 微信在华为市场的官方ID:C100375477
*/
static jumpToAppStore(context: common.UIAbilityContext) {
let bName = context.applicationInfo.name; // 获取当前包名 context.applicationInfo.name
const want: Want = {
abilityName: 'EntryAbility',
uri: "store://appgallery.huawei.com/app/detail?id=" + bName
};
context.startAbility(want).then(()=>{
console.log("跳转成功!");
}).catch((err: Error)=>{
console.log("跳转失败!" + err.message);
});
}
示意图
自己后端的版本更新接口,用来判断版本号和返回更新内容,以及返回配置信息。

必须要用真机来调试跳转应用市场,用模拟器的话就会跳转失败,不会弹窗下面这个弹窗。

app还没上架时,跳转商店就会展示如下页面,提示"此应用暂不支持在当前设备安装"。

我们可以更换成微信包名"com.tencent.wechat",再次跳转应用商店,就能看见微信的应用详情页面。

完整代码
TypeScript
import bundleManager from '@ohos.bundle.bundleManager';
import type { common, Want } from '@kit.AbilityKit';
import { AppUserDataShare } from '../data/MFUserDataShared';
import { httpReq } from '../network/HttpService';
import { plainToClass } from 'class-transformer';
import { MFAppVersionModel } from '../../pages/models/MFAppVersionModel';
import { Toast } from '../custom/Toast';
export class VersionUtil {
/**
* 获取当前应用版本信息
* @returns Promise<string> 格式: 版本名(版本号)
*/
static async getCurrentVersion(): Promise<string> {
try {
const bundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
return `${bundleInfo.versionName}`; //`${bundleInfo.versionName}(${bundleInfo.versionCode})`;
} catch (err) {
//console.error(`[VersionUtil] getCurrentVersion error: ${err.message}`);
return '';
}
}
/**
* 异步获取应用自身版本信息
*/
static async getAppVersionCode(): Promise<number> {
try {
const bundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
return bundleInfo.versionCode;
} catch (err) {
//console.error('获取版本信息失败:', err);
return 1000000;
}
}
/**
* 获取版本更新信息
* @param resBLock
* res true:成功 false:失败
*/
static requestAppVersionApi(showToast: boolean, resBlock?: (res: boolean, vModel?: MFAppVersionModel)=>void) {
if (!AppUserDataShare.isLogin()) { return }
let par = {
'platform': 'harmony'
} as Record<string, number | String>;
httpReq.get('/Org/SaaSAppVersion', par).then(async resData => {
if (resData.RetData != undefined) {
let versionModel = plainToClass(MFAppVersionModel, resData.RetData);
// let tempEnForce = versionModel?.en_force ?? false
let reqVersionCode = Number(versionModel.version_code); // 接口返回Code
let curVersionCode = await VersionUtil.getAppVersionCode(); // 本地Code
if (reqVersionCode > curVersionCode) {
//console.log('检测到新版本');
let tempShowAlert = versionModel?.show_alert ?? true
if (tempShowAlert) {
resBlock?.(true, versionModel)
} else {
resBlock?.(false)
}
} else {
if (showToast) {
Toast.show('当前已是最新版本')
}
resBlock?.(false)
}
} else {
resBlock?.(false)
}
})
.catch((error: Error) => {
resBlock?.(false)
});
}
/**
* 跳转应用商店更新页
* @param context UIAbility上下文
* 用真机调试
* 微信:com.tencent.wechat 高德:com.amap.hmapp 微信在华为市场的官方ID:C100375477
*/
static jumpToAppStore(context: common.UIAbilityContext) {
let bName = context.applicationInfo.name; // 获取当前包名 context.applicationInfo.name
// todo_zdm //bName = 'com.tencent.wechat'
const want: Want = {
abilityName: 'EntryAbility',
uri: "store://appgallery.huawei.com/app/detail?id=" + bName
};
context.startAbility(want).then(()=>{
// 拉起成功
console.log("跳转成功!");
}).catch((err: Error)=>{
// 拉起失败
console.log("跳转失败!" + err.message);
//console.log("跳转失败!" + JSON.stringify(err));
});
}
}
TypeScript
import { Expose, Transform } from "class-transformer";
import { BaseModel } from "../../support/base/BaseModel";
@Expose()
export class MFAppVersionModel extends BaseModel {
@Expose()
@Transform((obj) => obj.value ?? '')
content: string = '' // 更新内容
@Expose()
@Transform((obj) => obj.value ?? '')
download_url: string = '' // 下载地址
@Expose()
@Transform((obj) => obj.value ?? false)
en_force: boolean = false // 是否强制更新 true
@Expose()
@Transform((obj) => obj.value ?? '')
platform: string = '' // 平台 "harmony"
@Expose()
@Transform((obj) => obj.value ?? true)
show_alert: boolean = true // 是否展示弹窗 true
@Expose()
@Transform((obj) => obj.value ?? '')
version_code: string = '' // 版本号
}
TypeScript
{
"app": {
"bundleName": "com.mofshar.www",
"vendor": "mofsaas",
"versionCode": 1000000,
"versionName": "1.0.0",
"icon": "$media:layered_image",
"label": "$string:app_name"
}
}