harmony 鸿蒙管理群组关键资产(ArkTS)
管理群组关键资产(ArkTS)
以下为管理群组关键资产使用示例,请先查看开发指导:
前置条件
在应用配置文件app.json5中,配置群组ID:demo_group_id。
{
"app": {
//其他配置项此处省略
"assetAccessGroups": [
"demo_group_id"
]
}
}
新增群组关键资产
在群组中新增密码为demo_pwd、别名为demo_alias、附属信息为demo_label的关键资产。该关键资产在用户首次解锁设备后可被访问。
import { asset } from '@kit.AssetStoreKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';
function stringToArray(str: string): Uint8Array {
let textEncoder = new util.TextEncoder();
return textEncoder.encodeInto(str);
}
let attr: asset.AssetMap = new Map();
attr.set(asset.Tag.SECRET, stringToArray('demo_pwd'));
attr.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_FIRST_UNLOCKED);
attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label'));
attr.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
try {
asset.add(attr).then(() => {
console.info(`Asset added to the group successfully.`);
}).catch((err: BusinessError) => {
console.error(`Failed to add Asset to the group. Code is ${err.code}, message is ${err.message}`);
})
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to add Asset to the group. Code is ${err.code}, message is ${err.message}`);
}
删除群组关键资产
在群组中删除别名是demo_alias的关键资产。
import { asset } from '@kit.AssetStoreKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';
function stringToArray(str: string): Uint8Array {
let textEncoder = new util.TextEncoder();
return textEncoder.encodeInto(str);
}
let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // 此处指定别名删除单条群组关键资产,也可不指定别名删除多条群组关键资产
query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
try {
asset.remove(query).then(() => {
console.info(`Asset removed from the group successfully.`);
}).catch((err: BusinessError) => {
console.error(`Failed to remove Asset from the group. Code is ${err.code}, message is ${err.message}`);
});
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to remove Asset from the group. Code is ${err.code}, message is ${err.message}`);
}
更新群组关键资产
在群组中更新别名为demo_alias的关键资产,明文更新为demo_pwd_new,附属属性更新为demo_label_new。
import { asset } from '@kit.AssetStoreKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';
function stringToArray(str: string): Uint8Array {
let textEncoder = new util.TextEncoder();
return textEncoder.encodeInto(str);
}
let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
let attrsToUpdate: asset.AssetMap = new Map();
attrsToUpdate.set(asset.Tag.SECRET, stringToArray('demo_pwd_new'));
attrsToUpdate.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label_new'));
try {
asset.update(query, attrsToUpdate).then(() => {
console.info(`Asset in the group updated successfully.`);
}).catch((err: BusinessError) => {
console.error(`Failed to update Asset in the group. Code is ${err.code}, message is ${err.message}`);
});
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to update Asset in the group. Code is ${err.code}, message is ${err.message}`);
}
查询单条群组关键资产明文
在群组中查询别名为demo_alias的关键资产明文。
import { asset } from '@kit.AssetStoreKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';
function stringToArray(str: string): Uint8Array {
let textEncoder = new util.TextEncoder();
return textEncoder.encodeInto(str);
}
function arrayToString(arr: Uint8Array): string {
let textDecoder = util.TextDecoder.create("utf-8", { ignoreBOM: true });
let str = textDecoder.decodeToString(arr, { stream: false })
return str;
}
let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // 指定了群组关键资产别名,最多查询到一条满足条件的群组关键资产
query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL); // 此处表示需要返回群组关键资产的所有信息,即属性+明文
query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
try {
asset.query(query).then((res: Array<asset.AssetMap>) => {
for (let i = 0; i < res.length; i++) {
// parse the secret.
let secret: Uint8Array = res[i].get(asset.Tag.SECRET) as Uint8Array;
// parse uint8array to string
let secretStr: string = arrayToString(secret);
}
}).catch ((err: BusinessError) => {
console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
});
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
}
查询单条群组关键资产属性
在群组中查询别名为demo_alias的关键资产属性。
import { asset } from '@kit.AssetStoreKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';
function stringToArray(str: string): Uint8Array {
let textEncoder = new util.TextEncoder();
return textEncoder.encodeInto(str);
}
let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // 指定了群组关键资产别名,最多查询到一条满足条件的群组关键资产
query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ATTRIBUTES); // 此处表示仅返回群组关键资产属性,不包含群组关键资产明文
query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id'));
try {
asset.query(query).then((res: Array<asset.AssetMap>) => {
for (let i = 0; i < res.length; i++) {
// parse the attribute.
let accessibility: number = res[i].get(asset.Tag.ACCESSIBILITY) as number;
}
}).catch ((err: BusinessError) => {
console.error(`Failed to query Asset from the group. Code is ${err.code}, message is ${err.message}`);
});
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to query Asset from the group. Code is ${err.code}, message is ${err.message}`);
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Asset Store Kit(关键资产存储服务)
harmony 鸿蒙指定用户空间进行关键资产操作(仅对系统应用开放)
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