提交 7149ec3e 编写于 作者: 雪洛's avatar 雪洛

Merge branch 'dev' of https://github.com/dcloudio/uni-app into dev

...@@ -10901,23 +10901,23 @@ var serviceContext = (function () { ...@@ -10901,23 +10901,23 @@ var serviceContext = (function () {
uploadFile: uploadFile$1 uploadFile: uploadFile$1
}); });
const STORAGE_DATA_TYPE = '__TYPE';
const STORAGE_KEYS = 'uni-storage-keys';
function setStorage$1 ({ function setStorage$1 ({
key, key,
data data
} = {}) { } = {}) {
const value = { const type = typeof data;
type: typeof data === 'object' ? 'object' : 'string', const value = type === 'string' ? data : JSON.stringify({
type,
data: data data: data
}; });
localStorage.setItem(key, JSON.stringify(value)); try {
const keyList = localStorage.getItem('uni-storage-keys'); localStorage.setItem(key, value);
if (!keyList) { } catch (error) {
localStorage.setItem('uni-storage-keys', JSON.stringify([key])); return {
} else { errMsg: `setStorage:fail ${error}`
const keys = JSON.parse(keyList);
if (keys.indexOf(key) < 0) {
keys.push(key);
localStorage.setItem('uni-storage-keys', JSON.stringify(keys));
} }
} }
return { return {
...@@ -10935,13 +10935,33 @@ var serviceContext = (function () { ...@@ -10935,13 +10935,33 @@ var serviceContext = (function () {
function getStorage$1 ({ function getStorage$1 ({
key key
} = {}) { } = {}) {
const data = localStorage.getItem(key); const value = localStorage && localStorage.getItem(key);
return data ? { if (typeof value !== 'string') {
data: JSON.parse(data).data, return {
data: '',
errMsg: 'getStorage:fail'
}
}
let data = value;
try {
const object = JSON.parse(value);
// 兼容App端历史格式
const type = localStorage.getItem(key + STORAGE_DATA_TYPE);
if (!type) {
const keys = Object.keys(object);
if (keys.length === 2 && 'type' in object && 'data' in object) {
data = object.data;
} else if (keys.length === 1 && 'type' in object) {
data = '';
}
} else if (type !== 'String') {
data = object;
data = typeof data === 'string' ? JSON.parse(data) : data;
}
} catch (error) { }
return {
data,
errMsg: 'getStorage:ok' errMsg: 'getStorage:ok'
} : {
data: '',
errMsg: 'getStorage:fail'
} }
} }
...@@ -10955,14 +10975,11 @@ var serviceContext = (function () { ...@@ -10955,14 +10975,11 @@ var serviceContext = (function () {
function removeStorage$1 ({ function removeStorage$1 ({
key key
} = {}) { } = {}) {
const keyList = localStorage.getItem('uni-storage-keys'); if (localStorage) {
if (keyList) { // 兼容App端历史格式
const keys = JSON.parse(keyList); localStorage.removeItem(key + STORAGE_DATA_TYPE);
const index = keys.indexOf(key); localStorage.removeItem(key);
keys.splice(index, 1); }
localStorage.setItem('uni-storage-keys', JSON.stringify(keys));
}
localStorage.removeItem(key);
return { return {
errMsg: 'removeStorage:ok' errMsg: 'removeStorage:ok'
} }
...@@ -10975,7 +10992,7 @@ var serviceContext = (function () { ...@@ -10975,7 +10992,7 @@ var serviceContext = (function () {
} }
function clearStorage () { function clearStorage () {
localStorage.clear(); localStorage && localStorage.clear();
return { return {
errMsg: 'clearStorage:ok' errMsg: 'clearStorage:ok'
} }
...@@ -10985,18 +11002,23 @@ var serviceContext = (function () { ...@@ -10985,18 +11002,23 @@ var serviceContext = (function () {
clearStorage(); clearStorage();
} }
function getStorageInfo () { // TODO 暂时先不做大小的转换 function getStorageInfo () {
const keyList = localStorage.getItem('uni-storage-keys'); const length = (localStorage && (localStorage.length || localStorage.getLength())) || 0;
return keyList ? { const keys = [];
keys: JSON.parse(keyList), let currentSize = 0;
currentSize: 0, for (let index = 0; index < length; index++) {
limitSize: 0, const key = localStorage.key(index);
if (key !== STORAGE_KEYS && key.indexOf(STORAGE_DATA_TYPE) + STORAGE_DATA_TYPE.length !== key.length) {
const value = localStorage.getItem(key);
currentSize += key.length + value.length;
keys.push(key);
}
}
return {
keys,
currentSize: Math.ceil(currentSize * 2 / 1024),
limitSize: Number.MAX_VALUE,
errMsg: 'getStorageInfo:ok' errMsg: 'getStorageInfo:ok'
} : {
keys: '',
currentSize: 0,
limitSize: 0,
errMsg: 'getStorageInfo:fail'
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册