Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
m0_68066342
uni-app
提交
a15b2de8
U
uni-app
项目概览
m0_68066342
/
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,发现更多精彩内容 >>
提交
a15b2de8
编写于
12月 11, 2020
作者:
d-u-a
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update: <unicloud-db>
上级
8854a105
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
378 addition
and
332 deletion
+378
-332
packages/uni-cli-shared/components/unicloud-db.vue
packages/uni-cli-shared/components/unicloud-db.vue
+378
-332
未找到文件。
packages/uni-cli-shared/components/unicloud-db.vue
浏览文件 @
a15b2de8
<
template
>
<view>
<slot
:options=
"options"
:data=
"dataList"
:pagination=
"paginationInternal"
:loading=
"loading"
:error=
"errorMessage"
/>
<slot
:options=
"options"
:data=
"dataList"
:pagination=
"paginationInternal"
:loading=
"loading"
:error=
"errorMessage"
/>
</view>
</
template
>
<
script
>
const
events
=
{
load
:
'
load
'
,
error
:
'
error
'
}
const
pageMode
=
{
add
:
'
add
'
,
replace
:
'
replace
'
}
const
events
=
{
load
:
'
load
'
,
error
:
'
error
'
}
const
pageMode
=
{
add
:
'
add
'
,
replace
:
'
replace
'
}
const
attrs
=
[
'
pageCurrent
'
,
'
pageSize
'
,
'
collection
'
,
'
action
'
,
'
field
'
,
'
getcount
'
,
'
orderby
'
,
'
where
'
]
const
attrs
=
[
'
pageCurrent
'
,
'
pageSize
'
,
'
collection
'
,
'
action
'
,
'
field
'
,
'
getcount
'
,
'
orderby
'
,
'
where
'
]
export
default
{
name
:
'
UniClouddb
'
,
props
:
{
options
:
{
type
:
[
Object
,
Array
],
default
()
{
return
{}
export
default
{
name
:
'
UniClouddb
'
,
props
:
{
options
:
{
type
:
[
Object
,
Array
],
default
()
{
return
{}
}
},
collection
:
{
type
:
String
,
default
:
''
},
action
:
{
type
:
String
,
default
:
''
},
field
:
{
type
:
String
,
default
:
''
},
pageData
:
{
type
:
String
,
default
:
'
add
'
},
pageCurrent
:
{
type
:
Number
,
default
:
1
},
pageSize
:
{
type
:
Number
,
default
:
20
},
getcount
:
{
type
:
[
Boolean
,
String
],
default
:
false
},
orderby
:
{
type
:
String
,
default
:
''
},
where
:
{
type
:
[
String
,
Object
],
default
:
''
},
getone
:
{
type
:
[
Boolean
,
String
],
default
:
false
},
manual
:
{
type
:
Boolean
,
default
:
false
}
},
collection
:
{
type
:
String
,
default
:
''
},
action
:
{
type
:
String
,
default
:
''
},
field
:
{
type
:
String
,
default
:
''
},
pageData
:
{
type
:
String
,
default
:
'
add
'
},
pageCurrent
:
{
type
:
Number
,
default
:
1
},
pageSize
:
{
type
:
Number
,
default
:
20
},
getcount
:
{
type
:
[
Boolean
,
String
],
default
:
false
},
orderby
:
{
type
:
String
,
default
:
''
},
where
:
{
type
:
[
String
,
Object
],
default
:
''
},
getone
:
{
type
:
[
Boolean
,
String
],
default
:
false
data
()
{
return
{
loading
:
false
,
dataList
:
this
.
getone
?
{}
:
[],
paginationInternal
:
{
current
:
this
.
pageCurrent
,
size
:
this
.
pageSize
,
count
:
0
},
errorMessage
:
''
}
},
manual
:
{
type
:
Boolean
,
default
:
false
}
},
data
()
{
return
{
loading
:
false
,
dataList
:
this
.
getone
?
{}
:
[],
paginationInternal
:
{
current
:
this
.
pageCurrent
,
size
:
this
.
pageSize
,
count
:
0
},
errorMessage
:
''
}
},
created
()
{
this
.
_isEnded
=
false
created
()
{
this
.
_isEnded
=
false
this
.
$watch
(()
=>
{
var
al
=
[]
attrs
.
forEach
(
key
=>
{
al
.
push
(
this
[
key
])
})
return
al
},
(
newValue
,
oldValue
)
=>
{
this
.
paginationInternal
.
pageSize
=
this
.
pageSize
this
.
$watch
(()
=>
{
var
al
=
[]
attrs
.
forEach
(
key
=>
{
al
.
push
(
this
[
key
])
})
return
al
},
(
newValue
,
oldValue
)
=>
{
this
.
paginationInternal
.
pageSize
=
this
.
pageSize
let
needReset
=
false
for
(
let
i
=
2
;
i
<
newValue
.
length
;
i
++
)
{
if
(
newValue
[
i
]
!==
oldValue
[
i
])
{
needReset
=
true
break
let
needReset
=
false
for
(
let
i
=
2
;
i
<
newValue
.
length
;
i
++
)
{
if
(
newValue
[
i
]
!==
oldValue
[
i
])
{
needReset
=
true
break
}
}
if
(
needReset
)
{
this
.
clear
()
this
.
reset
()
}
if
(
newValue
[
0
]
!==
oldValue
[
0
])
{
this
.
paginationInternal
.
current
=
this
.
pageCurrent
}
}
if
(
needReset
)
{
this
.
clear
()
this
.
reset
()
}
if
(
newValue
[
0
]
!==
oldValue
[
0
])
{
this
.
paginationInternal
.
current
=
this
.
pageCurrent
}
this
.
_execLoadData
()
})
this
.
_execLoadData
()
})
// #ifdef H5
if
(
process
.
env
.
NODE_ENV
===
'
development
'
)
{
this
.
_debugDataList
=
[]
if
(
!
window
.
unidev
)
{
window
.
unidev
=
{
clientDB
:
{
data
:
[]
// #ifdef H5
if
(
process
.
env
.
NODE_ENV
===
'
development
'
)
{
this
.
_debugDataList
=
[]
if
(
!
window
.
unidev
)
{
window
.
unidev
=
{
clientDB
:
{
data
:
[]
}
}
}
window
.
unidev
.
clientDB
.
data
.
push
(
this
.
_debugDataList
)
}
window
.
unidev
.
clientDB
.
data
.
push
(
this
.
_debugDataList
)
}
// #endif
// #endif
// #ifdef MP-TOUTIAO
let
changeName
const
events
=
this
.
$scope
.
dataset
.
eventOpts
for
(
var
i
=
0
;
i
<
events
.
length
;
i
++
)
{
const
event
=
events
[
i
]
if
(
event
[
0
].
includes
(
'
^load
'
))
{
changeName
=
event
[
1
][
0
][
0
]
// #ifdef MP-TOUTIAO
let
changeName
const
events
=
this
.
$scope
.
dataset
.
eventOpts
for
(
var
i
=
0
;
i
<
events
.
length
;
i
++
)
{
const
event
=
events
[
i
]
if
(
event
[
0
].
includes
(
'
^load
'
))
{
changeName
=
event
[
1
][
0
][
0
]
}
}
}
if
(
changeName
)
{
let
parent
=
this
.
$parent
let
maxDepth
=
16
this
.
_changeDataFunction
=
null
while
(
parent
&&
maxDepth
>
0
)
{
const
fun
=
parent
[
changeName
]
if
(
fun
&&
typeof
fun
===
'
function
'
)
{
this
.
_changeDataFunction
=
fun
maxDepth
=
0
break
if
(
changeName
)
{
let
parent
=
this
.
$parent
let
maxDepth
=
16
this
.
_changeDataFunction
=
null
while
(
parent
&&
maxDepth
>
0
)
{
const
fun
=
parent
[
changeName
]
if
(
fun
&&
typeof
fun
===
'
function
'
)
{
this
.
_changeDataFunction
=
fun
maxDepth
=
0
break
}
parent
=
parent
.
$parent
maxDepth
--
}
parent
=
parent
.
$parent
maxDepth
--
}
}
// #endif
// #endif
if
(
!
this
.
manual
)
{
this
.
loadData
()
}
},
// #ifdef H5
beforeDestroy
()
{
if
(
process
.
env
.
NODE_ENV
===
'
development
'
&&
window
.
unidev
)
{
var
cd
=
this
.
_debugDataList
var
dl
=
window
.
unidev
.
clientDB
.
data
for
(
var
i
=
dl
.
length
-
1
;
i
>=
0
;
i
--
)
{
if
(
dl
[
i
]
===
cd
)
{
dl
.
splice
(
i
,
1
)
break
if
(
!
this
.
manual
)
{
this
.
loadData
()
}
},
// #ifdef H5
beforeDestroy
()
{
if
(
process
.
env
.
NODE_ENV
===
'
development
'
&&
window
.
unidev
)
{
var
cd
=
this
.
_debugDataList
var
dl
=
window
.
unidev
.
clientDB
.
data
for
(
var
i
=
dl
.
length
-
1
;
i
>=
0
;
i
--
)
{
if
(
dl
[
i
]
===
cd
)
{
dl
.
splice
(
i
,
1
)
break
}
}
}
}
},
// #endif
methods
:
{
loadData
(
args1
,
args2
)
{
let
callback
=
null
if
(
typeof
args1
===
'
object
'
)
{
if
(
args1
.
clear
)
{
this
.
clear
()
this
.
reset
()
},
// #endif
methods
:
{
loadData
(
args1
,
args2
)
{
let
callback
=
null
if
(
typeof
args1
===
'
object
'
)
{
if
(
args1
.
clear
)
{
this
.
clear
()
this
.
reset
()
}
if
(
args1
.
current
!==
undefined
)
{
this
.
paginationInternal
.
current
=
args1
.
current
}
if
(
typeof
args2
===
'
function
'
)
{
callback
=
args2
}
}
else
if
(
typeof
args1
===
'
function
'
)
{
callback
=
args1
}
if
(
args1
.
current
!==
undefined
)
{
this
.
paginationInternal
.
current
=
args1
.
current
this
.
_execLoadData
(
callback
)
},
loadMore
()
{
if
(
this
.
_isEnded
)
{
return
}
if
(
typeof
args2
===
'
function
'
)
{
callback
=
args2
this
.
_execLoadData
()
},
refresh
()
{
this
.
clear
()
this
.
_execLoadData
()
},
clear
()
{
this
.
_isEnded
=
false
this
.
dataList
=
[]
},
reset
()
{
this
.
paginationInternal
.
current
=
1
},
add
(
value
,
{
toastTitle
,
success
,
fail
,
complete
}
=
{})
{
uni
.
showLoading
()
let
db
=
uniCloud
.
database
()
db
.
collection
(
this
.
collection
).
add
(
value
).
then
((
res
)
=>
{
success
&&
success
(
res
)
uni
.
showToast
({
title
:
toastTitle
||
'
新增成功
'
})
}).
catch
((
err
)
=>
{
fail
&&
fail
(
err
)
uni
.
showModal
({
content
:
err
.
message
,
showCancel
:
false
})
}).
finally
(()
=>
{
uni
.
hideLoading
()
complete
&&
complete
()
})
},
remove
(
id
,
{
action
,
success
,
fail
,
complete
,
confirmTitle
,
confirmContent
}
=
{})
{
if
(
!
id
||
!
id
.
length
)
{
return
}
}
else
if
(
typeof
args1
===
'
function
'
)
{
callback
=
args1
}
this
.
_execLoadData
(
callback
)
},
loadMore
()
{
if
(
this
.
_isEnded
)
{
return
}
this
.
_execLoadData
()
},
refresh
()
{
this
.
clear
()
this
.
_execLoadData
()
},
clear
()
{
this
.
_isEnded
=
false
this
.
dataList
=
[]
},
reset
()
{
this
.
paginationInternal
.
current
=
1
},
remove
(
id
,
{
action
,
callback
,
confirmTitle
,
confirmContent
}
=
{})
{
if
(
!
id
||
!
id
.
length
)
{
return
}
uni
.
showModal
({
title
:
confirmTitle
||
'
提示
'
,
content
:
confirmContent
||
'
是否删除该数据
'
,
showCancel
:
true
,
success
:
(
res
)
=>
{
if
(
!
res
.
confirm
)
{
return
uni
.
showModal
({
title
:
confirmTitle
||
'
提示
'
,
content
:
confirmContent
||
'
是否删除该数据
'
,
showCancel
:
true
,
success
:
(
res
)
=>
{
if
(
!
res
.
confirm
)
{
return
}
this
.
_execRemove
(
id
,
action
,
success
,
fail
,
complete
)
}
this
.
_execRemove
(
id
,
action
,
callback
)
})
},
update
(
id
,
value
,
{
toastTitle
,
success
,
fail
,
complete
}
=
{})
{
uni
.
showLoading
()
let
db
=
uniCloud
.
database
()
return
db
.
collection
(
this
.
collection
).
doc
(
id
).
update
(
value
).
then
((
res
)
=>
{
success
&&
success
(
res
)
uni
.
showToast
({
title
:
toastTitle
||
'
修改成功
'
})
}).
catch
((
err
)
=>
{
fail
&&
fail
(
err
)
uni
.
showModal
({
content
:
err
.
message
,
showCancel
:
false
})
}).
finally
(()
=>
{
uni
.
hideLoading
()
complete
&&
complete
()
})
},
_execLoadData
(
callback
)
{
if
(
this
.
loading
)
{
return
}
})
},
_execLoadData
(
callback
)
{
if
(
this
.
loading
)
{
return
}
this
.
loading
=
true
this
.
errorMessage
=
''
this
.
loading
=
true
this
.
errorMessage
=
''
this
.
_getExec
().
then
((
res
)
=>
{
this
.
loading
=
false
const
{
data
,
count
}
=
res
.
result
this
.
_isEnded
=
data
.
length
<
this
.
pageSize
this
.
_getExec
().
then
((
res
)
=>
{
this
.
loading
=
false
const
{
data
,
count
}
=
res
.
result
this
.
_isEnded
=
data
.
length
<
this
.
pageSize
const
data2
=
this
.
getone
?
(
data
.
length
?
data
[
0
]
:
undefined
)
:
data
const
data2
=
this
.
getone
?
(
data
.
length
?
data
[
0
]
:
undefined
)
:
data
callback
&&
callback
(
data2
,
this
.
_isEnded
)
this
.
_dispatchEvent
(
events
.
load
,
data2
)
callback
&&
callback
(
data2
,
this
.
_isEnded
)
this
.
_dispatchEvent
(
events
.
load
,
data2
)
if
(
this
.
pageData
===
pageMode
.
add
)
{
this
.
dataList
.
push
(...
data2
)
if
(
this
.
dataList
.
length
)
{
this
.
paginationInternal
.
current
++
if
(
this
.
pageData
===
pageMode
.
add
)
{
this
.
dataList
.
push
(...
data2
)
if
(
this
.
dataList
.
length
)
{
this
.
paginationInternal
.
current
++
}
}
else
{
this
.
dataList
=
data2
}
}
else
{
this
.
dataList
=
data2
}
if
(
this
.
getcount
)
{
this
.
paginationInternal
.
count
=
count
}
// #ifdef H5
if
(
process
.
env
.
NODE_ENV
===
'
development
'
)
{
this
.
_debugDataList
.
length
=
0
this
.
_debugDataList
.
push
(...
JSON
.
parse
(
JSON
.
stringify
(
this
.
dataList
)))
}
// #endif
}).
catch
((
err
)
=>
{
this
.
loading
=
false
this
.
errorMessage
=
err
callback
&&
callback
()
this
.
$emit
(
events
.
error
,
err
)
if
(
process
.
env
.
NODE_ENV
===
'
development
'
)
{
console
.
error
(
err
)
}
})
},
_getExec
()
{
/* eslint-disable no-undef */
let
db
=
uniCloud
.
database
()
if
(
this
.
getcount
)
{
this
.
paginationInternal
.
count
=
count
}
if
(
this
.
action
)
{
db
=
db
.
action
(
this
.
action
)
}
// #ifdef H5
if
(
process
.
env
.
NODE_ENV
===
'
development
'
)
{
this
.
_debugDataList
.
length
=
0
this
.
_debugDataList
.
push
(...
JSON
.
parse
(
JSON
.
stringify
(
this
.
dataList
)))
}
// #endif
}).
catch
((
err
)
=>
{
this
.
loading
=
false
this
.
errorMessage
=
err
callback
&&
callback
()
this
.
$emit
(
events
.
error
,
err
)
if
(
process
.
env
.
NODE_ENV
===
'
development
'
)
{
console
.
error
(
err
)
}
})
},
_getExec
()
{
/* eslint-disable no-undef */
let
db
=
uniCloud
.
database
()
db
=
db
.
collection
(
this
.
collection
)
if
(
this
.
action
)
{
db
=
db
.
action
(
this
.
action
)
}
if
(
!
(
!
this
.
where
||
!
Object
.
keys
(
this
.
where
).
length
))
{
db
=
db
.
where
(
this
.
where
)
}
if
(
this
.
field
)
{
db
=
db
.
field
(
this
.
field
)
}
if
(
this
.
orderby
)
{
db
=
db
.
orderBy
(
this
.
orderby
)
}
db
=
db
.
collection
(
this
.
collection
)
const
{
current
,
size
}
=
this
.
paginationInternal
db
=
db
.
skip
(
size
*
(
current
-
1
)).
limit
(
size
).
get
({
getCount
:
this
.
getcount
})
if
(
!
(
!
this
.
where
||
!
Object
.
keys
(
this
.
where
).
length
))
{
db
=
db
.
where
(
this
.
where
)
}
if
(
this
.
field
)
{
db
=
db
.
field
(
this
.
field
)
}
if
(
this
.
orderby
)
{
db
=
db
.
orderBy
(
this
.
orderby
)
}
return
db
},
_execRemove
(
id
,
action
,
callback
)
{
if
(
!
this
.
collection
||
!
id
)
{
return
}
const
{
current
,
size
}
=
this
.
paginationInternal
db
=
db
.
skip
(
size
*
(
current
-
1
)).
limit
(
size
).
get
({
getCount
:
this
.
getcount
})
const
ids
=
Array
.
isArray
(
id
)
?
id
:
[
id
]
if
(
!
ids
.
length
)
{
return
}
return
db
},
_execRemove
(
id
,
action
,
success
,
fail
,
complete
)
{
if
(
!
this
.
collection
||
!
id
)
{
return
}
uni
.
showLoading
({
mask
:
true
})
const
ids
=
Array
.
isArray
(
id
)
?
id
:
[
id
]
if
(
!
ids
.
length
)
{
return
}
/* eslint-disable no-undef */
const
db
=
uniCloud
.
database
()
const
dbCmd
=
db
.
command
uni
.
showLoading
({
mask
:
true
})
let
exec
=
db
if
(
action
)
{
exec
=
exec
.
action
(
action
)
}
/* eslint-disable no-undef */
const
db
=
uniCloud
.
database
()
const
dbCmd
=
db
.
command
exec
.
collection
(
this
.
collection
).
where
({
_id
:
dbCmd
.
in
(
ids
)
}).
remove
().
then
((
res
)
=>
{
callback
&&
callback
(
res
.
result
)
if
(
this
.
pageData
===
pageMode
.
replace
)
{
this
.
refresh
()
}
else
{
this
.
removeData
(
ids
)
let
exec
=
db
if
(
action
)
{
exec
=
exec
.
action
(
action
)
}
}).
catch
((
err
)
=>
{
uni
.
showModal
({
content
:
err
.
message
,
showCancel
:
false
exec
.
collection
(
this
.
collection
).
where
({
_id
:
dbCmd
.
in
(
ids
)
}).
remove
().
then
((
res
)
=>
{
success
&&
success
(
res
.
result
)
if
(
this
.
pageData
===
pageMode
.
replace
)
{
this
.
refresh
()
}
else
{
this
.
removeData
(
ids
)
}
}).
catch
((
err
)
=>
{
fail
&&
fail
(
err
)
uni
.
showModal
({
content
:
err
.
message
,
showCancel
:
false
})
}).
finally
(()
=>
{
uni
.
hideLoading
()
complete
&&
complete
()
})
}).
finally
(()
=>
{
uni
.
hideLoading
()
})
},
removeData
(
ids
)
{
const
il
=
ids
.
slice
(
0
)
const
dl
=
this
.
dataList
for
(
let
i
=
dl
.
length
-
1
;
i
>=
0
;
i
--
)
{
const
index
=
il
.
indexOf
(
dl
[
i
].
_id
)
if
(
index
>=
0
)
{
dl
.
splice
(
i
,
1
)
il
.
splice
(
index
,
1
)
},
removeData
(
ids
)
{
const
il
=
ids
.
slice
(
0
)
const
dl
=
this
.
dataList
for
(
let
i
=
dl
.
length
-
1
;
i
>=
0
;
i
--
)
{
const
index
=
il
.
indexOf
(
dl
[
i
].
_id
)
if
(
index
>=
0
)
{
dl
.
splice
(
i
,
1
)
il
.
splice
(
index
,
1
)
}
}
},
_dispatchEvent
(
type
,
data
)
{
if
(
this
.
_changeDataFunction
)
{
this
.
_changeDataFunction
(
data
,
this
.
_isEnded
)
}
else
{
this
.
$emit
(
type
,
data
,
this
.
_isEnded
)
}
}
},
_dispatchEvent
(
type
,
data
)
{
if
(
this
.
_changeDataFunction
)
{
this
.
_changeDataFunction
(
data
,
this
.
_isEnded
)
}
else
{
this
.
$emit
(
type
,
data
,
this
.
_isEnded
)
}
}
}
}
</
script
>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录