1# 管理域帐号(仅对系统应用开放) 2 3用户可以在系统中添加域帐号,后续可以域帐号身份登录、使用系统。 4 5## 开发准备 6 71. 申请权限,申请流程请参考:[申请应用权限](../../security/AccessToken/determine-application-mode.md#system_basic等级的应用申请权限)。 8 - ohos.permission.MANAGE_LOCAL_ACCOUNTS 9 - ohos.permission.GET_DOMAIN_ACCOUNTS 10 112. 导入系统帐号模块。 12 13 ```ts 14 import account_osAccount from '@ohos.account.osAccount'; 15 import { AsyncCallback, BusinessError } from '@ohos.base'; 16 ``` 17 183. 获取获取系统帐号管理对象。 19 20 ```ts 21 let osAccountMgr = account_osAccount.getAccountManager(); 22 ``` 23 24## 判断指定域帐号是否存在 25 26在添加域帐号之前,应该先判断域帐号是否存在。开发者可以使用[hasAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#hasaccount10)接口进行判断。 27 28具体开发实例如下: 29 301. 定义待判断的域帐号信息。 31 32 ```ts 33 let domainAccountInfo: account_osAccount.DomainAccountInfo = { 34 accountName: 'testAccountName', 35 domain: 'testDomain' 36 } 37 ``` 38 392. 调用[hasAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#hasaccount10)接口。 40 41 ```ts 42 let isAccountExisted: boolean = await account_osAccount.DomainAccountManager.hasAccount(domainAccountInfo); 43 ``` 44 45## 添加域帐号 46 47用户在设置中添加其他域帐号,允许其他域帐号用户使用同一设备。开发者可以使用[createOsAccountForDomain](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#createosaccountfordomain)完成此操作。 48 49具体开发实例如下: 50 511. 定义域帐号信息,指定域名、帐号名、帐号标识(可选)。 52 53 ```ts 54 let domainInfo: account_osAccount.DomainAccountInfo = { 55 domain: 'testDomain', 56 accountName: 'testAccountName' 57 }; 58 ``` 59 602. 指定类型和域帐号信息,调用[createOsAccountForDomain](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#createosaccountfordomain)接口在设备上创建一个域帐号。 61 62 ```ts 63 try { 64 accountMgr.createOsAccountForDomain(account_osAccount.OsAccountType.NORMAL, domainInfo, 65 (err: BusinessError, osAccountInfo: account_osAccount.OsAccountInfo)=>{ 66 console.log('createOsAccountForDomain err:' + JSON.stringify(err)); 67 console.log('createOsAccountForDomain osAccountInfo:' + JSON.stringify(osAccountInfo)); 68 }); 69 } catch (e) { 70 console.log('createOsAccountForDomain exception: ' + JSON.stringify(e)); 71 } 72 ``` 73 74## 删除域帐号 75 76用户可以删除不再使用的域帐号。由于域帐号和系统帐号是一一绑定关系,开发者可以使用[removeOsAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#removeosaccount)接口删除与目标域帐号绑定的系统帐号,进而实现删除域帐号。 77 78具体开发实例如下: 79 801. 调用[getOsAccountLocalIdForDomain](../../reference/apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalidfordomain9)方法,根据域帐号信息获取系统帐号ID。 81 82 ```ts 83 let domainInfo: account_osAccount.DomainAccountInfo = { 84 domain: 'testDomain', 85 accountName: 'testAccountName' 86 }; 87 88 try { 89 let localId: number = accountMgr.getOsAccountLocalIdForDomain(domainInfo); 90 } catch (err) { 91 console.log('getOsAccountLocalIdForDomain exception: ' + JSON.stringify(err)); 92 } 93 ``` 94 952. 调用[removeOsAccount](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#removeosaccount)方法删除域帐号。 96 97 ```ts 98 try { 99 accountMgr.removeOsAccount(osAccountInfo.localId, (err: BusinessError)=>{ 100 if (err) { 101 console.log('removeOsAccount failed, error: ' + JSON.stringify(err)); 102 } else { 103 console.log('removeOsAccount successfully'); 104 } 105 }); 106 } catch (err) { 107 console.log('removeOsAccount exception: ' + JSON.stringify(err)); 108 } 109 ``` 110 111## 查询域帐号信息 112 113用户通过身份认证后,可以查询自己或他人的域帐号信息。开发者可以使用[getAccountInfo](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#getaccountinfo10)接口完成此操作。 114 115具体开发实例如下: 116 1171. 定义查询选项,可以指定需要查询的域名和帐号名。选项的类型为[GetDomainAccountInfoOptions](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#getdomainaccountinfooptions10)。 118 119 ```ts 120 let options: account_osAccount.GetDomainAccountInfoOptions = { 121 domain: 'testDomain', 122 accountName: 'testAccountName' 123 } 124 ``` 125 1262. 调用[getAccountInfo](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#getaccountinfo10)接口查询域帐号信息。 127 128 ```ts 129 try { 130 account_osAccount.DomainAccountManager.getAccountInfo(domainAccountInfo, 131 (err: BusinessError, result: account_osAccount.DomainAccountInfo) => { 132 if (err) { 133 console.log('call getAccountInfo failed, error: ' + JSON.stringify(err)); 134 } else { 135 console.log('getAccountInfo result: ' + result); 136 } 137 }); 138 } catch (err) { 139 console.log('getAccountInfo exception = ' + JSON.stringify(err)); 140 } 141 ``` 142