diff --git a/zh-cn/application-dev/reference/apis/js-apis-account.md b/zh-cn/application-dev/reference/apis/js-apis-account.md index 1bec1b4c3aa15afa2340f83bf2c7829c56b95a5a..8414a3ffa1c857b652b56eb4beac59678348087d 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-account.md +++ b/zh-cn/application-dev/reference/apis/js-apis-account.md @@ -2,4 +2,5 @@ -- **[分布式帐号管理](js-apis-distributed-account.md)** \ No newline at end of file +- **[分布式帐号管理](js-apis-distributed-account.md)** +- **[应用帐号管理](js-apis-appAccount)** \ No newline at end of file diff --git a/zh-cn/application-dev/reference/apis/js-apis-appAccount.md b/zh-cn/application-dev/reference/apis/js-apis-appAccount.md new file mode 100644 index 0000000000000000000000000000000000000000..4d1501cfbbddb504bf6d0a6ab0b37e4a7d249fc8 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-appAccount.md @@ -0,0 +1,479 @@ +# 应用帐号管理 + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 + + +## 导入模块 + +``` +import account_appAccount from '@ohos.account.appAccount'; +``` + + +## account.createAppAccountManager + +createAppAccountManager(): AppAccountManager; + +获取应用帐号单实例对象。 + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | AppAccountManager | 返回一个实例,实例提供查询和更新应用帐号的方法。 | + +- 示例: + ``` + var appAccountManager = account.createAppAccountManager(); + ``` + +## AppAccountManager + +提供查询、更新和订阅应用帐号的方法(需要先获取应用帐号的单实例对象)。 + +### addAccount + +addAccount(name: string, callback: AsyncCallback): void; +addAccount(name: string, extraInfo: string, callback: AsyncCallback): void; +addAccount(name: string, extraInfo?: string): Promise; + +创建应用帐号。 + +需要权限:无。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | name | string | 是 | 创建的应用账号名称。 | +| extraInfo | string | 是 | 账号附加信息。 | + | callback | AsyncCallback | 是 | 创建账号状态回调。 | + +- 示例: + ``` + var appAccountManager = account.createAppAccountManager(); + appAccountManager.addAccount("account_name_demo", (err)=>{ + console.debug("====>add account demo err:" + JSON.stringify(err)); + }); + ``` + +### deleteAccount + +deleteAccount(name: string, callback: AsyncCallback): void; +deleteAccount(name: string): Promise; + +删除应用帐号。 + +需要权限:无。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------------------- | ---- | -------------------- | + | name | string | 是 | 删除的应用账号名称。 | + | callback | AsyncCallback | 是 | 删除账号状态回调。 | + + +- 示例: + ``` + var appAccountManager = account.createAppAccountManager(); + appAccountManager.addAccount("account_name_demo", (err)=>{ + console.debug("====>add account demo err:" + JSON.stringify(err)); + appAccountManager.deleteAccount("account_name_demo", (err)=>{ + console.debug("====>delete account name demo err:" + JSON.stringify(err)); + }); + }); + ``` + +### disableAppAccess + +disableAppAccess(name: string, bundleName: string, callback: AsyncCallback): void; +disableAppAccess(name: string, bundleName: string): Promise; + +收回指定应用访问帐户数据的权限。 + +需要权限:无。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | ---------- | ------------------- | ---- | ------------------ | + | name | string | 是 | 应用账号名称。 | + | bundleName | string | 是 | 指定应用包名称。 | + | callback | AsyncCallback | 是 | 权限收回状态回调。 | + +- 示例: + ``` + var appAccountManager = account.createAppAccountManager(); + var enableBundle = "com.example.actsaccountsceneappaccess"; + appAccountManager.disableAppAccess("disableAppAccess_demo", enableBundle, (err)=>{ + console.debug("====>disable appaccess demo err:" + JSON.stringify(err)); + }); + ``` + +### enableAppAccess + +enableAppAccess(name: string, bundleName: string, callback: AsyncCallback): void; +enableAppAccess(name: string, bundleName: string): Promise; + +授予指定应用访问帐户数据的权限。 + +需要权限:无。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | ---------- | ------------------- | ---- | ------------------ | + | name | string | 是 | 应用账号名称。 | +| bundleName | string | 是 | 指定应用包名称。 | + | callback | AsyncCallback | 是 | 权限授予状态回调。 | + +- 示例: + ``` + var appAccountManager = account.createAppAccountManager(); + var enableBundle = "com.example.actsaccountsceneappaccess"; + appAccountManager.enableAppAccess("enableappAccess_demo", enableBundle, (err)=>{ + console.debug("====>enable appaccess demo err:" + JSON.stringify(err)); + }); + ``` + +### checkAppAccountSyncEnable + +checkAppAccountSyncEnable(name: string, callback: AsyncCallback): void; +checkAppAccountSyncEnable(name: string): Promise; + +检查应用帐号同步功能是否开启。 + +需要权限:ohos.permission.DISTRIBUTED_DATASYNC,系统应用可用。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ---------------------- | ---- | ---------------------- | + | name | string | 是 | 应用账号名称。 | + | callback | AsyncCallback | 是 | 应用账号同步状态回调。 | + +- 示例: + + ``` + var appAccountManager = account.createAppAccountManager(); + appAccountManager.checkAppAccountSyncEnable("syncenable_callback_notset", (err, data)=>{ + console.debug("====>checkAppAccountSyncEnable err:" + JSON.stringify(err)); + console.debug("====>checkAppAccountSyncEnable data:" + JSON.stringify(data)); + }); + ``` + +### setAccountCredential + +setAccountCredential(name: string, credentialType: string, credential: string, + callback: AsyncCallback): void; +setAccountCredential(name: string, credentialType: string, credential: string): Promise; + +设置应用帐号认证信息。 + +需要权限:无。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------------- | ------------------- | ---- | ------------------------ | + | name | string | 是 | 应用账号名称。 | + | credentialType | string | 是 | 认证类型。 | + | credential | string | 是 | 认证信息。 | + | callback | AsyncCallback | 是 | 帐号认证信息设置状态回调 | + +- 示例: + + ``` + var appAccountManager = account.createAppAccountManager(); + appAccountManager.setAccountCredential("account_name", "credentialType1", "credential1",(err)=>{ + console.debug("====>ActsAccountCredential_0100 setAccountCredential:" + JSON.stringify(err)); + }); + ``` + +### setAccountExtraInfo + +setAccountExtraInfo(name: string, extraInfo: string, callback: AsyncCallback): void; +setAccountExtraInfo(name: string, extraInfo: string): Promise; + +设置帐号扩展信息。 + +需要权限:无。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | --------- | ------------------- | ---- | ---------------------- | + | name | string | 是 | 应用账号名称。 | + | extraInfo | string | 是 | 应用账号扩展信息。 | + | callback | AsyncCallback | 是 | 设置扩展信息状态回调。 | + +- 示例: + + ``` + var appAccountManager = account.createAppAccountManager(); + var notExtralAccount = "account_name_extral_info"; + appAccountManager.setAccountExtraInfo(notExtralAccount, "account_name_extral_info", (err)=>{ + console.debug("====>setAccountExtraInfo err:" + JSON.stringify(err)); + }); + ``` + +### setAppAccountSyncEnable + +setAppAccountSyncEnable(name: string, isEnable: boolean, callback: AsyncCallback): void; +setAppAccountSyncEnable(name: string, isEnable: boolean): Promise; + +设置应用帐号信息同步状态。 + +需要权限:ohos.permission.DISTRIBUTED_DATASYNC,系统应用可用。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------------------- | ---- | -------------------------- | + | name | string | 是 | 应用账号名称。 | + | isEnable | boolean | 是 | 同步状态。 | + | callback | AsyncCallback | 是 | 应用帐号信息同步状态回调。 | + +- 示例: + + ``` + var appAccountManager = account.createAppAccountManager(); + appAccountManager.setAppAccountSyncEnable("syncenable_callback_settrue", true, (err)=>{ + console.debug("====>setAppAccountSyncEnable err:" + JSON.stringify(err)); + }); + ``` + +### setAssociatedData + +setAssociatedData(name: string, key: string, value: string, callback: AsyncCallback): void; +setAssociatedData(name: string, key: string, value: string): Promise; + +设置应用帐号关联信息。 + +需要权限:无。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------------------- | ---- | ---------------------------- | + | name | string | 是 | 应用账号名称。 | + | key | string | 是 | 关联信息主键。 | + | value | string | 是 | 关联信息内容。 | + | callback | AsyncCallback | 是 | 应用帐号关联信息设置状态回调 | + +- 示例: + + ``` + var appAccountManager = account.createAppAccountManager(); + appAccountManager.setAssociatedData("account_name", "key1", "value1", (err)=>{ + console.debug("====>setAssociatedData ActsAccountAssociatedData_0100 err:" + JSON.stringify(err)); + }); + ``` + +### getAllAccessibleAccounts + +getAllAccessibleAccounts(callback: AsyncCallback>): void; +getAllAccessibleAccounts(): Promise>; + +获取全部应用已授权帐号信息。 + +需要权限:ohos.permission.GET_ACCOUNTS_PRIVILEGED,系统应用可用。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------------------------------------ | ---- | ---------------- | + | callback | AsyncCallback> | 是 | 应用账号信息列表 | + +- 示例: + + ``` + var appAccountManager = account.createAppAccountManager(); + appAccountManager.getAllAccessibleAccounts((err, data)=>{ + console.debug("====>getAllAccessibleAccounts err:" + JSON.stringify(err)); + console.debug("====>getAllAccessibleAccounts data:" + JSON.stringify(data)); + }); + ``` + +### getAllAccounts + +getAllAccounts(owner: string, callback: AsyncCallback>): void; +getAllAccounts(owner: string): Promise>; + +获取指定应用全部帐号信息。 + +需要权限:ohos.permission.GET_ACCOUNTS_PRIVILEGED,系统应用可用。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------------------------------------ | ---- | ---------------- | + | owner | string | 是 | 应用包名称 | + | callback | AsyncCallback> | 是 | 应用账号信息列表 | + +- 示例: + + ``` + var appAccountManager = account.createAppAccountManager(); + var selfBundle = "com.example.actsgetallaaccounts"; + appAccountManager.getAllAccounts(selfBundle, (err, data)=>{ + console.debug("====>getAllAccounts err:" + JSON.stringify(err)); + console.debug("====>getAllAccounts data:" + JSON.stringify(data)); + }); + ``` + +### getAccountCredential + +getAccountCredential(name: string, credentialType: string, callback: AsyncCallback): void; +getAccountCredential(name: string, credentialType: string): Promise; + +获取应用帐号认证信息。 + +需要权限:无。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------------- | --------------------- | ---- | ------------------ | + | name | string | 是 | 应用账号名称。 | + | credentialType | string | 是 | 认证类型。 | + | callback | AsyncCallback | 是 | 认证信息内容回调。 | + +- 示例: + + ``` + var appAccountManager = account.createAppAccountManager(); + appAccountManager.getAccountCredential("account_name", "credentialType", (err,data)=>{ + console.debug("====>getAccountCredential err:" + JSON.stringify(err)); + console.debug("====>getAccountCredential data:" + JSON.stringify(data)); + }); + ``` + +### getAccountExtraInfo + +getAccountExtraInfo(name: string, callback: AsyncCallback): void; +getAccountExtraInfo(name: string): Promise; + +获取应用帐号扩展信息。 + +需要权限:无。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | --------------------- | ---- | ------------------ | + | name | string | 是 | 应用账号名称。 | + | callback | AsyncCallback | 是 | 扩展信息内容回调。 | + +- 示例: + + ``` + var appAccountManager = account.createAppAccountManager(); + appAccountManager.getAccountExtraInfo("account_name", (err, data)=>{ + console.debug("====>getAccountExtraInfo err:" + JSON.stringify(err)); + console.debug("====>getAccountExtraInfo data:" + JSON.stringify(data)); + }); + ``` + +### getAssociatedData + +getAssociatedData(name: string, key: string, callback: AsyncCallback): void; +getAssociatedData(name: string, key: string): Promise; + +获取应用帐号关联信息。 + +需要权限:无。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | --------------------- | ---- | ------------------ | + | name | string | 是 | 应用账号名称。 | + | key | string | 是 | 关联信息主键。 | + | callback | AsyncCallback | 是 | 关联信息内容回调。 | + +- 示例: + + ``` + var appAccountManager = account.createAppAccountManager(); + appAccountManager.getAssociatedData("account_name", "key", (err, data)=>{ + console.debug("====>getAssociatedData err:" + JSON.stringify(err)); + console.debug("====>getAssociatedData data:" + JSON.stringify(data)); + }); + ``` + +### on + +on(type: 'change', owners: Array, callback: Callback>): void; + +订阅应用帐号变更事件。 + +需要权限:无。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------------------------------- | ---- | ---------------------------- | + | type | 'change' | 是 | 订阅事件类型。 | + | owners | Array | 是 | 订阅bundle列表。 | + | callback | Callback> | 是 | 变更的应用账号信息列表回调。 | + +- 示例: + + ``` + var appAccountManager = account.createAppAccountManager(); + function changeOnCallback(data){ + console.debug("====>receive change data:" + JSON.stringify(data)); + } + try{ + appAccountManager.on('change', ["com.example.actsaccounttest"], changeOnCallback); + } + catch(err){ + console.error("====>on accountOnOffDemo err:" + JSON.stringify(err)); + } + ``` + +### off + +off(type: 'change', callback?: Callback): void; + +取消订阅应用帐号变更事件。 + +需要权限:无。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------------- | ---- | ------------------ | + | type | 'change' | 是 | 订阅事件类型。 | + | callback | Callback | 是 | 取消订阅状态回调。 | + +- 示例: + + ``` + var appAccountManager = account.createAppAccountManager(); + function changeOnCallback(data){ + console.debug("====>receive change data:" + JSON.stringify(data)); + appAccountManager.off('change', function(){ + console.debug("====>off finish===="); + } + } + try{ + appAccountManager.on('change', ["com.example.actsaccounttest"], changeOnCallback); + } + catch(err){ + console.error("====>on accountOnOffDemo err:" + JSON.stringify(err)); + } + ``` + +### + + +## AppAccountInfo + +提供应用帐户信息结构。 + + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------- | -------- | -------- | +| owner | string | 是 | 应用包名称,非空字符串。 | +| name | string | 是 | 应用账号名称,非空字符串。 |