鸿蒙NEXT开发实战往期必看文章:
一分钟了解”纯血版!鸿蒙HarmonyOS Next应用开发!
“非常详细的” 鸿蒙HarmonyOS Next应用开发学习路线!(从零基础入门到精通)
HarmonyOS NEXT应用开发案例实践总结合(持续更新......)
HarmonyOS NEXT应用开发性能优化实践总结(持续更新......)
场景描述
HarmonyOS只提供了OAID和AAID,OAID需要权限涉及用户不同意和重置问题,AAID卸载应用或清除数据均会变化。如果需要保持应用在卸载时保证设备ID的持久化,则可用Asset Store Kit(关键资产存储开发服务)包含了关键资产存储服务开放的接口能力集合,提供了用户短敏感数据的安全存储及管理能力。其中,短敏感数据可以是密码类(账号/密码)、Token类(应用凭据)、其他关键明文(如银行卡号)等长度较短的用户敏感数据。
方案描述
场景一
我们在登录账号之后,卸载应用,再重装应用,我们的账号以及ID依旧存在。
效果图
方案
1.在module.json5配置权限ohos.permission.STORE_PERSISTENT_DATA
2.在调用add接口时,请设置一下IS_PERSISTENT这个属性为true则可实现卸载时保留数据attr.set(asset.Tag.IS_PERSISTENT, true)。3.将账号密码保存到asset中,读取的时候查询数据库中是否含有ID和Alias,如果没有说明是新设备则弹窗进行信任,信任成功时将id存入进去,如果已经登录过就不用进行弹窗信任,直接展示存入的容。卸载应用,重新安装,我们可以重新登入并且可以看到原来存储的数据且不会弹窗,说明账号以及id都存在,实现了持久化存储。
核心代码
async aboutToAppear(): Promise<void> {
let query: asset.AssetMap = new Map();
query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ATTRIBUTES); //返回关键资产属性,不含关键资产明文。
try {
let res = asset.querySync(query)
let alias = res[0].get(asset.Tag.ALIAS) as Uint8Array //查询出存储的别名alias
let aliasStr = arrayToString(alias)
let query2: asset.AssetMap = new Map();
query2.set(asset.Tag.ALIAS, stringToArray(aliasStr))
query2.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL) //返回关键资产明文及属性
let res2 = asset.querySync(query2)
for (let i = 0; i < res2.length; i++) { //查询ID以及手机类型
ID = res[i].get(asset.Tag.SECRET) as Uint8Array;
IDStr = arrayToString(ID);
deviceType = res[i].get(asset.Tag.DATA_LABEL_NORMAL_1) as Uint8Array;
deviceTypeStr = arrayToString(deviceType)
}
} catch (err) {
if (err) {
console.log('暂无设备')
}
}
}
async function login(account: string, password: string) {
deviceTypeStr = deviceInfo.marketName
let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray(account)); // 指定了关键资产别名,最多查询到一条满足条件的关键资产
query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL); // 此处表示需要返回关键资产的所有信息,即属性+明文
await asset.query(query).then((res: Array<asset.AssetMap>) => {
for (let i = 0; i < res.length; i++) { //如果查询的结果与之匹配,说明已经登陆过了
inputAccount = res[i].get(asset.Tag.ALIAS) as Uint8Array;
inputAccountStr = arrayToString(inputAccount);
ID = res[i].get(asset.Tag.SECRET) as Uint8Array;
IDStr = arrayToString(ID);
deviceType = res[i].get(asset.Tag.DATA_LABEL_NORMAL_1) as Uint8Array;
deviceTypeStr = arrayToString(deviceType)
if (account == inputAccountStr && password == '123456') {
let myList: MesList = new MesList(inputAccountStr, IDStr, deviceTypeStr)
router.pushUrl({
url: 'pages/Search_AssetLogin', //跳转页面展示存储的东西
params: { myList: myList }
})
} else {
console.log('密码错误')
}
}
}).catch(async (err: BusinessError) => {
if (err.code === 24000002) {
AlertDialog.show(
{
title: '是否信任此设备',
subtitle: '',
message: '',
autoCancel: true,
alignment: DialogAlignment.Bottom,
gridCount: 4,
offset: { dx: 0, dy: -20 },
primaryButton: {
value: '取消',
action: () => {
log('请重新登录')
}
},
secondaryButton: {
enabled: true,
defaultFocus: true,
style: DialogButtonStyle.HIGHLIGHT,
value: '确认',
action: () => {
let attr: asset.AssetMap = new Map();
attr.set(asset.Tag.ALIAS, stringToArray(account))
attr.set(asset.Tag.SECRET, stringToArray(state.ID))
attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray(deviceTypeStr))
attr.set(asset.Tag.IS_PERSISTENT, true)
try {
asset.addSync(attr); //第一次登录,弹窗点击信任添加数据
log("登录成功")
let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray(account)); // 指定了关键资产别名,最多查询到一条满足条件的关键资产
query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL); // 此处表示需要返回关键资产的所有信息,即属性+明文
let res = asset.querySync(query)
for (let i = 0; i < res.length; i++) { //查询数据跳转页面展示
inputAccount = res[i].get(asset.Tag.ALIAS) as Uint8Array;
inputAccountStr = arrayToString(inputAccount);
ID = res[i].get(asset.Tag.SECRET) as Uint8Array;
IDStr = arrayToString(ID);
deviceType = res[i].get(asset.Tag.DATA_LABEL_NORMAL_1) as Uint8Array;
deviceTypeStr = arrayToString(deviceType)
}
let myList: MesList = new MesList(inputAccountStr, IDStr, deviceTypeStr)
router.pushUrl({
url: 'pages/Search_AssetLogin',
params: { myList: myList }
})
} catch (error) {
if (error.code === 24000003) {
log('请勿重复登录');
} else {
log('登录失败')
}
}
}
},
}
)
} else {
log('查询失败');
}
});
}
export class One {
async remove() {
let query: asset.AssetMap = new Map();
try {
asset.remove(query).then(() => {
console.info(`Asset removed successfully.`);
router.pushUrl({
url: 'pages/Asset_login'
})
console.log('请重新登录')
}).catch((err: BusinessError) => {
console.error(`Failed to remove Asset. Code is ${err.code}, message is ${err.message}`);
});
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to remove Asset. Code is ${err.code}, message is ${err.message}`);
}
}
}
标签:set,进阶,res,HarmonyOS,Tag,let,asset,query,ID
From: https://blog.csdn.net/adaedwa187545/article/details/143625995