提交 4006e84a 编写于 作者: Q qiang

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

上级 cd74c717
const STORAGE_DATA_TYPE = '__TYPE' const STORAGE_DATA_TYPE = '__TYPE'
const STORAGE_KEYS = 'uni-storage-keys' 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 keys = Object.keys(object)
if (keys.length === 2 && 'type' in object && 'data' in object) {
return object.data
} else if (keys.length === 1 && 'type' in object) {
return ''
}
}
} catch (error) { }
}
export function setStorage ({ export function setStorage ({
key, key,
data data
...@@ -11,6 +26,11 @@ export function setStorage ({ ...@@ -11,6 +26,11 @@ export function setStorage ({
data: data data: data
}) })
try { try {
if (type === 'string' && parseValue(value) !== undefined) {
localStorage.setItem(key + STORAGE_DATA_TYPE, 'String')
} else {
localStorage.removeItem(key + STORAGE_DATA_TYPE)
}
localStorage.setItem(key, value) localStorage.setItem(key, value)
} catch (error) { } catch (error) {
return { return {
...@@ -40,22 +60,18 @@ export function getStorage ({ ...@@ -40,22 +60,18 @@ export function getStorage ({
} }
} }
let data = value let data = value
try { const type = localStorage.getItem(key + STORAGE_DATA_TYPE)
const object = JSON.parse(value) if (!type) {
// 兼容H5和V3初期历史格式
const object = parseValue(value)
data = object !== undefined ? object : data
} else if (type !== 'String') {
// 兼容App端历史格式 // 兼容App端历史格式
const type = localStorage.getItem(key + STORAGE_DATA_TYPE) try {
if (!type) { data = JSON.parse(value)
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 data = typeof data === 'string' ? JSON.parse(data) : data
} } catch (error) { }
} catch (error) { } }
return { return {
data, data,
errMsg: 'getStorage:ok' errMsg: 'getStorage:ok'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册