Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
erosXXX
uni-app
提交
a849158f
U
uni-app
项目概览
erosXXX
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a849158f
编写于
3月 17, 2020
作者:
雪洛
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'storage-refactor' into dev
上级
99abf182
6c7ab04f
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
349 addition
and
153 deletion
+349
-153
build/rollup.config.app.js
build/rollup.config.app.js
+0
-1
src/platforms/app-plus/service/api/index.js
src/platforms/app-plus/service/api/index.js
+2
-0
src/platforms/app-plus/service/api/storage/storage.js
src/platforms/app-plus/service/api/storage/storage.js
+195
-0
src/platforms/h5/service/api/storage/storage.js
src/platforms/h5/service/api/storage/storage.js
+152
-152
未找到文件。
build/rollup.config.app.js
浏览文件 @
a849158f
...
...
@@ -35,7 +35,6 @@ if (process.env.UNI_SERVICE === 'legacy') {
output
.
name
=
'
serviceContext
'
output
.
banner
=
`export function createServiceContext(Vue, weex, plus, UniServiceJSBridge,instanceContext){
var localStorage = plus.storage
var setTimeout = instanceContext.setTimeout
var clearTimeout = instanceContext.clearTimeout
var setInterval = instanceContext.setInterval
...
...
src/platforms/app-plus/service/api/index.js
浏览文件 @
a849158f
...
...
@@ -57,6 +57,8 @@ export * from './route/re-launch'
export
*
from
'
./route/redirect-to
'
export
*
from
'
./route/switch-tab
'
export
*
from
'
./storage/storage
'
export
*
from
'
./ui/keyboard
'
export
*
from
'
./ui/navigation-bar
'
export
*
from
'
./ui/popup
'
...
...
src/platforms/app-plus/service/api/storage/storage.js
0 → 100644
浏览文件 @
a849158f
import
{
invoke
}
from
'
../../bridge
'
const
STORAGE_DATA_TYPE
=
'
__TYPE
'
const
STORAGE_KEYS
=
'
uni-storage-keys
'
function
parseValue
(
value
)
{
const
types
=
[
'
object
'
,
'
string
'
,
'
number
'
,
'
boolean
'
,
'
undefined
'
]
try
{
const
object
=
typeof
value
===
'
string
'
?
JSON
.
parse
(
value
)
:
value
const
type
=
object
.
type
if
(
types
.
indexOf
(
type
)
>=
0
)
{
const
keys
=
Object
.
keys
(
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
)
{
return
''
}
}
}
catch
(
error
)
{}
}
export
function
setStorage
({
key
,
data
,
isSync
}
=
{},
callbackId
)
{
const
type
=
typeof
data
const
value
=
type
===
'
string
'
?
data
:
JSON
.
stringify
({
type
,
data
:
data
})
try
{
if
(
type
===
'
string
'
&&
parseValue
(
value
)
!==
undefined
)
{
plus
.
storage
.
setItemAsync
(
key
+
STORAGE_DATA_TYPE
,
type
)
}
else
{
plus
.
storage
.
removeItemAsync
(
key
+
STORAGE_DATA_TYPE
)
}
plus
.
storage
.
setItemAsync
(
key
,
value
,
function
()
{
invoke
(
callbackId
,
{
errMsg
:
'
setStorage:ok
'
})
},
function
(
err
)
{
invoke
(
callbackId
,
{
errMsg
:
`setStorage:fail
${
err
.
message
}
`
})
})
}
catch
(
error
)
{
invoke
(
callbackId
,
{
errMsg
:
`setStorage:fail
${
error
}
`
})
}
}
export
function
setStorageSync
(
key
,
data
)
{
const
type
=
typeof
data
const
value
=
type
===
'
string
'
?
data
:
JSON
.
stringify
({
type
,
data
:
data
})
try
{
if
(
type
===
'
string
'
&&
parseValue
(
value
)
!==
undefined
)
{
plus
.
storage
.
setItem
(
key
+
STORAGE_DATA_TYPE
,
type
)
}
else
{
plus
.
storage
.
removeItem
(
key
+
STORAGE_DATA_TYPE
)
}
plus
.
storage
.
setItem
(
key
,
value
)
}
catch
(
error
)
{
}
}
function
parseGetStorage
(
type
,
value
)
{
let
data
=
value
if
(
type
!==
'
string
'
||
(
type
===
'
string
'
&&
value
===
'
{"type":"undefined"}
'
))
{
try
{
// 兼容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
data
}
export
function
getStorage
({
key
}
=
{},
callbackId
)
{
plus
.
storage
.
getItemAsync
(
key
,
function
(
res
)
{
plus
.
storage
.
getItemAsync
(
key
+
STORAGE_DATA_TYPE
,
function
(
typeRes
)
{
const
typeOrigin
=
typeRes
.
data
||
''
const
type
=
typeOrigin
.
toLowerCase
()
invoke
(
callbackId
,
{
data
:
parseGetStorage
(
type
,
res
.
data
),
errMsg
:
'
getStorage:ok
'
})
},
function
()
{
const
type
=
''
invoke
(
callbackId
,
{
data
:
parseGetStorage
(
type
,
res
.
data
),
errMsg
:
'
getStorage:ok
'
})
})
},
function
(
err
)
{
invoke
(
callbackId
,
{
data
:
''
,
errMsg
:
`getStorage:fail
${
err
.
message
}
`
})
})
}
export
function
getStorageSync
(
key
)
{
const
value
=
plus
.
storage
.
getItem
(
key
)
const
typeOrigin
=
plus
.
storage
.
getItem
(
key
+
STORAGE_DATA_TYPE
)
||
''
const
type
=
typeOrigin
.
toLowerCase
()
if
(
typeof
value
!==
'
string
'
)
{
return
''
}
return
parseGetStorage
(
type
,
value
)
}
export
function
removeStorage
({
key
}
=
{},
callbackId
)
{
// 兼容App端历史格式
plus
.
storage
.
removeItemAsync
(
key
+
STORAGE_DATA_TYPE
)
plus
.
storage
.
removeItemAsync
(
key
,
function
(
res
)
{
invoke
(
callbackId
,
{
errMsg
:
'
removeStorage:ok
'
})
},
function
(
err
)
{
invoke
(
callbackId
,
{
errMsg
:
`removeStorage:fail
${
err
.
message
}
`
})
})
}
export
function
removeStorageSync
(
key
)
{
plus
.
storage
.
removeItem
(
key
+
STORAGE_DATA_TYPE
)
plus
.
storage
.
removeItem
(
key
)
}
export
function
clearStorage
(
args
,
callbackId
)
{
plus
.
storage
.
clearAsync
(
function
(
res
)
{
invoke
(
callbackId
,
{
errMsg
:
'
clearStorage:ok
'
})
},
function
(
err
)
{
invoke
(
callbackId
,
{
errMsg
:
`clearStorage:fail
${
err
.
message
}
`
})
})
}
export
function
clearStorageSync
()
{
plus
.
storage
.
clear
()
}
export
function
getStorageInfo
()
{
const
length
=
(
plus
.
storage
.
length
||
plus
.
storage
.
getLength
())
||
0
const
keys
=
[]
let
currentSize
=
0
for
(
let
index
=
0
;
index
<
length
;
index
++
)
{
const
key
=
plus
.
storage
.
key
(
index
)
if
(
key
!==
STORAGE_KEYS
&&
key
.
indexOf
(
STORAGE_DATA_TYPE
)
+
STORAGE_DATA_TYPE
.
length
!==
key
.
length
)
{
const
value
=
plus
.
storage
.
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
'
}
}
export
function
getStorageInfoSync
()
{
const
res
=
getStorageInfo
()
delete
res
.
errMsg
return
res
}
src/
core
/service/api/storage/storage.js
→
src/
platforms/h5
/service/api/storage/storage.js
浏览文件 @
a849158f
const
STORAGE_DATA_TYPE
=
'
__TYPE
'
const
STORAGE_KEYS
=
'
uni-storage-keys
'
function
parseValue
(
value
)
{
const
types
=
[
'
object
'
,
'
string
'
,
'
number
'
,
'
boolean
'
,
'
undefined
'
]
try
{
const
object
=
typeof
value
===
'
string
'
?
JSON
.
parse
(
value
)
:
value
const
type
=
object
.
type
if
(
types
.
indexOf
(
type
)
>=
0
)
{
const
keys
=
Object
.
keys
(
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
)
{
return
''
}
}
}
catch
(
error
)
{
}
}
export
function
setStorage
({
key
,
data
}
=
{})
{
const
type
=
typeof
data
const
value
=
type
===
'
string
'
?
data
:
JSON
.
stringify
({
type
,
data
:
data
})
try
{
if
(
type
===
'
string
'
&&
parseValue
(
value
)
!==
undefined
)
{
localStorage
.
setItem
(
key
+
STORAGE_DATA_TYPE
,
type
)
}
else
{
localStorage
.
removeItem
(
key
+
STORAGE_DATA_TYPE
)
}
localStorage
.
setItem
(
key
,
value
)
}
catch
(
error
)
{
return
{
errMsg
:
`setStorage:fail
${
error
}
`
}
}
return
{
errMsg
:
'
setStorage:ok
'
}
}
export
function
setStorageSync
(
key
,
data
)
{
setStorage
({
key
,
data
})
}
export
function
getStorage
({
key
}
=
{})
{
const
value
=
localStorage
&&
localStorage
.
getItem
(
key
)
if
(
typeof
value
!==
'
string
'
)
{
return
{
data
:
''
,
errMsg
:
'
getStorage:fail
'
}
}
let
data
=
value
const
typeOrigin
=
localStorage
.
getItem
(
key
+
STORAGE_DATA_TYPE
)
||
''
const
type
=
typeOrigin
.
toLowerCase
()
if
(
type
!==
'
string
'
||
(
typeOrigin
===
'
String
'
&&
value
===
'
{"type":"undefined"}
'
))
{
try
{
// 兼容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
{
data
,
errMsg
:
'
getStorage:ok
'
}
}
export
function
getStorageSync
(
key
)
{
const
res
=
getStorage
({
key
})
return
res
.
data
}
export
function
removeStorage
({
key
}
=
{})
{
if
(
localStorage
)
{
// 兼容App端历史格式
localStorage
.
removeItem
(
key
+
STORAGE_DATA_TYPE
)
localStorage
.
removeItem
(
key
)
}
return
{
errMsg
:
'
removeStorage:ok
'
}
}
export
function
removeStorageSync
(
key
)
{
removeStorage
({
key
})
}
export
function
clearStorage
()
{
localStorage
&&
localStorage
.
clear
()
return
{
errMsg
:
'
clearStorage:ok
'
}
}
export
function
clearStorageSync
()
{
clearStorage
()
}
export
function
getStorageInfo
()
{
const
length
=
(
localStorage
&&
(
localStorage
.
length
||
localStorage
.
getLength
()))
||
0
const
keys
=
[]
let
currentSize
=
0
for
(
let
index
=
0
;
index
<
length
;
index
++
)
{
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
'
}
}
export
function
getStorageInfoSync
()
{
const
res
=
getStorageInfo
()
delete
res
.
errMsg
return
res
}
const
STORAGE_DATA_TYPE
=
'
__TYPE
'
const
STORAGE_KEYS
=
'
uni-storage-keys
'
function
parseValue
(
value
)
{
const
types
=
[
'
object
'
,
'
string
'
,
'
number
'
,
'
boolean
'
,
'
undefined
'
]
try
{
const
object
=
typeof
value
===
'
string
'
?
JSON
.
parse
(
value
)
:
value
const
type
=
object
.
type
if
(
types
.
indexOf
(
type
)
>=
0
)
{
const
keys
=
Object
.
keys
(
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
)
{
return
''
}
}
}
catch
(
error
)
{
}
}
export
function
setStorage
({
key
,
data
}
=
{})
{
const
type
=
typeof
data
const
value
=
type
===
'
string
'
?
data
:
JSON
.
stringify
({
type
,
data
:
data
})
try
{
if
(
type
===
'
string
'
&&
parseValue
(
value
)
!==
undefined
)
{
localStorage
.
setItem
(
key
+
STORAGE_DATA_TYPE
,
type
)
}
else
{
localStorage
.
removeItem
(
key
+
STORAGE_DATA_TYPE
)
}
localStorage
.
setItem
(
key
,
value
)
}
catch
(
error
)
{
return
{
errMsg
:
`setStorage:fail
${
error
}
`
}
}
return
{
errMsg
:
'
setStorage:ok
'
}
}
export
function
setStorageSync
(
key
,
data
)
{
setStorage
({
key
,
data
})
}
export
function
getStorage
({
key
}
=
{})
{
const
value
=
localStorage
&&
localStorage
.
getItem
(
key
)
if
(
typeof
value
!==
'
string
'
)
{
return
{
data
:
''
,
errMsg
:
'
getStorage:fail
'
}
}
let
data
=
value
const
typeOrigin
=
localStorage
.
getItem
(
key
+
STORAGE_DATA_TYPE
)
||
''
const
type
=
typeOrigin
.
toLowerCase
()
if
(
type
!==
'
string
'
||
(
typeOrigin
===
'
String
'
&&
value
===
'
{"type":"undefined"}
'
))
{
try
{
// 兼容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
{
data
,
errMsg
:
'
getStorage:ok
'
}
}
export
function
getStorageSync
(
key
)
{
const
res
=
getStorage
({
key
})
return
res
.
data
}
export
function
removeStorage
({
key
}
=
{})
{
if
(
localStorage
)
{
// 兼容App端历史格式
localStorage
.
removeItem
(
key
+
STORAGE_DATA_TYPE
)
localStorage
.
removeItem
(
key
)
}
return
{
errMsg
:
'
removeStorage:ok
'
}
}
export
function
removeStorageSync
(
key
)
{
removeStorage
({
key
})
}
export
function
clearStorage
()
{
localStorage
&&
localStorage
.
clear
()
return
{
errMsg
:
'
clearStorage:ok
'
}
}
export
function
clearStorageSync
()
{
clearStorage
()
}
export
function
getStorageInfo
()
{
const
length
=
(
localStorage
&&
(
localStorage
.
length
||
localStorage
.
getLength
()))
||
0
const
keys
=
[]
let
currentSize
=
0
for
(
let
index
=
0
;
index
<
length
;
index
++
)
{
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
'
}
}
export
function
getStorageInfoSync
()
{
const
res
=
getStorageInfo
()
delete
res
.
errMsg
return
res
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录