harmony 鸿蒙管理分布式账号(仅对系统应用开放)

  • 2025-06-12
  • 浏览 (5)

管理分布式账号(仅对系统应用开放)

OEM厂商可以通过分布式账号SDK将自有账号与本地系统账号建立关联关系。

开发准备

  1. 申请权限:ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS。申请流程请参考:申请应用权限

  2. 导入分布式账号模块。

   import { distributedAccount, BusinessError } from '@kit.BasicServicesKit';
  1. 获取分布式账号的单实例对象。
   const distributedAccountAbility = distributedAccount.getDistributedAccountAbility();

在当前系统账号上登录绑定分布式账号

具体开发实例如下:

  1. 定义待登录的分布式账号信息。其中,登录场景下需将event指定为”Ohos.account.event.LOGIN”。
   let distributedInfo: distributedAccount.DistributedInfo = {
       name: 'ZhangSan',
       id: '12345',
       event: 'Ohos.account.event.LOGIN',
   };
  1. 调用setOsAccountDistributedInfo接口,将当前系统账号与指定分布式账号绑定到一起。
   distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => {
       console.log('setOsAccountDistributedInfo successfully');
   }).catch((err: BusinessError) => {
       console.error('setOsAccountDistributedInfo exception: '  + JSON.stringify(err));
   });
  1. 在账号绑定之后,可以调用getOsAccountDistributedInfo接口查看分布式账号的登录信息。
   distributedAccountAbility.getOsAccountDistributedInfo().then((data: distributedAccount.DistributedInfo) => {
       console.log('distributed information: ' + JSON.stringify(data));
   }).catch((err: BusinessError) => {
       console.error('getOsAccountDistributedInfo exception: '  + JSON.stringify(err));
   });

在当前系统账号上登出解绑分布式账号

具体开发实例如下:

  1. 定义待登出的分布式账号信息。其中,登录场景下需将event指定为”Ohos.account.event.LOGOUT”。
   let distributedInfo: distributedAccount.DistributedInfo = {
       name: 'ZhangSan',
       id: '12345',
       event: 'Ohos.account.event.LOGOUT',
   };
  1. 调用setOsAccountDistributedInfo接口,将指定的分布式账号与当前系统账号解绑。
   distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => {
       console.log('setOsAccountDistributedInfo successfully');
   }).catch((err: BusinessError) => {
       console.error('setOsAccountDistributedInfo exception: '  + JSON.stringify(err));
   });

在指定的系统账号上登录绑定分布式账号

具体开发实例如下:

  1. 确定目标系统账号,并定义待登录的分布式账号信息。其中,登录场景下需将event指定为”Ohos.account.event.LOGIN”。
   let localId: number = 100;
   let distributedInfo: distributedAccount.DistributedInfo = {
       name: 'ZhangSan',
       id: '12345',
       event: 'Ohos.account.event.LOGIN',
   };
  1. 调用setOsAccountDistributedInfoByLocalId接口,将指定分布式账号与当前系统账号绑定。
   distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => {
       console.log('setOsAccountDistributedInfoByLocalId successfully');
   }).catch((err: BusinessError) => {
       console.error('setOsAccountDistributedInfoByLocalId exception: '  + JSON.stringify(err));
   });
  1. 在账号绑定之后,可以调用getOsAccountDistributedInfoByLocalId接口查看分布式账号的登录信息。
   distributedAccountAbility.getOsAccountDistributedInfoByLocalId(localId).then((data: distributedAccount.DistributedInfo) => {
       console.log('distributed information: ' + JSON.stringify(data));
   }).catch((err: BusinessError) => {
       console.error('getOsAccountDistributedInfoByLocalId exception: '  + JSON.stringify(err));
   });

在指定系统账号上登出解绑分布式账号

具体开发实例如下:

  1. 确定目标系统账号,并定义待登出的分布式账号信息。其中,登录场景下需将event指定为”Ohos.account.event.LOGOUT”。
   let localId: number = 100;
   let distributedInfo: distributedAccount.DistributedInfo = {
       name: 'ZhangSan',
       id: '12345',
       event: 'Ohos.account.event.LOGOUT',
   };
  1. 调用setOsAccountDistributedInfoByLocalId接口,将指定的分布式账号与目标系统账号解绑。
   distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => {
       console.log('setOsAccountDistributedInfoByLocalId successfully');
   }).catch((err: BusinessError) => {
       console.error('setOsAccountDistributedInfoByLocalId exception: '  + JSON.stringify(err));
   });

你可能感兴趣的鸿蒙文章

harmony 鸿蒙账号管理

harmony 鸿蒙账号管理概述

harmony 鸿蒙认证域账号(仅对系统应用开放)

harmony 鸿蒙使用约束管控系统账号(仅对系统应用开放)

harmony 鸿蒙管理应用账号

harmony 鸿蒙管理域账号(仅对系统应用开放)

harmony 鸿蒙管理域账号插件(仅对系统应用开放)

harmony 鸿蒙管理系统账号凭据(仅对系统应用开放)

harmony 鸿蒙管理系统账号(仅对系统应用开放)

0  赞