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.
112 lines
2.1 KiB
112 lines
2.1 KiB
/**
|
|
* @description 存储数据
|
|
* @param {*} key 本地缓存中指定的key
|
|
* @param {*} data 需要缓存的数据
|
|
*/
|
|
export const setStorage = (key, data) => {
|
|
try {
|
|
wx.setStorageSync(key, data)
|
|
} catch (error) {
|
|
console.error(`存储指定 ${key} 数据发生了异常`,error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description 从本地读取指定 key 的数据来保存
|
|
* @param {*} key
|
|
*/
|
|
export const getStorage = (key) => {
|
|
try {
|
|
const data = wx.getStorageSync(key)
|
|
|
|
if (data) {
|
|
return data
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error(`读取指定 ${key} 数据发生了异常`,error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description 从本地移除指定key的数据
|
|
* @param {*} key
|
|
*/
|
|
export const removeStorage = (key) => {
|
|
try {
|
|
wx.removeStorageSync(key)
|
|
} catch (error) {
|
|
console.error(`移除指定 ${key} 数据发生了异常`,error)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description 从本地移除、清空全部数据
|
|
*/
|
|
export const clearStorage =() => {
|
|
try{
|
|
wx.clearStorageSync()
|
|
} catch (error) {
|
|
console.error(`清楚、清空数据发生了异常`,error)
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @description 异步将数据存储到本地
|
|
* @param {*} key 本地缓存中指定的 key
|
|
* @param {*} data 需要缓存的数据
|
|
*/
|
|
export const asyncSetStorage =(key, data) => {
|
|
return new Promise((resolve) => {
|
|
wx.setStorage({
|
|
key,
|
|
data,
|
|
complete (res) {
|
|
resolve(res)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* @description 异步从本地获取指定 key 的数据
|
|
* @param {*} key
|
|
*/
|
|
export const asyncGetStorage = (key) => {
|
|
return new Promise((resolve) =>{
|
|
wx.getStorage({
|
|
key,
|
|
complete(res) {
|
|
resolve(res)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* @description 异步从本地移除指定 key 的数据
|
|
* @param {*} key
|
|
*/
|
|
export const asyncRemoveStorage = (key) => {
|
|
return new Promise((resolve) =>{
|
|
wx.removeStorage({
|
|
key,
|
|
complete(res) {
|
|
resolve(res)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
/**
|
|
* @description 异步从本地移除全部数据
|
|
*/
|
|
export const asyncClearStorage = () => {
|
|
return new Promise((resolve) =>{
|
|
wx.clearStorage({
|
|
complete(res) {
|
|
resolve(res)
|
|
}
|
|
})
|
|
})
|
|
} |