目录

Data Cache

has.setStorageSync

has.setStorageSync(string key, string | object | number | boolean | null | array data)

Stores data in the local cache based on the specified key. This API returns the result synchronously. The original content corresponding to the key will be overwritten.

The data remains available until the user deletes it.

The maximum size of the data corresponding to a single key is 1 MB, and the maximum size of all data is 10 MB.

Since: 1.0.0

Parameters

Parameter Type Mandatory Description
key string Yes Key of the stored data.
data string | object | number | boolean | null | array Yes Data to be stored. Only basic-type objects and objects that can be serialized using JSON.stringify are supported.

Error code information

Error Code Error Message
101

Common error. Possible causes:

1.storage error, fail to calculate size.

2.exceed data max length.

3.exceed storage max length.

102

Parameter error. Possible causes:

1.key is invalid.

2.check type is invalid.

Example:

has.setStorageSync('key', 'value');

has.setStorage

has.setStorage(Object object)

Stores data in the local cache based on the specified key. This API uses an asynchronous callback to return the result. The original content corresponding to the key will be overwritten.

The data remains available until the user deletes it.

The maximum size of the data corresponding to a single key is 1 MB, and the maximum size of all data is 10 MB.

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
key string Yes Key of the stored data.
data string | object | number | boolean | null | array Yes Data to be stored. Only basic-type objects and objects that can be serialized using JSON.stringify are supported.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
101

Common error. Possible causes:

1.storage error, fail to calculate size.

2.exceed data max length.

3.exceed storage max length.

102

Parameter error. Possible causes:

1.key is invalid.

2.check type is invalid.

60000801

setStorage error.

For details, see File Management Error Codes.

Example:

has.setStorage({
  key: 'key',
  data: 'test',
  success: (res) => {
    console.info('setStorage success');
  },
  fail: (err) => {
    console.error('setStorage fail', err);
  },
  complete: (res) => {
    console.info('setStorage complete', res);
  }
});

has.removeStorageSync

has.removeStorageSync(string key)

Removes the cache of the specified key.

Since: 1.0.0

Parameters

Parameter Type Mandatory Description
key string Yes Key of the removed cache.

Error code information

Error Code Error Message
102

Parameter error. Possible causes:

1.key is invalid.

2.check type is invalid.

Example:

has.removeStorageSync('key');

has.removeStorage

has.removeStorage(Object object)

Removes the cache of the specified key. This API uses an asynchronous callback to return the result.

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
key string Yes Key of the removed cache.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
102

Parameter error. Possible causes:

1.key is invalid.

2.check type is invalid.

60000701

removeStorage error.

For details, see File Management Error Codes.

Example:

has.removeStorage({
  key: 'key',
  success: () => {
    console.info('removeStorage success');
  },
  fail: (err) => {
    console.error('removeStorage fail', err);
  },
  complete: (res) => {
    console.info('removeStorage complete', res);
  }
});

has.getStorageSync

has.getStorageSync(string key): Object

Obtains the cache of the specified key.

Since: 1.0.0

Parameters

Parameter Type Mandatory Description
key string Yes Key of the obtained cache.

Returns

Parameter Type Description
data any Cache of the specified key obtained.

Error code information

Error Code Error Message
102

Parameter error. Possible causes:

1.key is invalid.

2.check type is invalid.

Example:

has.getStorageSync('key');

has.getStorage

has.getStorage(Object object)

Obtains the cache of the specified key. This API uses an asynchronous callback to return the result.

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
key string Yes Key of the obtained cache.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
data any Cache of the specified key obtained.

Error code information

Error Code Error Message
102

Parameter error. Possible causes:

1.key is invalid.

2.check type is invalid.

60000601

getStorage error.

For details, see File Management Error Codes.

Example:

has.getStorage({
  key: 'key',
  success: (res) => {
    console.info('getStorage success:', res);
  },
  fail: (err) => {
    console.error('getStorage fail:', err);
  },
  complete: (res) => {
    console.info('getStorage complete:', res);
  }
});

has.getStorageInfoSync

has.getStorageInfoSync(): Object

Obtains cache information.

Since: 1.0.0

Returns

An object containing the following fields.

Parameter Type Description
keys Array<string> All keys in the local cache.
currentSize number Data size that has been applied for, in KB.
limitSize number Maximum data size allowed, in KB.

