1# @ohos.account.distributedAccount (分布式帐号管理)(系统接口) 2 3本模块提供管理分布式帐号的一些基础功能,主要包括查询和更新帐号登录状态。 4 5> **说明:** 6> 7> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.account.distributedAccount (分布式帐号管理)](js-apis-distributed-account.md)。 9 10## 导入模块 11 12```ts 13import account_distributedAccount from '@ohos.account.distributedAccount'; 14``` 15 16## DistributedAccountAbility 17 18提供查询和更新分布式帐号登录状态方法(需要先获取分布式帐号的单实例对象)。 19 20### getOsAccountDistributedInfoByLocalId<sup>10+</sup> 21 22getOsAccountDistributedInfoByLocalId(localId: number, callback: AsyncCallback<DistributedInfo>): void 23 24获取指定系统帐号的分布式信息。使用callback异步回调。 25 26**系统接口:** 此接口为系统接口。 27 28**系统能力:** SystemCapability.Account.OsAccount 29 30**需要权限:** ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS 或 ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 31 32**参数:** 33 34 | 参数名 | 类型 | 必填 | 说明 | 35 | -------- | -------- | -------- | -------- | 36 | localId | number | 是 | 系统帐号ID。 | 37 | callback | AsyncCallback<[DistributedInfo](js-apis-distributed-account.md#distributedinfo)> | 是 | 回调参数。当获取分布式帐号信息成功,err为undefined,data为获取到的分布式帐号信息对象;否则为错误对象。 | 38 39**错误码:** 40 41| 错误码ID | 错误信息| 42| -------- | ------------------- | 43| 12300001 | System service exception. | 44| 12300003 | Account not found. | 45 46**示例:** 47 48```ts 49import { BusinessError } from '@ohos.base'; 50 51const accountAbility: account_distributedAccount.DistributedAccountAbility = account_distributedAccount.getDistributedAccountAbility(); 52try { 53 accountAbility.getOsAccountDistributedInfoByLocalId(100, 54 (err: BusinessError, data: account_distributedAccount.DistributedInfo) => { 55 if (err) { 56 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 57 } else { 58 console.log('distributed information: ' + JSON.stringify(data)); 59 } 60 }); 61} catch (err) { 62 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 63} 64``` 65 66### getOsAccountDistributedInfoByLocalId<sup>10+</sup> 67 68getOsAccountDistributedInfoByLocalId(localId: number): Promise<DistributedInfo> 69 70获取指定系统帐号的分布式信息。使用Promise异步回调。 71 72**系统接口:** 此接口为系统接口。 73 74**系统能力:** SystemCapability.Account.OsAccount 75 76**需要权限:** ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS 或 ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 77 78**返回值:** 79 80 | 类型 | 说明 | 81 | -------- | -------- | 82 | Promise<[DistributedInfo](js-apis-distributed-account.md#distributedinfo)> | Promise对象,返回分布式帐号信息对象。 | 83 84**错误码:** 85 86| 错误码ID | 错误信息| 87| -------- | ------------------- | 88| 12300001 | System service exception. | 89| 12300003 | Account not found. | 90 91**示例:** 92 93```ts 94import { BusinessError } from '@ohos.base'; 95 96const accountAbility: account_distributedAccount.DistributedAccountAbility = account_distributedAccount.getDistributedAccountAbility(); 97try { 98 accountAbility.getOsAccountDistributedInfoByLocalId(100).then(( 99 data: account_distributedAccount.DistributedInfo) => { 100 console.log('distributed information: ' + JSON.stringify(data)); 101 }).catch((err: BusinessError) => { 102 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 103 }); 104} catch (err) { 105 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 106} 107``` 108 109### setOsAccountDistributedInfoByLocalId<sup>10+</sup> 110 111setOsAccountDistributedInfoByLocalId(localId: number, distributedInfo: DistributedInfo, callback: AsyncCallback<void>): void 112 113设置指定系统帐号的分布式信息。使用callback异步回调。 114 115**系统接口:** 此接口为系统接口。 116 117**系统能力:** SystemCapability.Account.OsAccount 118 119**需要权限:** ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS 120 121**参数:** 122 123 | 参数名 | 类型 | 必填 | 说明 | 124 | -------- | -------- | -------- | -------- | 125 | localId | number | 是 | 系统帐号ID。 | 126 | accountInfo | [DistributedInfo](js-apis-distributed-account.md#distributedinfo) | 是 | 分布式帐号信息。 | 127 | callback | AsyncCallback<void> | 是 | 回调函数。当设置指定系统帐号的分布式信息成功时,err为undefined,否则为错误对象。 | 128 129**错误码:** 130 131| 错误码ID | 错误信息| 132| -------- | ------------------- | 133| 12300001 | System service exception. | 134| 12300002 | Invalid distributedInfo. | 135| 12300003 | Account identified by localId or by distributedInfo not found. | 136| 12300008 | Restricted OS account. | 137 138**示例:** 139 140```ts 141import { BusinessError } from '@ohos.base'; 142 143const accountAbility: account_distributedAccount.DistributedAccountAbility = account_distributedAccount.getDistributedAccountAbility(); 144let accountInfo: account_distributedAccount.DistributedInfo = 145 {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'}; 146try { 147 accountAbility.setOsAccountDistributedInfoByLocalId(100, accountInfo, (err: BusinessError) => { 148 if (err) { 149 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 150 } else { 151 console.log('setOsAccountDistributedInfoByLocalId successfully'); 152 } 153 }); 154} catch (err) { 155 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 156} 157``` 158 159### setOsAccountDistributedInfoByLocalId<sup>10+</sup> 160 161setOsAccountDistributedInfoByLocalId(localId: number, distributedInfo: DistributedInfo): Promise<void> 162 163设置指定系统帐号的分布式信息。使用Promise异步回调。 164 165**系统接口:** 此接口为系统接口。 166 167**系统能力:** SystemCapability.Account.OsAccount 168 169**需要权限:** ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS 170 171**参数:** 172 173 | 参数名 | 类型 | 必填 | 说明 | 174 | -------- | -------- | -------- | -------- | 175 | localId | number | 是 | 系统帐号ID。 | 176 | distributedInfo | [DistributedInfo](js-apis-distributed-account.md#distributedinfo) | 是 | 分布式帐户信息。 | 177 178**返回值:** 179 180 | 类型 | 说明 | 181 | -------- | -------- | 182 | Promise<void> | Promise对象,无返回结果的Promise对象。 | 183 184**错误码:** 185 186| 错误码ID | 错误信息| 187| -------- | ------------------- | 188| 12300001 | System service exception. | 189| 12300002 | Invalid distributedInfo. | 190| 12300003 | Account identified by localId or by distributedInfo not found. | 191| 12300008 | Restricted OS account. | 192 193**示例:** 194 195```ts 196import { BusinessError } from '@ohos.base'; 197 198const accountAbility: account_distributedAccount.DistributedAccountAbility = account_distributedAccount.getDistributedAccountAbility(); 199let accountInfo: account_distributedAccount.DistributedInfo = 200 {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'}; 201try { 202 accountAbility.setOsAccountDistributedInfoByLocalId(100, accountInfo).then(() => { 203 console.log('setOsAccountDistributedInfoByLocalId successfully'); 204 }).catch((err: BusinessError) => { 205 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 206 }); 207} catch (err) { 208 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 209} 210``` 211