1# 管理分布式账号(仅对系统应用开放) 2 3<!--Kit: Basic Services Kit--> 4<!--Subsystem: Account--> 5<!--Owner: @steven-q--> 6<!--Designer: @JiDong-CS1--> 7<!--Tester: @zhaimengchao--> 8<!--Adviser: @zengyawen--> 9 10OEM厂商可以通过[分布式账号SDK](../../reference/apis-basic-services-kit/js-apis-distributed-account.md)将自有账号与本地系统账号建立关联关系。 11 12## 开发准备 13 141. 申请权限:ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS。申请流程请参考:[申请应用权限](../../security/AccessToken/determine-application-mode.md#system_basic等级应用申请权限的方式)。 15 162. 导入分布式账号模块。 17 18 ```ts 19 import { distributedAccount, BusinessError } from '@kit.BasicServicesKit'; 20 ``` 21 223. 获取分布式账号的单实例对象。 23 24 ```ts 25 const distributedAccountAbility = distributedAccount.getDistributedAccountAbility(); 26 ``` 27 28## 在当前系统账号上登录绑定分布式账号 29 30具体开发实例如下: 31 321. 定义待登录的分布式账号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGIN"。 33 34 ```ts 35 let distributedInfo: distributedAccount.DistributedInfo = { 36 name: 'ZhangSan', 37 id: '12345', 38 event: 'Ohos.account.event.LOGIN', 39 }; 40 ``` 41 422. 调用[setOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#setosaccountdistributedinfo9)接口,将当前系统账号与指定分布式账号绑定到一起。 43 44 ```ts 45 distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => { 46 console.info('setOsAccountDistributedInfo successfully'); 47 }).catch((err: BusinessError) => { 48 console.error(`setOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`); 49 }); 50 ``` 51 523. 在账号绑定之后,可以调用[getOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#getosaccountdistributedinfo9)接口查看分布式账号的登录信息。 53 54 ```ts 55 distributedAccountAbility.getOsAccountDistributedInfo().then((data: distributedAccount.DistributedInfo) => { 56 console.info('distributed information: ' + JSON.stringify(data)); 57 }).catch((err: BusinessError) => { 58 console.error(`getOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`); 59 }); 60 ``` 61 62## 在当前系统账号上登出解绑分布式账号 63 64具体开发实例如下: 65 661. 定义待登出的分布式账号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGOUT"。 67 68 ```ts 69 let distributedInfo: distributedAccount.DistributedInfo = { 70 name: 'ZhangSan', 71 id: '12345', 72 event: 'Ohos.account.event.LOGOUT', 73 }; 74 ``` 75 762. 调用[setOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#setosaccountdistributedinfo9)接口,将指定的分布式账号与当前系统账号解绑。 77 78 ```ts 79 distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => { 80 console.info('setOsAccountDistributedInfo successfully'); 81 }).catch((err: BusinessError) => { 82 console.error(`setOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`); 83 }); 84 ``` 85 86## 在指定的系统账号上登录绑定分布式账号 87 88具体开发实例如下: 89 901. 确定目标系统账号,并定义待登录的分布式账号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGIN"。 91 92 ```ts 93 let localId: number = 100; 94 let distributedInfo: distributedAccount.DistributedInfo = { 95 name: 'ZhangSan', 96 id: '12345', 97 event: 'Ohos.account.event.LOGIN', 98 }; 99 ``` 100 1012. 调用[setOsAccountDistributedInfoByLocalId](../../reference/apis-basic-services-kit/js-apis-distributed-account-sys.md#setosaccountdistributedinfobylocalid10)接口,将指定分布式账号与当前系统账号绑定。 102 103 ```ts 104 distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => { 105 console.info('setOsAccountDistributedInfoByLocalId successfully'); 106 }).catch((err: BusinessError) => { 107 console.error(`setOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`); 108 }); 109 ``` 110 1113. 在账号绑定之后,可以调用[getOsAccountDistributedInfoByLocalId](../../reference/apis-basic-services-kit/js-apis-distributed-account-sys.md#getosaccountdistributedinfobylocalid10)接口查看分布式账号的登录信息。 112 113 ```ts 114 distributedAccountAbility.getOsAccountDistributedInfoByLocalId(localId).then((data: distributedAccount.DistributedInfo) => { 115 console.info('distributed information: ' + JSON.stringify(data)); 116 }).catch((err: BusinessError) => { 117 console.error(`getOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`); 118 }); 119 ``` 120 121## 在指定系统账号上登出解绑分布式账号 122 123具体开发实例如下: 124 1251. 确定目标系统账号,并定义待登出的分布式账号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGOUT"。 126 127 ```ts 128 let localId: number = 100; 129 let distributedInfo: distributedAccount.DistributedInfo = { 130 name: 'ZhangSan', 131 id: '12345', 132 event: 'Ohos.account.event.LOGOUT', 133 }; 134 ``` 135 1362. 调用[setOsAccountDistributedInfoByLocalId](../../reference/apis-basic-services-kit/js-apis-distributed-account-sys.md#setosaccountdistributedinfobylocalid10)接口,将指定的分布式账号与目标系统账号解绑。 137 138 ```ts 139 distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => { 140 console.info('setOsAccountDistributedInfoByLocalId successfully'); 141 }).catch((err: BusinessError) => { 142 console.error(`setOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`); 143 }); 144 ``` 145