提交 0f9c7837 编写于 作者: Q qiang

fix: 解决 app-v3 从其他模式读取 storage 数据格式错误的问题

上级 06b48591
...@@ -4,12 +4,14 @@ const STORAGE_KEYS = 'uni-storage-keys' ...@@ -4,12 +4,14 @@ const STORAGE_KEYS = 'uni-storage-keys'
function parseValue (value) { function parseValue (value) {
const types = ['object', 'string', 'number', 'boolean', 'undefined'] const types = ['object', 'string', 'number', 'boolean', 'undefined']
try { try {
const object = JSON.parse(value) const object = typeof value === 'string' ? JSON.parse(value) : value
if (types.indexOf(object.type) >= 0) { const type = object.type
if (types.indexOf(type) >= 0) {
const keys = Object.keys(object) 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 return object.data
} else if (keys.length === 1 && 'type' in object) { } else if (keys.length === 1) {
return '' return ''
} }
} }
...@@ -27,7 +29,7 @@ export function setStorage ({ ...@@ -27,7 +29,7 @@ export function setStorage ({
}) })
try { try {
if (type === 'string' && parseValue(value) !== undefined) { if (type === 'string' && parseValue(value) !== undefined) {
localStorage.setItem(key + STORAGE_DATA_TYPE, 'String') localStorage.setItem(key + STORAGE_DATA_TYPE, type)
} else { } else {
localStorage.removeItem(key + STORAGE_DATA_TYPE) localStorage.removeItem(key + STORAGE_DATA_TYPE)
} }
...@@ -60,16 +62,24 @@ export function getStorage ({ ...@@ -60,16 +62,24 @@ export function getStorage ({
} }
} }
let data = value let data = value
const type = localStorage.getItem(key + STORAGE_DATA_TYPE) const typeOrigin = localStorage.getItem(key + STORAGE_DATA_TYPE) || ''
if (!type) { const type = typeOrigin.toLowerCase()
if (type !== 'string' || (typeOrigin === 'String' && value === '{"type":"undefined"}')) {
try {
// 兼容H5和V3初期历史格式 // 兼容H5和V3初期历史格式
const object = parseValue(value) let object = JSON.parse(value)
data = object !== undefined ? object : data const result = parseValue(object)
} else if (type !== 'String') { if (result !== undefined) {
data = result
} else if (type) {
// 兼容App端历史格式 // 兼容App端历史格式
try { data = object
data = JSON.parse(value) if (typeof object === 'string') {
data = typeof data === 'string' ? JSON.parse(data) : data object = JSON.parse(object)
// eslint-disable-next-line valid-typeof
data = typeof object === (type === 'null' ? 'object' : type) ? object : data
}
}
} catch (error) { } } catch (error) { }
} }
return { return {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册