分布式帳號(hào)管理

2024-01-23 17:53 更新

本模塊提供管理分布式帳號(hào)的一些基礎(chǔ)功能,主要包括查詢和更新帳號(hào)登錄狀態(tài)。

說(shuō)明

本模塊首批接口從API version 7開始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。

導(dǎo)入模塊

  1. import account_distributedAccount from '@ohos.account.distributedAccount';

account_distributedAccount.getDistributedAccountAbility

getDistributedAccountAbility(): DistributedAccountAbility

獲取分布式帳號(hào)單實(shí)例對(duì)象。

系統(tǒng)能力: SystemCapability.Account.OsAccount

返回值:

類型

說(shuō)明

DistributedAccountAbility

返回一個(gè)實(shí)例,實(shí)例提供查詢和更新分布式帳號(hào)登錄狀態(tài)方法。

示例:

  1. const accountAbility = account_distributedAccount.getDistributedAccountAbility();

DistributedAccountAbility

提供查詢和更新分布式帳號(hào)登錄狀態(tài)方法(需要先獲取分布式帳號(hào)的單實(shí)例對(duì)象)。

getOsAccountDistributedInfo9+

getOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void

獲取分布式帳號(hào)信息,使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.Account.OsAccount

需要權(quán)限: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS 或 ohos.permission.GET_DISTRIBUTED_ACCOUNTS 或 ohos.permission.DISTRIBUTED_DATASYNC

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

callback

AsyncCallback<DistributedInfo>

回調(diào)參數(shù)。當(dāng)獲取分布式帳號(hào)信息成功,err為undefined,data為獲取到的分布式帳號(hào)信息對(duì)象;否則為錯(cuò)誤對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

12300001

System service exception.

示例:

  1. const accountAbility = account_distributedAccount.getDistributedAccountAbility();
  2. try {
  3. accountAbility.getOsAccountDistributedInfo((err, data) => {
  4. console.log("getOsAccountDistributedInfo err: " + JSON.stringify(err));
  5. console.log('Query account info name: ' + data.name);
  6. console.log('Query account info id: ' + data.id);
  7. });
  8. } catch (e) {
  9. console.log("getOsAccountDistributedInfo exception: " + JSON.stringify(e));
  10. }

getOsAccountDistributedInfo9+

getOsAccountDistributedInfo(): Promise<DistributedInfo>

獲取分布式帳號(hào)信息。使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.Account.OsAccount

需要權(quán)限: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS 或 ohos.permission.GET_DISTRIBUTED_ACCOUNTS 或 ohos.permission.DISTRIBUTED_DATASYNC

返回值:

類型

說(shuō)明

Promise<DistributedInfo>

Promise對(duì)象,返回分布式帳號(hào)信息對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

12300001

System service exception.

示例:

  1. const accountAbility = account_distributedAccount.getDistributedAccountAbility();
  2. try {
  3. accountAbility.getOsAccountDistributedInfo().then((data) => {
  4. console.log('Query account info name: ' + data.name);
  5. console.log('Query account info id: ' + data.id);
  6. }).catch((err) => {
  7. console.log("getOsAccountDistributedInfo err: " + JSON.stringify(err));
  8. });
  9. } catch (e) {
  10. console.log("getOsAccountDistributedInfo exception: " + JSON.stringify(e));
  11. }

queryOsAccountDistributedInfo(deprecated)

queryOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void

獲取分布式帳號(hào)信息。使用callback異步回調(diào)。

說(shuō)明

從 API version 7開始支持,從API version 9開始廢棄。建議使用getOsAccountDistributedInfo。

系統(tǒng)能力: SystemCapability.Account.OsAccount

需要權(quán)限: ohos.permission.MANAGE_LOCAL_ACCOUNTS 或 ohos.permission.DISTRIBUTED_DATASYNC

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

callback

AsyncCallback<DistributedInfo>

回調(diào)函數(shù)。當(dāng)獲取分布式帳號(hào)信息成功,err為undefined,data為獲取到的分布式帳號(hào)信息對(duì)象;否則為錯(cuò)誤對(duì)象。

示例:

  1. const accountAbility = account_distributedAccount.getDistributedAccountAbility();
  2. accountAbility.queryOsAccountDistributedInfo((err, data) => {
  3. console.log("queryOsAccountDistributedInfo err: " + JSON.stringify(err));
  4. console.log('Query account info name: ' + data.name);
  5. console.log('Query account info id: ' + data.id);
  6. });

queryOsAccountDistributedInfo(deprecated)

queryOsAccountDistributedInfo(): Promise<DistributedInfo>

獲取分布式帳號(hào)信息。使用Promise異步回調(diào)。

說(shuō)明

從 API version 7開始支持,從API version 9開始廢棄。建議使用getOsAccountDistributedInfo。

系統(tǒng)能力: SystemCapability.Account.OsAccount

需要權(quán)限: ohos.permission.MANAGE_LOCAL_ACCOUNTS 或 ohos.permission.DISTRIBUTED_DATASYNC

返回值:

類型

說(shuō)明

Promise<DistributedInfo>

Promise對(duì)象,返回分布式帳號(hào)信息對(duì)象。

