管理应用帐号

应用开发者可以使用应用帐号SDK管理本应用的帐号数据。

能力限制:应用卸载场景下,被卸载应用的帐号数据会被删除;本地帐号删除场景下,被删除本地帐号下的所有应用的帐号数据会被删除。

开发准备

  1. 导入应用帐号模块。

    import account_appAccount from '@ohos.account.appAccount';
    
  2. 获取应用帐号的实例对象。

    const appAccountManager = account_appAccount.createAppAccountManager();
    

创建应用帐号

用户在应用中登录后,开发者可以在系统中创建一个关联的应用帐号,后续可以基于此帐号进行数据管理。

具体开发实例如下:

  1. 参数准备,指定帐号名和可选配置。

    let name: string = "ZhangSan";
    let options: account_appAccount.CreateAccountOptions = {
      customData: {
        age: '10'
      }
    };
    
  2. 调用createAccount接口,根据名称和选项创建应用帐号。

    try {
      await appAccountManager.createAccount(name, options);
      console.log('createAccount successfully');
    } catch (err: BusinessError) {
      console.log('createAccount failed, error: ' + JSON.stringify(err));
    }
    

查询应用帐号列表

具体开发实例如下:

  1. 准备参数,指定帐号所有者。

    let owner: string = 'com.example.accountjsdemo2';
    
  2. 调用getAllAccounts接口查询帐号列表。

    appAccountManager.getAllAccounts().then((data: account_appAccount.AppAccountInfo[]) => {
        console.debug('getAllAccounts successfully, data: ' + JSON.stringify(data));
    }).catch((err: BusinessError) => {
        console.debug('getAllAccounts failed, error: ' + JSON.stringify(err));
    });
    

存取帐号的凭据

具体开发实例如下:

  1. 准备参数,指定帐号名、凭据类型和凭据。

    let name: string = 'ZhangSan';
    let credentialType: string = 'PIN_SIX';
    let credential: string = 'xxxxxx';
    
  2. 调用getCredential接口,获取帐号的凭据。

    appAccountManager.getCredential(name, credentialType).then((data: string) => {
        console.log('getCredential successfully, data: ' + data);
    }).catch((err: BusinessError) => {
        console.log('getCredential failed, error: ' + JSON.stringify(err));
    });
    
  3. 调用setCredential接口,设置帐号的凭据。

    appAccountManager.setCredential(name, credentialType, credential).then(() => {
        console.log('setCredential successfully');
    }).catch((err: BusinessError) => {
        console.log('setCredential failed: ' + JSON.stringify(err));
    });
    

存取帐号的自定义数据

具体开发实例如下:

  1. 准备参数,指定帐号名和自定义键值。

    let name: string = 'ZhangSan';
    let key: string = 'age';
    let value: string = '12';
    
  2. 调用setCustomData接口,设置帐号的自定义数据。

    appAccountManager.setCustomData(name, key, value).then(() => {
        console.log('setCustomData successfully');
    }).catch((err: BusinessError) => {
        console.log('setCustomData failed: ' + JSON.stringify(err));
    });
    
  3. 调用getCustomData接口,获取帐号的自定义数据。

    appAccountManager.getCustomData(name, key).then((data: string) => {
        console.log('getCustomData successfully, data: ' + data);
    }).catch((err: BusinessError) => {
        console.log('getCustomData failed, error: ' + JSON.stringify(err));
    });
    

存取帐号的授权令牌

具体开发实例如下:

  1. 准备参数,指定帐号名、帐号所有者、授权类型和授权令牌。

    let name: string = 'ZhangSan';
    let owner: string = 'com.example.accountjsdemo';
    let authType: string = 'getSocialData';
    let token: string = 'xxxxxx';
    
  2. 调用setAuthToken接口,设置指定授权类型的授权令牌。

    appAccountManager.setAuthToken(name, authType, token).then(() => {
        console.log('setAuthToken successfully');
    }).catch((err: BusinessError) => {
        console.log('setAuthToken failed: ' + JSON.stringify(err));
    });
    
  3. 调用getAuthToken接口,获取指定授权类型的授权令牌。

    appAccountManager.getAuthToken(name, owner, authType).then((data: string) => {
        console.log('getAuthToken successfully, data: ' + data);
    }).catch((err: BusinessError) => {
        console.log('getAuthToken failed, error: ' + JSON.stringify(err));
    });
    

删除应用帐号

用户退出登录后,应用需及时将相应的应用帐号从系统中删除。

具体开发实例如下:

  1. 指定要删除的帐号名称,调用removeAccount接口删除帐号。

    let name: string = 'Zhangsan';
    appAccountManager.removeAccount(name).then(() => {
        console.log('removeAccount successfully');
    }).catch((err: BusinessError) => {
        console.log('removeAccount failed, error: ' + JSON.stringify(err));
    });