harmony 鸿蒙管理分布式账号(仅对系统应用开放)
管理分布式账号(仅对系统应用开放)
OEM厂商可以通过分布式账号SDK将自有账号与本地系统账号建立关联关系。
开发准备
申请权限: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接口,将当前系统账号与指定分布式账号绑定到一起。
distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => {
console.log('setOsAccountDistributedInfo successfully');
}).catch((err: BusinessError) => {
console.error('setOsAccountDistributedInfo exception: ' + JSON.stringify(err));
});
- 在账号绑定之后,可以调用getOsAccountDistributedInfo接口查看分布式账号的登录信息。
distributedAccountAbility.getOsAccountDistributedInfo().then((data: distributedAccount.DistributedInfo) => {
console.log('distributed information: ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
console.error('getOsAccountDistributedInfo exception: ' + JSON.stringify(err));
});
在当前系统账号上登出解绑分布式账号
具体开发实例如下:
- 定义待登出的分布式账号信息。其中,登录场景下需将event指定为”Ohos.account.event.LOGOUT”。
let distributedInfo: distributedAccount.DistributedInfo = {
name: 'ZhangSan',
id: '12345',
event: 'Ohos.account.event.LOGOUT',
};
- 调用setOsAccountDistributedInfo接口,将指定的分布式账号与当前系统账号解绑。
distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => {
console.log('setOsAccountDistributedInfo successfully');
}).catch((err: BusinessError) => {
console.error('setOsAccountDistributedInfo exception: ' + JSON.stringify(err));
});
在指定的系统账号上登录绑定分布式账号
具体开发实例如下:
- 确定目标系统账号,并定义待登录的分布式账号信息。其中,登录场景下需将event指定为”Ohos.account.event.LOGIN”。
let localId: number = 100;
let distributedInfo: distributedAccount.DistributedInfo = {
name: 'ZhangSan',
id: '12345',
event: 'Ohos.account.event.LOGIN',
};
- 调用setOsAccountDistributedInfoByLocalId接口,将指定分布式账号与当前系统账号绑定。
distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => {
console.log('setOsAccountDistributedInfoByLocalId successfully');
}).catch((err: BusinessError) => {
console.error('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err));
});
- 在账号绑定之后,可以调用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));
});
在指定系统账号上登出解绑分布式账号
具体开发实例如下:
- 确定目标系统账号,并定义待登出的分布式账号信息。其中,登录场景下需将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.log('setOsAccountDistributedInfoByLocalId successfully');
}).catch((err: BusinessError) => {
console.error('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err));
});
你可能感兴趣的鸿蒙文章
0
赞
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