The distributedAccount module provides APIs for managing distributed accounts, including querying and updating account login states.
NOTE
The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
import { distributedAccount } from '@kit.BasicServicesKit';
getDistributedAccountAbility(): DistributedAccountAbility
Obtains a DistributedAccountAbility instance.
System capability: SystemCapability.Account.OsAccount
Return value
Type | Description |
---|---|
DistributedAccountAbility | DistributedAccountAbility instance obtained. This instance provides APIs for querying and updating the login state of a distributed account. |
Example
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
Provides APIs for querying and updating the login state of a distributed account. You must obtain a DistributedAccountAbility instance first.
getOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void
Obtains distributed account information. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Account.OsAccount
Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS (for system applications only), ohos.permission.GET_DISTRIBUTED_ACCOUNTS (for system applications only), or ohos.permission.DISTRIBUTED_DATASYNC
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<DistributedInfo> | Yes | Callback used to return the result. If the operation is successful, err is undefined and data is the distributed account information obtained. Otherwise, err is an error object. |
Error codes
ID | Error Message |
---|---|
201 | Permission denied. |
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
12300001 | System service exception. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
try {
accountAbility.getOsAccountDistributedInfo(
(err: BusinessError, data: distributedAccount.DistributedInfo) => {
if (err) {
console.error('getOsAccountDistributedInfo exception: ' + JSON.stringify(err));
} else {
console.log('distributed information: ' + JSON.stringify(data));
}
});
} catch (err) {
console.error('getOsAccountDistributedInfo exception: ' + JSON.stringify(err));
}
getOsAccountDistributedInfo(): Promise<DistributedInfo>
Obtains distributed account information. This API uses a promise to return the result.
System capability: SystemCapability.Account.OsAccount
Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS (for system applications only), ohos.permission.GET_DISTRIBUTED_ACCOUNTS (for system applications only), or ohos.permission.DISTRIBUTED_DATASYNC
Return value
Type | Description |
---|---|
Promise<DistributedInfo> | Promise used to return the distributed account information obtained. |
Error codes
ID | Error Message |
---|---|
201 | Permission denied. |
12300001 | System service exception. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
try {
accountAbility.getOsAccountDistributedInfo().then((data: distributedAccount.DistributedInfo) => {
console.log('distributed information: ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
console.error('getOsAccountDistributedInfo exception: ' + JSON.stringify(err));
});
} catch (err) {
console.error('getOsAccountDistributedInfo exception: ' + JSON.stringify(err));
}
queryOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void
Queries distributed account information. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getOsAccountDistributedInfo instead.
System capability: SystemCapability.Account.OsAccount
Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS (for system applications only) or ohos.permission.DISTRIBUTED_DATASYNC
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<DistributedInfo> | Yes | Callback used to return the result. If the operation is successful, err is undefined and data is the distributed account information obtained. Otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
accountAbility.queryOsAccountDistributedInfo(
(err: BusinessError, data: distributedAccount.DistributedInfo) => {
if (err) {
console.error('queryOsAccountDistributedInfo exception: ' + JSON.stringify(err));
} else {
console.log('distributed information: ' + JSON.stringify(data));
}
});
queryOsAccountDistributedInfo(): Promise<DistributedInfo>
Queries distributed account information. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use getOsAccountDistributedInfo instead.
System capability: SystemCapability.Account.OsAccount
Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS (for system applications only) or ohos.permission.DISTRIBUTED_DATASYNC
Return value
Type | Description |
---|---|
Promise<DistributedInfo> | Promise used to return the distributed account information obtained. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
accountAbility.queryOsAccountDistributedInfo().then((data: distributedAccount.DistributedInfo) => {
console.log('distributed information: ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
console.error('queryOsAccountDistributedInfo exception: ' + JSON.stringify(err));
});
setOsAccountDistributedInfo(accountInfo: DistributedInfo, callback: AsyncCallback<void>): void
Sets the distributed account information. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Account.OsAccount
Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS (available only for system applications)
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
accountInfo | DistributedInfo | Yes | Distributed account information to set. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the distributed account information is set successfully, err is undefined. Otherwise, err is an error object. |
Error codes
ID | Error Message |
---|---|
201 | Permission denied. |
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
12300001 | System service exception. |
12300002 | Invalid accountInfo. |
12300003 | Account not found. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
let accountInfo: distributedAccount.DistributedInfo =
{id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
try {
accountAbility.setOsAccountDistributedInfo(accountInfo, (err: BusinessError) => {
if (err) {
console.error('setOsAccountDistributedInfo exception: ' + JSON.stringify(err));
} else {
console.log('setOsAccountDistributedInfo successfully');
}
});
} catch (err) {
console.error('setOsAccountDistributedInfo exception: ' + JSON.stringify(err));
}
setOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void>
Sets the distributed account information. This API uses a promise to return the result.
System capability: SystemCapability.Account.OsAccount
Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS (available only for system applications)
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
accountInfo | DistributedInfo | Yes | Distributed account information to set. |
Return value
Type | Description |
---|---|
Promise<void> | Promise that returns no value. |
Error codes
ID | Error Message |
---|---|
201 | Permission denied. |
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
12300001 | System service exception. |
12300002 | Invalid accountInfo. |
12300003 | Account not found. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
let accountInfo: distributedAccount.DistributedInfo =
{id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
try {
accountAbility.setOsAccountDistributedInfo(accountInfo).then(() => {
console.log('setOsAccountDistributedInfo successfully');
}).catch((err: BusinessError) => {
console.error('setOsAccountDistributedInfo exception: ' + JSON.stringify(err));
});
} catch (err) {
console.error('setOsAccountDistributedInfo exception: ' + JSON.stringify(err));
}
updateOsAccountDistributedInfo(accountInfo: DistributedInfo, callback: AsyncCallback<void>): void
Updates the distributed account information. This API uses an asynchronous callback to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use setOsAccountDistributedInfo instead.
System capability: SystemCapability.Account.OsAccount
Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS (available only for system applications)
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
accountInfo | DistributedInfo | Yes | Distributed account information to update. |
callback | AsyncCallback<void> | Yes | Callback used to return the result. If the distributed account information is updated successfully, err is undefined. Otherwise, err is an error object. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
let accountInfo: distributedAccount.DistributedInfo =
{id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
accountAbility.updateOsAccountDistributedInfo(accountInfo, (err: BusinessError) => {
if (err) {
console.error('queryOsAccountDistributedInfo exception: ' + JSON.stringify(err));
} else {
console.log('queryOsAccountDistributedInfo successfully');
}
});
updateOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void>
Updates the distributed account information. This API uses a promise to return the result.
NOTE
This API is supported since API version 7 and deprecated since API version 9. Use setOsAccountDistributedInfo instead.
System capability: SystemCapability.Account.OsAccount
Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS (available only for system applications)
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
accountInfo | DistributedInfo | Yes | Distributed account information to update. |
Return value
Type | Description |
---|---|
Promise<void> | Promise that returns no value. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
let accountInfo: distributedAccount.DistributedInfo =
{id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
accountAbility.updateOsAccountDistributedInfo(accountInfo).then(() => {
console.log('updateOsAccountDistributedInfo successfully');
}).catch((err: BusinessError) => {
console.error('updateOsAccountDistributedInfo exception: ' + JSON.stringify(err));
});
Represents the distributed information about a system account.
System capability: SystemCapability.Account.OsAccount
Name | Type | Mandatory | Description |
---|---|---|---|
name | string | Yes | Name of the distributed account. It must be a non-null string. |
id | string | Yes | UID of the distributed account. It must be a non-null string. |
event | string | Yes | Login state of the distributed account. The state can be login, logout, token invalid, or logoff, which correspond to the following strings respectively: - Ohos.account.event.LOGIN - Ohos.account.event.LOGOUT - Ohos.account.event.TOKEN_INVALID - Ohos.account.event.LOGOFF |
nickname9+ | string | No | Nickname of the distributed account. By default, no value is passed in. |
avatar9+ | string | No | Avatar of the distributed account. By default, no value is passed in. |
status10+ | DistributedAccountStatus | No | Status of the distributed account. The value is of the enumerated type. The default status is unlogged. |
scalableData8+ | object | No | Additional information about the distributed account, in the form of KV pairs. This parameter is left empty by default. |
Enumerates the statuses of a distributed account.
System capability: SystemCapability.Account.OsAccount
Name | Value | Description |
---|---|---|
NOT_LOGGED_IN | 0 | The account has not logged in. |
LOGGED_IN | 1 | The account has logged in. |
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。