openharmony 鸿蒙 js-apis-distributed-account

2025-06-12 浏览 (1)

@ohos.account.distributedAccount (Distributed Account Management)

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.

Modules to Import

import { distributedAccount } from '@kit.BasicServicesKit';

distributedAccount.getDistributedAccountAbility

getDistributedAccountAbility(): DistributedAccountAbility

Obtains a DistributedAccountAbility instance.

System capability: SystemCapability.Account.OsAccount

Return value

TypeDescription
DistributedAccountAbilityDistributedAccountAbility instance obtained. This instance provides APIs for querying and updating the login state of a distributed account.

Example

const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();

DistributedAccountAbility

Provides APIs for querying and updating the login state of a distributed account. You must obtain a DistributedAccountAbility instance first.

getOsAccountDistributedInfo9+

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 (available only for system applications), ohos.permission.GET_DISTRIBUTED_ACCOUNTS (available only for system applications), or ohos.permission.DISTRIBUTED_DATASYNC

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<DistributedInfo>YesCallback 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

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001System 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.log('getOsAccountDistributedInfo exception: ' + JSON.stringify(err));
      } else {
        console.log('distributed information: ' + JSON.stringify(data));
      }
    });
} catch (err) {
  console.log('getOsAccountDistributedInfo exception: ' + JSON.stringify(err));
}

getOsAccountDistributedInfo9+

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 (available only for system applications), ohos.permission.GET_DISTRIBUTED_ACCOUNTS (available only for system applications), or ohos.permission.DISTRIBUTED_DATASYNC

Return value

TypeDescription
Promise<DistributedInfo>Promise used to return the distributed account information obtained.

Error codes

IDError Message
201Permission denied.
12300001System 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.log('getOsAccountDistributedInfo exception: '  + JSON.stringify(err));
  });
} catch (err) {
  console.log('getOsAccountDistributedInfo exception: ' + JSON.stringify(err));
}

queryOsAccountDistributedInfo(deprecated)

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 (available only for system applications) or ohos.permission.DISTRIBUTED_DATASYNC

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<DistributedInfo>YesCallback 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.log('queryOsAccountDistributedInfo exception: ' + JSON.stringify(err));
    } else {
      console.log('distributed information: ' + JSON.stringify(data));
    }
  });

queryOsAccountDistributedInfo(deprecated)

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 (available only for system applications) or ohos.permission.DISTRIBUTED_DATASYNC

Return value

TypeDescription
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.log('queryOsAccountDistributedInfo exception: '  + JSON.stringify(err));
});

setOsAccountDistributedInfo9+

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

NameTypeMandatoryDescription
accountInfoDistributedInfoYesDistributed account information to set.
callbackAsyncCallback<void>YesCallback used to return the result. If the distributed account information is set successfully, err is undefined. Otherwise, err is an error object.

Error codes

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001System service exception.
12300002Invalid accountInfo.
12300003Account 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.log('setOsAccountDistributedInfo exception: ' + JSON.stringify(err));
    } else {
      console.log('setOsAccountDistributedInfo successfully');
    }
  });
} catch (err) {
    console.log('setOsAccountDistributedInfo exception: ' + JSON.stringify(err));
}

setOsAccountDistributedInfo9+

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

NameTypeMandatoryDescription
accountInfoDistributedInfoYesDistributed account information to set.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001System service exception.
12300002Invalid accountInfo.
12300003Account 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.log('setOsAccountDistributedInfo exception: '  + JSON.stringify(err));
  });
} catch (err) {
    console.log('setOsAccountDistributedInfo exception: ' + JSON.stringify(err));
}

updateOsAccountDistributedInfo(deprecated)

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

NameTypeMandatoryDescription
accountInfoDistributedInfoYesDistributed account information to set.
callbackAsyncCallback<void>YesCallback 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.log('queryOsAccountDistributedInfo exception: ' + JSON.stringify(err));
  } else {
    console.log('queryOsAccountDistributedInfo successfully');
  }
});

updateOsAccountDistributedInfo(deprecated)

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

NameTypeMandatoryDescription
accountInfoDistributedInfoYesDistributed account information.

Return value

TypeDescription
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.log('updateOsAccountDistributedInfo exception: '  + JSON.stringify(err));
});

DistributedInfo

Represents the distributed information about a system account.

System capability: SystemCapability.Account.OsAccount

NameTypeMandatoryDescription
namestringYesName of the distributed account. It must be a non-null string.
idstringYesUID of the distributed account. It must be a non-null string.
eventstringYesLogin 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+stringNoNickname of the distributed account. By default, no value is passed in.
avatar9+stringNoAvatar of the distributed account. By default, no value is passed in.
status10+DistributedAccountStatusNoStatus of the distributed account. The value is of the enumerated type. The default status is unlogged.
scalableData8+objectNoAdditional information about the distributed account, in the form of KV pairs. This parameter is left empty by default.

DistributedAccountStatus10+

Enumerates the statuses of a distributed account.

System capability: SystemCapability.Account.OsAccount

NameValueDescription
NOT_LOGGED_IN0The account has not logged in.
LOGGED_IN1The account has logged in.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Basic Services Kit

harmony 鸿蒙DeviceInfo

harmony 鸿蒙InitSync

harmony 鸿蒙OH_Print

harmony 鸿蒙OsAccount

harmony 鸿蒙Pasteboard

harmony 鸿蒙Print_Margin

harmony 鸿蒙Print_PageSize

harmony 鸿蒙Print_PrintAttributes

harmony 鸿蒙Print_PrintDocCallback

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/V3rS5e