forked from ccsens_fe/tall-mui-3
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
1.9 KiB
101 lines
1.9 KiB
4 years ago
|
export default {
|
||
|
/**
|
||
|
* 设置本地存储 同步
|
||
|
* @param {string} key
|
||
|
* @param {*} data
|
||
|
*/
|
||
|
setStorageSync(key, data) {
|
||
|
const value = typeof data === 'string' ? data : JSON.stringify(data);
|
||
|
uni.setStorageSync(key, value);
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 获取本地存储的信息 根据key
|
||
|
* @param {string} key
|
||
|
* @return {string}
|
||
|
*/
|
||
|
getStorageSync(key) {
|
||
|
return uni.getStorageSync(key);
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 根据key移除某条数据 同步
|
||
|
* @param {string} key
|
||
|
*/
|
||
|
removeStorageSync(key) {
|
||
|
uni.removeStorageSync(key);
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 清楚全部数据 同步
|
||
|
*/
|
||
|
clearStorageSync() {
|
||
|
uni.clearStorageSync();
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 设置本地存储 异步
|
||
|
* @param {string} key
|
||
|
* @param {*} data
|
||
|
*/
|
||
|
setStorage(key, data) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
const value = typeof data === 'string' ? data : JSON.stringify(data);
|
||
|
uni.setStorage({
|
||
|
key,
|
||
|
data: value,
|
||
|
success() {
|
||
|
resolve(`数据${key}存储成功`);
|
||
|
},
|
||
|
fail() {
|
||
|
reject(`数据${key}存储失败`);
|
||
|
},
|
||
|
});
|
||
|
});
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 获取本地存储的信息 根据key 异步
|
||
|
* @param {string} key
|
||
|
* @return {string}
|
||
|
*/
|
||
|
getStorage(key) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
uni.getStorage({
|
||
|
key,
|
||
|
success(res) {
|
||
|
resolve(res.data);
|
||
|
},
|
||
|
fail(error) {
|
||
|
reject(`数据${key}获取失败, error: ${error}`);
|
||
|
},
|
||
|
});
|
||
|
});
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 根据key移除某条数据 异步
|
||
|
* @param {string} key
|
||
|
*/
|
||
|
removeStorage(key) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
uni.removeStorage({
|
||
|
key,
|
||
|
success(res) {
|
||
|
resolve(res);
|
||
|
},
|
||
|
fail(error) {
|
||
|
reject(`数据${key}删除失败, error: ${error}`);
|
||
|
},
|
||
|
});
|
||
|
});
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 清楚全部数据 异步
|
||
|
*/
|
||
|
clearStorage() {
|
||
|
uni.clearStorage();
|
||
|
},
|
||
|
};
|