1# 管理群组关键资产(ArkTS) 2 3以下为管理群组关键资产使用示例,请先查看开发指导: 4 5- [新增关键资产(ArkTS)](asset-js-add.md) 6- [删除关键资产(ArkTS)](asset-js-remove.md) 7- [更新关键资产(ArkTS)](asset-js-update.md) 8- [查询关键资产(ArkTS)](asset-js-query.md) 9 10## 前置条件 11 12在应用配置文件app.json5中,配置群组ID:demo_group_id。 13 14```json 15{ 16 "app": { 17 //其他配置项此处省略 18 "assetAccessGroups": [ 19 "demo_group_id" 20 ] 21 } 22} 23``` 24 25## 新增群组关键资产 26 27在群组中新增一条密码是demo_pwd,别名是demo_alias,附属信息是demo_label的关键资产,该关键资产在用户首次解锁设备后可被访问。 28 29```typescript 30import { asset } from '@kit.AssetStoreKit'; 31import { util } from '@kit.ArkTS'; 32import { BusinessError } from '@kit.BasicServicesKit'; 33 34function stringToArray(str: string): Uint8Array { 35 let textEncoder = new util.TextEncoder(); 36 return textEncoder.encodeInto(str); 37} 38 39let attr: asset.AssetMap = new Map(); 40attr.set(asset.Tag.SECRET, stringToArray('demo_pwd')); 41attr.set(asset.Tag.ALIAS, stringToArray('demo_alias')); 42attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_FIRST_UNLOCKED); 43attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label')); 44attr.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id')); 45try { 46 asset.add(attr).then(() => { 47 console.info(`Asset added to the group successfully.`); 48 }).catch((err: BusinessError) => { 49 console.error(`Failed to add Asset to the group. Code is ${err.code}, message is ${err.message}`); 50 }) 51} catch (error) { 52 let err = error as BusinessError; 53 console.error(`Failed to add Asset to the group. Code is ${err.code}, message is ${err.message}`); 54} 55``` 56 57## 删除群组关键资产 58 59在群组中删除一条别名是demo_alias的关键资产。 60 61```typescript 62import { asset } from '@kit.AssetStoreKit'; 63import { util } from '@kit.ArkTS'; 64import { BusinessError } from '@kit.BasicServicesKit'; 65 66function stringToArray(str: string): Uint8Array { 67 let textEncoder = new util.TextEncoder(); 68 return textEncoder.encodeInto(str); 69} 70 71let query: asset.AssetMap = new Map(); 72query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // 此处指定别名删除单条群组关键资产,也可不指定别名删除多条群组关键资产 73query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id')); 74try { 75 asset.remove(query).then(() => { 76 console.info(`Asset removed from the group successfully.`); 77 }).catch((err: BusinessError) => { 78 console.error(`Failed to remove Asset from the group. Code is ${err.code}, message is ${err.message}`); 79 }); 80} catch (error) { 81 let err = error as BusinessError; 82 console.error(`Failed to remove Asset from the group. Code is ${err.code}, message is ${err.message}`); 83} 84``` 85 86## 更新群组关键资产 87 88在群组中更新别名是demo_alias的关键资产,将关键资产明文更新为demo_pwd_new,附属属性更新成demo_label_new。 89 90```typescript 91import { asset } from '@kit.AssetStoreKit'; 92import { util } from '@kit.ArkTS'; 93import { BusinessError } from '@kit.BasicServicesKit'; 94 95function stringToArray(str: string): Uint8Array { 96 let textEncoder = new util.TextEncoder(); 97 return textEncoder.encodeInto(str); 98} 99 100let query: asset.AssetMap = new Map(); 101query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); 102query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id')); 103let attrsToUpdate: asset.AssetMap = new Map(); 104attrsToUpdate.set(asset.Tag.SECRET, stringToArray('demo_pwd_new')); 105attrsToUpdate.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label_new')); 106try { 107 asset.update(query, attrsToUpdate).then(() => { 108 console.info(`Asset in the group updated successfully.`); 109 }).catch((err: BusinessError) => { 110 console.error(`Failed to update Asset in the group. Code is ${err.code}, message is ${err.message}`); 111 }); 112} catch (error) { 113 let err = error as BusinessError; 114 console.error(`Failed to update Asset in the group. Code is ${err.code}, message is ${err.message}`); 115} 116``` 117 118## 查询单条群组关键资产明文 119 120在群组中查询别名是demo_alias的关键资产明文。 121 122```typescript 123import { asset } from '@kit.AssetStoreKit'; 124import { util } from '@kit.ArkTS'; 125import { BusinessError } from '@kit.BasicServicesKit'; 126 127function stringToArray(str: string): Uint8Array { 128 let textEncoder = new util.TextEncoder(); 129 return textEncoder.encodeInto(str); 130} 131 132function arrayToString(arr: Uint8Array): string { 133 let textDecoder = util.TextDecoder.create("utf-8", { ignoreBOM: true }); 134 let str = textDecoder.decodeToString(arr, { stream: false }) 135 return str; 136} 137 138let query: asset.AssetMap = new Map(); 139query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // 指定了群组关键资产别名,最多查询到一条满足条件的群组关键资产 140query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL); // 此处表示需要返回群组关键资产的所有信息,即属性+明文 141query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id')); 142try { 143 asset.query(query).then((res: Array<asset.AssetMap>) => { 144 for (let i = 0; i < res.length; i++) { 145 // parse the secret. 146 let secret: Uint8Array = res[i].get(asset.Tag.SECRET) as Uint8Array; 147 // parse uint8array to string 148 let secretStr: string = arrayToString(secret); 149 } 150 }).catch ((err: BusinessError) => { 151 console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`); 152 }); 153} catch (error) { 154 let err = error as BusinessError; 155 console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`); 156} 157``` 158 159## 查询单条群组关键资产属性 160 161在群组中查询别名是demo_alias的关键资产属性。 162 163```typescript 164import { asset } from '@kit.AssetStoreKit'; 165import { util } from '@kit.ArkTS'; 166import { BusinessError } from '@kit.BasicServicesKit'; 167 168function stringToArray(str: string): Uint8Array { 169 let textEncoder = new util.TextEncoder(); 170 return textEncoder.encodeInto(str); 171} 172 173let query: asset.AssetMap = new Map(); 174query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // 指定了群组关键资产别名,最多查询到一条满足条件的群组关键资产 175query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ATTRIBUTES); // 此处表示仅返回群组关键资产属性,不包含群组关键资产明文 176query.set(asset.Tag.GROUP_ID, stringToArray('demo_group_id')); 177try { 178 asset.query(query).then((res: Array<asset.AssetMap>) => { 179 for (let i = 0; i < res.length; i++) { 180 // parse the attribute. 181 let accessibility: number = res[i].get(asset.Tag.ACCESSIBILITY) as number; 182 } 183 }).catch ((err: BusinessError) => { 184 console.error(`Failed to query Asset from the group. Code is ${err.code}, message is ${err.message}`); 185 }); 186} catch (error) { 187 let err = error as BusinessError; 188 console.error(`Failed to query Asset from the group. Code is ${err.code}, message is ${err.message}`); 189} 190``` 191