示例:

  1. const accountAbility = account_distributedAccount.getDistributedAccountAbility();
  2. accountAbility.queryOsAccountDistributedInfo().then((data) => {
  3. console.log('Query account info name: ' + data.name);
  4. console.log('Query account info id: ' + data.id);
  5. }).catch((err) => {
  6. console.log("queryOsAccountDistributedInfoerr: " + JSON.stringify(err));
  7. });

setOsAccountDistributedInfo9+

setOsAccountDistributedInfo(accountInfo: DistributedInfo, callback: AsyncCallback<void>): void

更新分布式帳號(hào)信息。使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.Account.OsAccount

需要權(quán)限: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

accountInfo

DistributedInfo

分布式帳號(hào)信息。

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)更新分布式帳號(hào)信息成功時(shí),err為undefined,否則為錯(cuò)誤對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

12300001

System service exception.

12300002

Invalid accountInfo.

12300003

Account not found.

示例:

  1. const accountAbility = account_distributedAccount.getDistributedAccountAbility();
  2. let accountInfo = {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
  3. try {
  4. accountAbility.setOsAccountDistributedInfo(accountInfo, (err) => {
  5. console.log("setOsAccountDistributedInfo err: " + JSON.stringify(err));
  6. });
  7. } catch (e) {
  8. console.log("setOsAccountDistributedInfo exception: " + JSON.stringify(e));
  9. }

setOsAccountDistributedInfo9+

setOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void>

更新分布式帳號(hào)信息。使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.Account.OsAccount

需要權(quán)限: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

accountInfo

DistributedInfo

分布式帳戶信息。

返回值:

類型

說(shuō)明

Promise<void>

Promise對(duì)象,無(wú)返回結(jié)果的Promise對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

12300001

System service exception.

12300002

invalid accountInfo.

示例:

  1. const accountAbility = account_distributedAccount.getDistributedAccountAbility();
  2. let accountInfo = {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
  3. try {
  4. accountAbility.setOsAccountDistributedInfo(accountInfo).then(() => {
  5. console.log('setOsAccountDistributedInfo Success');
  6. }).catch((err) => {
  7. console.log("setOsAccountDistributedInfo err: " + JSON.stringify(err));
  8. });
  9. } catch (e) {
  10. console.log("setOsAccountDistributedInfo exception: " + JSON.stringify(e));
  11. }

updateOsAccountDistributedInfo(deprecated)

updateOsAccountDistributedInfo(accountInfo: DistributedInfo, callback: AsyncCallback<void>): void

更新分布式帳號(hào)信息。使用callback異步回調(diào)。

說(shuō)明

從 API version 7開始支持,從API version 9開始廢棄。建議使用setOsAccountDistributedInfo

系統(tǒng)能力: SystemCapability.Account.OsAccount

需要權(quán)限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

accountInfo

DistributedInfo

分布式帳號(hào)信息。

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)更新分布式帳號(hào)信息成功時(shí),err為undefined,否則為錯(cuò)誤對(duì)象。

示例:

  1. const accountAbility = account_distributedAccount.getDistributedAccountAbility();
  2. let accountInfo = {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
  3. accountAbility.updateOsAccountDistributedInfo(accountInfo, (err) => {
  4. console.log("queryOsAccountDistributedInfo err: " + JSON.stringify(err));
  5. });

updateOsAccountDistributedInfo(deprecated)

updateOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void>

更新分布式帳號(hào)信息。使用Promise異步回調(diào)。

說(shuō)明

從 API version 7開始支持,從API version 9開始廢棄。建議使用setOsAccountDistributedInfo。

系統(tǒng)能力: SystemCapability.Account.OsAccount

需要權(quán)限: ohos.permission.MANAGE_LOCAL_ACCOUNTS

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

accountInfo

DistributedInfo

分布式帳戶信息。

返回值:

類型

說(shuō)明

Promise<void>

Promise對(duì)象,無(wú)返回結(jié)果的Promise對(duì)象。

示例:

  1. const accountAbility = account_distributedAccount.getDistributedAccountAbility();
  2. let accountInfo = {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
  3. accountAbility.updateOsAccountDistributedInfo(accountInfo).then(() => {
  4. console.log('updateOsAccountDistributedInfo Success');
  5. }).catch((err) => {
  6. console.log("updateOsAccountDistributedInfo err: " + JSON.stringify(err));
  7. });

DistributedInfo

提供操作系統(tǒng)帳戶的分布式信息。

系統(tǒng)能力: SystemCapability.Account.OsAccount

名稱

類型

必填

說(shuō)明

name

string

分布式帳號(hào)名稱,非空字符串。

id

string

分布式帳號(hào)UID,非空字符串。

event

string

分布式帳號(hào)登錄狀態(tài),包括登錄、登出、Token失效和注銷,分別對(duì)應(yīng)以下字符串:

- Ohos.account.event.LOGIN

- Ohos.account.event.LOGOUT

- Ohos.account.event.TOKEN_INVALID

- Ohos.account.event.LOGOFF

nickname9+

string

分布式帳號(hào)的昵稱,非空字符串。

avatar9+

string

分布式帳號(hào)的頭像,非空字符串。

scalableData

object

分布式帳號(hào)擴(kuò)展信息,根據(jù)業(yè)務(wù)所需,以k-v形式傳遞定制化信息。

說(shuō)明:該參數(shù)是預(yù)留的可選項(xiàng),目前查詢和更新的方法實(shí)現(xiàn)中未使用。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)