Auto.js study notes 11: The implementation of the local storage function is actually the XML file used to cache SharedPreference in Android

Declare that the autojs I use is version 4.1.1

Honji Existencestorages

The storages module provides support for saving simple data, user configuration, etc. The saved data will be retained unless the app is uninstalled or actively deleted.

storages supportnumber, boolean, stringequal number deferred type and moreObject ,ArrayforJSON.stringifyordering.

The data saved in storages is shared between scripts.Any script can obtain the corresponding data as long as it knows the storage name, so It cannot be used for the storage of sensitive data. Storages cannot provide independent storage based on domain names like LocalStorage in web development, because the path of the script may change at any time.

storages.create(name)

  • name {string} local storage name

Creates a local storage and returns anStorage object. Locally stored data with different names are separated, while locally stored data with the same name are a>. Shared

For example, in a script, create a storage named ABC and store a=123:

var storage = storages.create("ABC");
storage.put("a", 123);

Storages

Note:

The following api must be written like this, var sgObj= storages.create("ABC"); to obtain theStorage object and assign the value

sgObj.put("a", 123);

Get value (get(key[, defaultValue]))

  • key {string} key value
  • defaultValue {any} optional, default value

Get the data with key value from local storage and return it.

If the storage does not contain the data, then if a default value parameter is specified, the default value will be returned, otherwise undefined will be returned.

The data returned by may be of any data type, depending on the data type used to save the key's data using Storage.put.

Set value (put(key, value))

  • key {string} key value
  • value {any} value

Save the value value to local storage. value can be any data type except undefined. If value is undefined, throwTypeError.

The stored procedure actually usesJSON.stringify to convert the value into a string and then saves it, so the value must be JSONizable to be accepted.

Remove key value data (remove(key))

Remove the data whose key value is key. Returns no value.

Data containing key values ​​(contains(key))

  • key {string} key value

Returns whether the local storage contains data with the key value key. Returns true if yes, otherwise returns false.

Remove all data stored locally (clear())

Remove all data from this local storage. Returns no value.

Practical Development Tool Methods

 myUtils.isEmpty() is my custom non-empty judgment function.

var SharedPrefersUtilsClass = {
  getWxAutoConfigStorage:function(){
        let storage = storages.create("wx_auto_send_file");
        return storage;
    },initWxAutoConfig:function(){
        if(!this.getWxAutoConfigStorage().contains("send_info")||
        myUtils.isEmpty(this.getWxAutoSendConfig())){//第一次存储
            this.setWxAutoSendConfig(wxAutoConfigObj);
        }
    },
    /** 获取微信信息发送所有配置信息 */
    getWxAutoSendConfig:function(){
        return this.getWxAutoConfigStorage().get("send_info");
    },
    /** 设置微信信息发送所有配置信息 */
    setWxAutoSendConfig :function (sendInfoObj){
        this.getWxAutoConfigStorage().put("send_info", JSON.stringify(sendInfoObj));
    },
    /** 清除微信信息发送所有配置信息的数据 */
    removeWxAutoSendConfig:function(){
       return this.getWxAutoConfigStorage().remove("send_info");
    },
    /** 清除所有数据 */
    clearAllData:function(){
        this.getWxAutoConfigStorage().clear();
    }
}

Just watching and typing is useless
After reading, you must practice it
You must type the code
Be sure to run trial and error
This is meaningful learning

Guess you like

Origin blog.csdn.net/piyangbo/article/details/125170842