1 Star 0 Fork 5.2K

OpenHarmony-build / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
js-apis-distributed-account.md 17.35 KB
一键复制 编辑 原始数据 按行查看 历史
zengyawen 提交于 2021-11-10 20:02 . add arkui

分布式帐号管理

说明: 从API Version 7 开始支持。

导入模块

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

account_distributedAccount.getDistributedAccountAbility

getDistributedAccountAbility(): DistributedAccountAbility

获取分布式帐号单实例对象。

  • 返回值:

    类型

    说明

    DistributedAccountAbility

    返回一个实例,实例提供查询和更新分布式帐号登录状态方法。

  • 示例:

    const accountAbility = account_distributedAccount.getDistributedAccountAbility();

DistributedAccountAbility

提供查询和更新分布式帐号登录状态方法(需要先获取分布式帐号的单实例对象)。

queryOsAccountDistributedInfo

queryOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void

获取分布式帐号信息,使用callback回调异步返回结果。

需要权限:ohos.permission.MANAGE_LOCAL_ACCOUNTS,该权限仅供系统应用使用。

  • 参数:

    参数名

    类型

    必填

    说明

    callback

    AsyncCallback<DistributedInfo>

    获取分布式帐号信息的回调。

  • 示例:

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

queryOsAccountDistributedInfo

queryOsAccountDistributedInfo(): Promise<DistributedInfo>

获取分布式帐号信息,使用Promise方式异步返回结果。

需要权限:ohos.permission.MANAGE_LOCAL_ACCOUNTS,该权限仅供系统应用使用。

  • 返回值:

    类型

    说明

    Promise<DistributedInfo>

    Promise实例,用于获取异步返回结果。

  • 示例:

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

updateOsAccountDistributedInfo

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

更新分布式帐号信息,使用callback回调异步返回结果。

需要权限:ohos.permission.MANAGE_LOCAL_ACCOUNTS,该权限仅供系统应用使用。

  • 参数:

    参数名

    类型

    必填

    说明

    accountInfo

    DistributedInfo

    分布式帐号信息。

    callback

    AsyncCallback<void>

    更新分布式帐号信息的回调。

  • 示例:

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

updateOsAccountDistributedInfo

updateOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void>

更新分布式帐号信息,使用Promise方式异步返回结果。

需要权限:ohos.permission.MANAGE_LOCAL_ACCOUNTS,该权限仅供系统应用使用。

  • 参数:

    参数名

    类型

    必填

    说明

    accountInfo

    DistributedInfo

    分布式帐户信息。

  • 返回值:

    类型

    说明

    Promise<void>

    Promise实例,用于获取异步返回结果。

  • 示例:

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

DistributedInfo

提供操作系统帐户的分布式信息。

参数名

类型

必填

说明

name

string

分布式帐号名称,非空字符串。

id

string

分布式帐号UID,非空字符串。

event

string

分布式帐号登录状态,包括登录、登出、Token失效和注销,分别对应以下字符串:

  • Ohos.account.event.LOGIN
  • Ohos.account.event.LOGOUT
  • Ohos.account.event.TOKEN_INVALID
  • Ohos.account.event.LOGOFF

scalableData

object

分布式帐号扩展信息,根据业务所需,以k-v形式传递定制化信息。

说明:该参数是预留的可选项,目前查询和更新的方法实现中未使用。

1
https://gitee.com/openharmony-build/docs.git
git@gitee.com:openharmony-build/docs.git
openharmony-build
docs
docs
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891