@icanvas/apis
Version:
这是icanvas的Api功能包
57 lines (56 loc) • 1.21 kB
JavaScript
let Storage = {
/**
* 异步获取本地storage
* @param {String} key
*/
Get(key) {
if (ENV.core == 'wxgame') {
return new Promise((success, fail) => {
wx.getStorage({ success, fail, key });
})
.then(r => r.data)
.catch(() => null);
} else {
return Promise.resolve(window.localStorage.getItem(key))
.then(res => JSON.parse(res))
.catch(() => null);
}
},
/**
* 异步设置本地storage
* @param {String} key
* @param {Any} data
*/
Set(key, data) {
if (ENV.core == 'wxgame') {
return new Promise((success, fail) => (wx.setStorage({ success, fail, key, data }), data)).then(() => data);
} else {
window.localStorage.setItem(key, JSON.stringify(data));
return Promise.resolve(data);
}
},
/**
* 同步获取storage
*/
GetSync:
ENV.core == 'wxgame'
? wx.getStorageSync
: key => {
let res = window.localStorage.getItem(key);
try {
return JSON.parse(res);
} catch (e) {
return null;
}
},
/**
* 同步设置storage
*/
SetSync:
ENV.core == 'wxgame'
? wx.setStorageSync
: (key, data) => {
window.localStorage.setItem(key, JSON.stringify(data));
},
};
export default Storage;