1# 管理域账号插件(仅对系统应用开放) 2 3OEM厂商可以采用插件方式定制化域账号管理能力,系统提供了域账号插件注册和注销能能力。 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 { osAccount, AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 15 ``` 16 173. 获取系统账号管理对象。 18 19 ```ts 20 let accountMgr = osAccount.getAccountManager() 21 ``` 22 23## 注册插件 24 25域插件原型为[DomainPlugin](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#domainplugin9),域插件开发者需要继承并实现插件原型中定义的接口。开发者可以使用[registerPlugin](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#registerplugin9)接口完成插件注册操作。 26 27具体开发实例如下: 28 291. 定义插件。 30 31 ```ts 32 let plugin: osAccount.DomainPlugin = { 33 auth: (domainAccountInfo: osAccount.DomainAccountInfo, credential: Uint8Array, 34 callback: osAccount.IUserAuthCallback) => { 35 console.info("plugin auth domain" + domainAccountInfo.domain) 36 console.info("plugin auth accountName" + domainAccountInfo.accountName) 37 console.info("plugin auth accountId" + domainAccountInfo.accountId) 38 39 let result: osAccount.AuthResult = { 40 token: new Uint8Array([0]), 41 remainTimes: 5, 42 freezingTime: 0 43 }; 44 callback.onResult(0, result); 45 }, 46 authWithPopup: (domainAccountInfo: osAccount.DomainAccountInfo, 47 callback: osAccount.IUserAuthCallback) => { 48 console.info("plugin authWithPopup domain" + domainAccountInfo.domain) 49 console.info("plugin authWithPopup accountName" + domainAccountInfo.accountName) 50 console.info("plugin authWithPopup accountId" + domainAccountInfo.accountId) 51 52 let result: osAccount.AuthResult = { 53 token: new Uint8Array([0]), 54 remainTimes: 5, 55 freezingTime: 0 56 }; 57 callback.onResult(0, result); 58 }, 59 authWithToken: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array, callback: osAccount.IUserAuthCallback) => { 60 console.info("plugin authWithToken domain" + domainAccountInfo.domain) 61 console.info("plugin authWithToken accountName" + domainAccountInfo.accountName) 62 console.info("plugin authWithToken accountId" + domainAccountInfo.accountId) 63 let result: osAccount.AuthResult = { 64 token: new Uint8Array([0]), 65 remainTimes: 5, 66 freezingTime: 0 67 }; 68 callback.onResult(0, result); 69 }, 70 getAccountInfo: (options: osAccount.GetDomainAccountInfoPluginOptions, 71 callback: AsyncCallback<osAccount.DomainAccountInfo>) => { 72 console.info("plugin getAccountInfo domain") 73 let domainAccountId = Date.now().toString() 74 let code: BusinessError = { 75 code: 0, 76 name: "mock_name", 77 message: "mock_message" 78 }; 79 let domainStr: string = ''; 80 if (options.domain != undefined) { 81 domainStr = options.domain 82 } 83 let accountInfo: osAccount.DomainAccountInfo = { 84 domain: domainStr, 85 accountName: options.accountName, 86 accountId: domainAccountId, 87 isAuthenticated: false 88 }; 89 callback(code, accountInfo); 90 }, 91 getAuthStatusInfo: (domainAccountInfo: osAccount.DomainAccountInfo, 92 callback: AsyncCallback<osAccount.AuthStatusInfo>) => { 93 94 console.info("plugin getAuthStatusInfo domain" + domainAccountInfo.domain) 95 console.info("plugin getAuthStatusInfo accountName" + domainAccountInfo.accountName) 96 console.info("plugin getAuthStatusInfo accountId" + domainAccountInfo.accountId) 97 98 let code: BusinessError = { 99 code: 0, 100 name: "mock_name", 101 message: "mock_message" 102 }; 103 let statusInfo: osAccount.AuthStatusInfo = { 104 remainTimes: 5, 105 freezingTime: 0 106 }; 107 callback(code, statusInfo); 108 }, 109 bindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, localId: number, 110 callback: AsyncCallback<void>) => { 111 console.info("plugin bindAccount domain" + domainAccountInfo.domain) 112 console.info("plugin bindAccount accountName" + domainAccountInfo.accountName) 113 console.info("plugin bindAccount accountId" + domainAccountInfo.accountId) 114 let code: BusinessError = { 115 code: 0, 116 name: "mock_name", 117 message: "mock_message" 118 }; 119 callback(code); 120 }, 121 unbindAccount: (domainAccountInfo: osAccount.DomainAccountInfo, callback: AsyncCallback<void>) => { 122 console.info("plugin unbindAccount domain" + domainAccountInfo.domain) 123 console.info("plugin unbindAccount accountName" + domainAccountInfo.accountName) 124 console.info("plugin unbindAccount accountId" + domainAccountInfo.accountId) 125 }, 126 isAccountTokenValid: (domainAccountInfo: osAccount.DomainAccountInfo, token: Uint8Array, 127 callback: AsyncCallback<boolean>) => { 128 console.info("plugin isAccountTokenValid domain" + domainAccountInfo.domain) 129 console.info("plugin isAccountTokenValid accountName" + domainAccountInfo.accountName) 130 console.info("plugin isAccountTokenValid accountId" + domainAccountInfo.accountId) 131 let code: BusinessError = { 132 code: 0, 133 name: "mock_name", 134 message: "mock_message" 135 }; 136 callback(code, true); 137 }, 138 getAccessToken: (options: osAccount.GetDomainAccessTokenOptions, callback: AsyncCallback<Uint8Array>) => { 139 console.info("plugin getAccessToken domain") 140 let code: BusinessError = { 141 code: 0, 142 name: "mock_name", 143 message: "mock_message" 144 }; 145 let token: Uint8Array = new Uint8Array([0]); 146 callback(code, token); 147 } 148 } 149 ``` 150 1512. 调用[registerPlugin](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#registerplugin9)注册插件。 152 153 ```ts 154 try { 155 osAccount.DomainAccountManager.registerPlugin(plugin) 156 console.info("registerPlugin success") 157 } catch (err) { 158 console.info("registerPlugin err: " + JSON.stringify(err)); 159 } 160 ``` 161 162## 注销插件 163 164当插件不再使用时,开发者可以使用[unregisterPlugin](../../reference/apis-basic-services-kit/js-apis-osAccount-sys.md#unregisterplugin9)接口注销插件。 165 166具体开发实例如下: 167 168```ts 169try { 170 osAccount.DomainAccountManager.unregisterPlugin(); 171 console.log('unregisterPlugin success.'); 172} catch(err) { 173 console.log('unregisterPlugin err:' + JSON.stringify(err)); 174} 175``` 176