Error code information

Error Code Error Message
101 Common error. Possible causes: operation failed.

Example:

const storageInfo = has.getStorageInfoSync();
console.info(storageInfo.keys);
console.info(storageInfo.currentSize);
console.info(storageInfo.limitSize);

has.getStorageInfo

has.getStorageInfo(Object object)

Obtains cache information. This API uses an asynchronous callback to return the result.

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
keys Array<string> All keys in the local cache.
currentSize number Data size that has been applied for, in KB.
limitSize number Maximum data size allowed, in KB.

Error code information

Error Code Error Message
101 Common error. Possible causes:operation failed.
60000101

getStorageInfo error.

For details, see File Management Error Codes.

Example:

has.getStorageInfo({
  success: (res) => {
    console.info('getStorageInfo success:', res);
  },
  fail: (err) => {
    console.error('getStorageInfo fail:', err);
  },
  complete: (res) => {
    console.info('getStorageInfo complete:', res);
  }
});

has.clearStorageSync

has.clearStorageSync()

Clears the local cache.

Since: 1.0.0

Error code information

Error Code Error Message
101 Common error. Possible causes: operation failed.

Example:

has.clearStorageSync();

has.clearStorage

has.clearStorage(Object object)

Clears the local cache. This API uses an asynchronous callback to return the result.

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
101 Common error. Possible causes: operation failed.
60000401

clearStorage error.

For details, see File Management Error Codes.

Example:

has.clearStorage({
  success: () => {
    console.info('clearStorage success!');
  },
  fail: (err) => {
    console.error('clearStorage fail:', err);
  },
  complete: (res) => {
    console.info('clearStorage complete:', res);
  }
});

has.batchSetStorageSync

has.batchSetStorageSync(Array<KVPair> kvList): void

Stores a batch of data in the specified keys of the local cache.

The original content corresponding to the key will be overwritten. The data remains available until the user deletes it or the system clears it due to insufficient storage space.

The maximum size of the data corresponding to a single key is 1 MB, and the maximum size of all data is 10 MB.

Since: 1.0.12

Parameters

Parameter Type Mandatory Description
kvList Array<KVPair> Yes List of data to be stored in batches in the local cache.

Fields of KVPair

Parameter Type Mandatory Description
key string Yes Specified key in the local cache.
value any Yes Data to be stored. Only basic-type objects, date-type objects, and objects that can be serialized using JSON.stringify are supported.

Error code information

Error Code Error Message
101

Common error. Possible causes:

1.fail to calculate size.

2.exceed data maximum size.

3.exceed storage maximum size.

102

Parameter error. Possible causes:

1.kvList value is invalid.

2.key is invalid.

3.check type is invalid.

60000802

batchSetStorageSync:fail.

For details, see File Management Error Codes.

Example:

try {
  const kvList = [{ key: 'key1', value: 'value1' }, { key: 'key2', value: 'value2' }];
  has.batchSetStorageSync(kvList);
} catch (err) {
  console.error('batchSetStorageSync error', err);
};

has.batchSetStorage

has.batchSetStorage(Object object): void

Stores a batch of data in the specified keys of the local cache. This API uses an asynchronous callback to return the result.

The original content corresponding to the key will be overwritten. The data remains available until the user deletes it or the system clears it due to insufficient storage space.

The maximum size of the data corresponding to a single key is 1 MB, and the maximum size of all data is 10 MB.

Since: 1.0.12

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
kvList Array<KVPair> Yes

KVPair contains the following parameters:

- key: key specified in the local cache.

- value: content to be stored. Only basic-type objects, date-type objects, and objects that can be serialized using JSON.stringify are supported.

success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
101

Common error. Possible causes:

1.fail to calculate size.

2.exceed data maximum size.

3.exceed storage maximum size.

102

Parameter error. Possible causes:

1.kvList value is invalid.

2.key is invalid.

3.check type is invalid.

60000802

batchSetStorage:fail.

For details, see File Management Error Codes.

Example:

const kvList = [{ key: 'key1', value: 'value1' }, { key: 'key2', value: 'value2' }];
has.batchSetStorage({
  kvList: kvList,
  success: (res) => {
    console.info('batchSetStorage success', res);
  },
  fail: (err) => {
    console.error('batchSetStorage fail', err);
  },
  complete: (res) => {
    console.info('batchSetStorage complete', res);
  }
});

