管理分布式账号(仅对系统应用开放)
OEM厂商可以通过@ohos.account.distributedAccount将自有账号与本地系统账号建立关联关系。
开发准备
-
申请权限:ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS。申请流程请参考:申请应用权限。
-
导入分布式账号模块。
import { distributedAccount, BusinessError } from '@kit.BasicServicesKit'; -
获取分布式账号的单实例对象。
const distributedAccountAbility = distributedAccount.getDistributedAccountAbility();
在当前系统账号上登录绑定分布式账号
具体开发实例如下:
-
定义待登录的分布式账号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGIN"。
let distributedInfo: distributedAccount.DistributedInfo = { name: 'ZhangSan', id: '12345', event: 'Ohos.account.event.LOGIN', }; -
调用setOsAccountDistributedInfo接口,将当前系统账号与指定分布式账号绑定到一起。
await distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => { console.info('setOsAccountDistributedInfo successfully'); }).catch((err: BusinessError) => { console.error(`setOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`); // ··· }); -
在账号绑定之后,可以调用getOsAccountDistributedInfo接口查看分布式账号的登录信息。
distributedAccountAbility.getOsAccountDistributedInfo().then((data: distributedAccount.DistributedInfo) => { console.info('distributed information: ' + JSON.stringify(data)); // ··· }).catch((err: BusinessError) => { console.error(`getOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`); // ··· });
在当前系统账号上登出解绑分布式账号
具体开发实例如下:
-
定义待登出的分布式账号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGOUT"。
let distributedInfo: distributedAccount.DistributedInfo = { name: 'ZhangSan', id: '12345', event: 'Ohos.account.event.LOGOUT', }; -
调用setOsAccountDistributedInfo接口,将指定的分布式账号与当前系统账号解绑。
distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => { console.info('setOsAccountDistributedInfo successfully'); // ··· }).catch((err: BusinessError) => { console.error(`setOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`); // ··· });
在指定的系统账号上登录绑定分布式账号
具体开发实例如下:
-
确定目标系统账号,并定义待登录的分布式账号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGIN"。
let localId: number = 100; let distributedInfo: distributedAccount.DistributedInfo = { name: 'ZhangSan', id: '12345', event: 'Ohos.account.event.LOGIN', }; -
调用setOsAccountDistributedInfoByLocalId接口,将指定分布式账号与当前系统账号绑定。
await distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => { console.info('setOsAccountDistributedInfoByLocalId successfully'); }).catch((err: BusinessError) => { console.error(`setOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`); // ··· }); -
在账号绑定之后,可以调用getOsAccountDistributedInfoByLocalId接口查看分布式账号的登录信息。
distributedAccountAbility.getOsAccountDistributedInfoByLocalId(localId) .then((data: distributedAccount.DistributedInfo) => { console.info('distributed information: ' + JSON.stringify(data)); // ··· }).catch((err: BusinessError) => { console.error(`getOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`); // ··· });
在指定系统账号上登出解绑分布式账号
具体开发实例如下:
-
确定目标系统账号,并定义待登出的分布式账号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGOUT"。
let localId: number = 100; let distributedInfo: distributedAccount.DistributedInfo = { name: 'ZhangSan', id: '12345', event: 'Ohos.account.event.LOGOUT', }; -
调用setOsAccountDistributedInfoByLocalId接口,将指定的分布式账号与目标系统账号解绑。
distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => { console.info('setOsAccountDistributedInfoByLocalId successfully'); // ··· }).catch((err: BusinessError) => { console.error(`setOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`); // ··· });