1# 管理分布式账号(仅对系统应用开放)
2
3OEM厂商可以通过[分布式账号SDK](../../reference/apis-basic-services-kit/js-apis-distributed-account.md)将自有账号与本地系统账号建立关联关系。
4
5## 开发准备
6
71. 申请权限:ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS。申请流程请参考:[申请应用权限](../../security/AccessToken/determine-application-mode.md#system_basic等级应用申请权限的方式)。
8
92. 导入分布式账号模块。
10
11   ```ts
12   import { distributedAccount, BusinessError } from '@kit.BasicServicesKit';
13   ```
14
153. 获取分布式账号的单实例对象。
16
17   ```ts
18   const distributedAccountAbility = distributedAccount.getDistributedAccountAbility();
19   ```
20
21## 在当前系统账号上登录绑定分布式账号
22
23具体开发实例如下:
24
251. 定义待登录的分布式账号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGIN"。
26
27   ```ts
28   let distributedInfo: distributedAccount.DistributedInfo = {
29       name: 'ZhangSan',
30       id: '12345',
31       event: 'Ohos.account.event.LOGIN',
32   };
33   ```
34
352. 调用[setOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#setosaccountdistributedinfo9)接口,将当前系统账号与指定分布式账号绑定到一起。
36
37   ```ts
38   distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => {
39       console.log('setOsAccountDistributedInfo successfully');
40   }).catch((err: BusinessError) => {
41       console.log('setOsAccountDistributedInfo exception: '  + JSON.stringify(err));
42   });
43   ```
44
453. 在账号绑定之后,可以调用[getOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#getosaccountdistributedinfo9)接口查看分布式账号的登录信息。
46
47   ```ts
48   distributedAccountAbility.getOsAccountDistributedInfo().then((data: distributedAccount.DistributedInfo) => {
49       console.log('distributed information: ' + JSON.stringify(data));
50   }).catch((err: BusinessError) => {
51       console.log('getOsAccountDistributedInfo exception: '  + JSON.stringify(err));
52   });
53   ```
54
55## 在当前系统账号上登出解绑分布式账号
56
57具体开发实例如下:
58
591. 定义待登出的分布式账号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGOUT"。
60
61   ```ts
62   let distributedInfo: distributedAccount.DistributedInfo = {
63       name: 'ZhangSan',
64       id: '12345',
65       event: 'Ohos.account.event.LOGOUT',
66   };
67   ```
68
692. 调用[setOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#setosaccountdistributedinfo9)接口,将指定的分布式账号与当前系统账号解绑。
70
71   ```ts
72   distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => {
73       console.log('setOsAccountDistributedInfo successfully');
74   }).catch((err: BusinessError) => {
75       console.log('setOsAccountDistributedInfo exception: '  + JSON.stringify(err));
76   });
77   ```
78
79## 在指定的系统账号上登录绑定分布式账号
80
81具体开发实例如下:
82
831. 确定目标系统账号,并定义待登录的分布式账号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGIN"。
84
85   ```ts
86   let localId: number = 100;
87   let distributedInfo: distributedAccount.DistributedInfo = {
88       name: 'ZhangSan',
89       id: '12345',
90       event: 'Ohos.account.event.LOGIN',
91   };
92   ```
93
942. 调用[setOsAccountDistributedInfoByLocalId](../../reference/apis-basic-services-kit/js-apis-distributed-account-sys.md#setosaccountdistributedinfobylocalid10)接口,将指定分布式账号与当前系统账号绑定。
95
96   ```ts
97   distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => {
98       console.log('setOsAccountDistributedInfoByLocalId successfully');
99   }).catch((err: BusinessError) => {
100       console.log('setOsAccountDistributedInfoByLocalId exception: '  + JSON.stringify(err));
101   });
102   ```
103
1043. 在账号绑定之后,可以调用[getOsAccountDistributedInfoByLocalId](../../reference/apis-basic-services-kit/js-apis-distributed-account-sys.md#getosaccountdistributedinfobylocalid10)接口查看分布式账号的登录信息。
105
106   ```ts
107   distributedAccountAbility.getOsAccountDistributedInfoByLocalId(localId).then((data: distributedAccount.DistributedInfo) => {
108       console.log('distributed information: ' + JSON.stringify(data));
109   }).catch((err: BusinessError) => {
110       console.log('getOsAccountDistributedInfoByLocalId exception: '  + JSON.stringify(err));
111   });
112   ```
113
114## 在指定系统账号上登出解绑分布式账号
115
116具体开发实例如下:
117
1181. 确定目标系统账号,并定义待登出的分布式账号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGOUT"。
119
120   ```ts
121   let localId: number = 100;
122   let distributedInfo: distributedAccount.DistributedInfo = {
123       name: 'ZhangSan',
124       id: '12345',
125       event: 'Ohos.account.event.LOGOUT',
126   };
127   ```
128
1292. 调用[setOsAccountDistributedInfoByLocalId](../../reference/apis-basic-services-kit/js-apis-distributed-account-sys.md#setosaccountdistributedinfobylocalid10)接口,将指定的分布式账号与目标系统账号解绑。
130
131   ```ts
132   distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => {
133       console.log('setOsAccountDistributedInfoByLocalId successfully');
134   }).catch((err: BusinessError) => {
135       console.log('setOsAccountDistributedInfoByLocalId exception: '  + JSON.stringify(err));
136   });
137   ```
138