• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 account_distributedAccount from '@ohos.account.distributedAccount';
13   ```
14
153. 获取分布式帐号的单实例对象。
16
17   ```ts
18   const distributedAccountAbility = account_distributedAccount.getDistributedAccountAbility();
19   ```
20
21## 在当前系统帐号上登录绑定分布式帐号
22
23具体开发实例如下:
24
251. 定义待登录的分布式帐号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGIN"。
26
27   ```ts
28   let distributedInfo: account_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: account_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.LOGIN"。
60
61   ```ts
62   let localId: number = 100;
63   let distributedInfo: account_distributedAccount.DistributedInfo = {
64       name: 'ZhangSan',
65       id: '12345',
66       event: 'Ohos.account.event.LOGIN',
67   };
68   ```
69
702. 调用setOsAccountDistributedInfoByLocalId接口,将指定分布式帐号与当前系统帐号绑定。
71
72   ```ts
73   distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => {
74       console.log('setOsAccountDistributedInfoByLocalId successfully');
75   }).catch((err: BusinessError) => {
76       console.log('setOsAccountDistributedInfoByLocalId exception: '  + JSON.stringify(err));
77   });
78   ```
79
803. 在帐号绑定之后,可以调用[getOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account-sys.md#setosaccountdistributedinfobylocalid10)接口查看分布式帐号的登录信息。
81
82   ```ts
83   distributedAccountAbility.getOsAccountDistributedInfoByLocalId(localId).then((data: account_distributedAccount.DistributedInfo) => {
84       console.log('distributed information: ' + JSON.stringify(data));
85   }).catch((err: BusinessError) => {
86       console.log('getOsAccountDistributedInfoByLocalId exception: '  + JSON.stringify(err));
87   });
88   ```
89
90## 在当前系统帐号上登出解绑分布式帐号
91
92具体开发实例如下:
93
941. 定义待登出的分布式帐号信息。其中,登录场景下需将event指定为"Ohos.account.event.LOGOUT"。
95
96   ```ts
97   let distributedInfo: account_distributedAccount.DistributedInfo = {
98       name: 'ZhangSan',
99       id: '12345',
100       event: 'Ohos.account.event.LOGOUT',
101   };
102   ```
103
1042. 调用[setOsAccountDistributedInfo](../../reference/apis-basic-services-kit/js-apis-distributed-account.md#setosaccountdistributedinfo9)接口,将指定的分布式帐号与当前系统帐号解绑。
105
106   ```ts
107   distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => {
108       console.log('setOsAccountDistributedInfo successfully');
109   }).catch((err: BusinessError) => {
110       console.log('setOsAccountDistributedInfo 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: account_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