From 0f9c7837f30097a009484ae578085e13fe07564c Mon Sep 17 00:00:00 2001 From: qiang Date: Sat, 18 Jan 2020 22:13:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=20app-v3=20=E4=BB=8E?= =?UTF-8?q?=E5=85=B6=E4=BB=96=E6=A8=A1=E5=BC=8F=E8=AF=BB=E5=8F=96=20storag?= =?UTF-8?q?e=20=E6=95=B0=E6=8D=AE=E6=A0=BC=E5=BC=8F=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/service/api/storage/storage.js | 38 ++++++++++++++++--------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/core/service/api/storage/storage.js b/src/core/service/api/storage/storage.js index ecd87aac6..dd92f954d 100644 --- a/src/core/service/api/storage/storage.js +++ b/src/core/service/api/storage/storage.js @@ -4,12 +4,14 @@ const STORAGE_KEYS = 'uni-storage-keys' function parseValue (value) { const types = ['object', 'string', 'number', 'boolean', 'undefined'] try { - const object = JSON.parse(value) - if (types.indexOf(object.type) >= 0) { + const object = typeof value === 'string' ? JSON.parse(value) : value + const type = object.type + if (types.indexOf(type) >= 0) { const keys = Object.keys(object) - if (keys.length === 2 && 'type' in object && 'data' in object) { + // eslint-disable-next-line valid-typeof + if (keys.length === 2 && 'data' in object && typeof object.data === type) { return object.data - } else if (keys.length === 1 && 'type' in object) { + } else if (keys.length === 1) { return '' } } @@ -27,7 +29,7 @@ export function setStorage ({ }) try { if (type === 'string' && parseValue(value) !== undefined) { - localStorage.setItem(key + STORAGE_DATA_TYPE, 'String') + localStorage.setItem(key + STORAGE_DATA_TYPE, type) } else { localStorage.removeItem(key + STORAGE_DATA_TYPE) } @@ -60,16 +62,24 @@ export function getStorage ({ } } let data = value - const type = localStorage.getItem(key + STORAGE_DATA_TYPE) - if (!type) { - // 兼容H5和V3初期历史格式 - const object = parseValue(value) - data = object !== undefined ? object : data - } else if (type !== 'String') { - // 兼容App端历史格式 + const typeOrigin = localStorage.getItem(key + STORAGE_DATA_TYPE) || '' + const type = typeOrigin.toLowerCase() + if (type !== 'string' || (typeOrigin === 'String' && value === '{"type":"undefined"}')) { try { - data = JSON.parse(value) - data = typeof data === 'string' ? JSON.parse(data) : data + // 兼容H5和V3初期历史格式 + let object = JSON.parse(value) + const result = parseValue(object) + if (result !== undefined) { + data = result + } else if (type) { + // 兼容App端历史格式 + data = object + if (typeof object === 'string') { + object = JSON.parse(object) + // eslint-disable-next-line valid-typeof + data = typeof object === (type === 'null' ? 'object' : type) ? object : data + } + } } catch (error) { } } return { -- GitLab