has.batchGetStorageSync

has.batchGetStorageSync(Array<string> keyList): Array<any>

Obtains a batch of content of the specified keys from the local cache. This API returns the result synchronously.

Since: 1.0.12

Parameters

Parameter Type Mandatory Description
keyList Array<string> Yes Key array in the local cache.

Returns

Content of the specified keys obtained, whose type is Array<any>.

Error code information

Error Code Error Message
102

Parameter error. Possible causes:

1.kvList value is invalid.

2.key is invalid.

3.check type is invalid.

Example:

try {
  const valueList = has.batchGetStorageSync(['key1','key2']);
  if (valueList) {
    // Array of the content corresponding to the keys: ['value1', 'value2']
  }
} catch (err) {
  console.error('batchGetStorageSync error', err);
};

has.batchGetStorage

has.batchGetStorage(Object object): void

Obtains a batch of content of the specified keys from the local cache. This API uses an asynchronous callback to return the result.

Since: 1.0.12

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
keyList Array<string> Yes Key array in the local cache.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
errMsg string Error message.
dataList Array<any> Content of the specified keys obtained.

Error code information

Error Code Error Message
102

Parameter error. Possible causes:

1.kvList value is invalid.

2.key is invalid.

3.check type is invalid.

60000602

batchGetStorage error.

For details, see File Management Error Codes.

Example:

has.batchGetStorage({
  keyList: ['key1', 'key2'],
  success: (res) => {
    // res.dataList: ['value1', 'value2']
    console.info('batchGetStorage success', res.dataList);
  },
  fail: (err) => {
    console.error('batchGetStorage fail', err);
  },
  complete: (res) => {
    console.info('batchGetStorage complete', res);
  }
});

has.setBackgroundFetchToken

has.setBackgroundFetchToken(Object object): void

Sets the custom sign-in status. This token is carried when a data prefetch request is initiated to facilitate the third-party server verify the request validity.

Since: 1.0.18

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
token string Yes Custom sign-in state.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
102 Parameter error. Possible causes: token is required and must be a string.

Example:

App({
  onLaunch(opts) {
    has.setBackgroundFetchToken({
      token: 'test_token',
      success: () => {
        console.info('setBackgroundFetchToken success!');
      },
      fail: (err) => {
        console.error('setBackgroundFetchToken fail:', err);
      },
      complete: (res) => {
        console.info('setBackgroundFetchToken complete:', res);
      }
    });
  }
});

has.onBackgroundFetchData

has.onBackgroundFetchData(function listener): void

Listens for the completion event of a prefetch request. If the request is complete during listening, the prefetch event is not triggered.

Since: 1.0.18

Parameters

Parameter Type Mandatory Description
listener function Yes Listener function used to listen for the completion event of a prefetch request.

Return value of callback

A result containing the following parameters.

Parameter Type Description
fetchType string Prefetch type. Currently, only pre is supported.
fetchedData Object Prefetched data.

Error code information

Error Code Error Message
102 Parameter error. Possible causes: listener should be a function.

Example:

App({
  onLaunch(options) {
    has.onBackgroundFetchData((res)=>{
      console.info('onBackgroundFetchData listen success:', res.fetchType); // Prefetch type.
      console.info('onBackgroundFetchData listen success:', res.fetchedData); // Prefetched data.
    })
  }
});

has.getBackgroundFetchData

has.getBackgroundFetchData(Object object): void

Obtains the prefetched data. If the current prefetch request is not complete when this API is called, the operation fails.

Since: 1.0.18

Parameters

Parameter Type Mandatory Description
fetchType string Yes Prefetch type. Currently, only pre is supported.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

An object containing the following fields.

Parameter Type Description
fetchedData Object Prefetched data.

Error code information

Error Code Error Message
102 Parameter error. Possible causes: fetchType must be 'pre'.
132 Background fetch data error. Possible causes: backgroundFetchData is null.

Example:

has.getBackgroundFetchData({
  fetchType: 'pre',
  success: (res) => {
    console.info('getBackgroundFetchData success', res);
  },
  fail: (err) => {
    console.error('getBackgroundFetchData fail', err);
  },
  complete: (res) => {
    console.info('getBackgroundFetchData complete', res);
  }
});