鸿蒙开发管理:【@ohos.account.distributedAccount (分布式帐号管理)】

分布式帐号管理

本模块提供管理分布式帐号的一些基础功能,主要包括查询和更新帐号登录状态。

说明: 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。开发前请熟悉鸿蒙开发指导文档gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md点击或者复制转到。

导入模块

复制代码
import account_distributedAccount from '@ohos.account.distributedAccount';

account_distributedAccount.getDistributedAccountAbility

getDistributedAccountAbility(): DistributedAccountAbility

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

系统能力: SystemCapability.Account.OsAccount

  • 返回值:

    | 类型 | 说明 |

    | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- |

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

  • 示例:

    复制代码
    const accountAbility = account_distributedAccount.getDistributedAccountAbility();

DistributedAccountAbility

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

queryOsAccountDistributedInfo

queryOsAccountDistributedInfo(callback: AsyncCallback): void

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

系统能力: SystemCapability.Account.OsAccount

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

  • 参数:

    | 参数名 | 类型 | 必填 | 说明 |

    | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -- | ------------- |

    | 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

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

系统能力: SystemCapability.Account.OsAccount

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

  • 返回值:

    | 类型 | 说明 |

    | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------- |

    | 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

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

系统能力: SystemCapability.Account.OsAccount

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

  • 参数:

    | 参数名 | 类型 | 必填 | 说明 |

    | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -- | ------------- |

    | accountInfo | [DistributedInfo]| 是 | 分布式帐号信息。 |

    | callback | AsyncCallback | 是 | 更新分布式帐号信息的回调。 |

  • 示例:

    复制代码
    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

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

系统能力: SystemCapability.Account.OsAccount

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

  • 参数:

    | 参数名 | 类型 | 必填 | 说明 |

    | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -- | -------- |

    | accountInfo | [DistributedInfo] | 是 | 分布式帐户信息。 |

  • 返回值:

    | 类型 | 说明 |

    | ------------- | --------------------- |

    | Promise | 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

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

系统能力: SystemCapability.Account.OsAccount

参数名 类型 必填 说明
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形式传递定制化信息。 说明:该参数是预留的可选项,目前查询和更新的方法实现中未使用。
相关推荐
AlbertZein2 小时前
HarmonyOS下饭菜时间 -- @Monitor
harmonyos
AlbertZein2 小时前
HarmonyOS一杯冰美式的时间 -- UIUtils基础功能
harmonyos
行者964 小时前
Flutter与OpenHarmony跨平台分享组件深度实践
flutter·harmonyos·鸿蒙
行者964 小时前
Flutter跨平台开发在OpenHarmony上的评分组件实现与优化
开发语言·flutter·harmonyos·鸿蒙
码界奇点5 小时前
基于Spring Cloud微服务架构的电商系统设计与实现
spring cloud·微服务·架构·毕业设计·鸿蒙系统·源代码管理
90后的晨仔6 小时前
HarmonyOS 多模块项目中的公共库治理与最佳实践
harmonyos
kirk_wang8 小时前
Flutter 三方库在 OpenHarmony 上的适配之路:以 geolocator 为例
flutter·移动开发·跨平台·arkts·鸿蒙
kirk_wang8 小时前
Flutter艺术探索-Flutter动画基础:Implicit Animations入门
flutter·移动开发·flutter教程·移动开发教程
lili-felicity10 小时前
React Native 鸿蒙跨平台开发:LayoutAnimation 实现鸿蒙端按钮点击的缩放反馈动画
react native·react.js·harmonyos
Captaincc10 小时前
如何在一天内重启你的人生
程序员