Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
41497f8d
U
uni-app
项目概览
DCloud
/
uni-app
4 个月 前同步成功
通知
730
Star
38707
Fork
3642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
7
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
7
Issue
7
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
You need to sign in or sign up before continuing.
提交
41497f8d
编写于
12月 11, 2020
作者:
d-u-a
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update: <unicloud-db> lint
上级
a15b2de8
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
384 addition
and
376 deletion
+384
-376
packages/uni-cli-shared/components/unicloud-db.vue
packages/uni-cli-shared/components/unicloud-db.vue
+384
-376
未找到文件。
packages/uni-cli-shared/components/unicloud-db.vue
浏览文件 @
41497f8d
<
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
{}
}
},
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
export
default
{
name
:
'
UniClouddb
'
,
props
:
{
options
:
{
type
:
[
Object
,
Array
],
default
()
{
return
{}
}
},
data
()
{
return
{
loading
:
false
,
dataList
:
this
.
getone
?
{}
:
[],
paginationInternal
:
{
current
:
this
.
pageCurrent
,
size
:
this
.
pageSize
,
count
:
0
},
errorMessage
:
''
}
collection
:
{
type
:
String
,
default
:
''
},
created
()
{
this
.
_isEnded
=
false
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
}
},
data
()
{
return
{
loading
:
false
,
dataList
:
this
.
getone
?
{}
:
[],
paginationInternal
:
{
current
:
this
.
pageCurrent
,
size
:
this
.
pageSize
,
count
:
0
},
errorMessage
:
''
}
},
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
}
}
if
(
needReset
)
{
this
.
clear
()
this
.
reset
()
}
if
(
newValue
[
0
]
!==
oldValue
[
0
])
{
this
.
paginationInternal
.
current
=
this
.
pageCurrent
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
}
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
)
}
// #endif
window
.
unidev
.
clientDB
.
data
.
push
(
this
.
_debugDataList
)
}
// #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
}
parent
=
parent
.
$parent
maxDepth
--
}
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
--
}
// #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
()
}
if
(
args1
.
current
!==
undefined
)
{
this
.
paginationInternal
.
current
=
args1
.
current
}
if
(
typeof
args2
===
'
function
'
)
{
callback
=
args2
}
}
else
if
(
typeof
args1
===
'
function
'
)
{
callback
=
args1
}
},
// #endif
methods
:
{
loadData
(
args1
,
args2
)
{
let
callback
=
null
if
(
typeof
args1
===
'
object
'
)
{
if
(
args1
.
clear
)
{
this
.
clear
()
this
.
reset
()
}
this
.
_execLoadData
(
callback
)
},
loadMore
()
{
if
(
this
.
_isEnded
)
{
return
if
(
args1
.
current
!==
undefined
)
{
this
.
paginationInternal
.
current
=
args1
.
current
}
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
if
(
typeof
args2
===
'
function
'
)
{
callback
=
args2
}
}
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
},
add
(
value
,
{
toastTitle
,
success
,
fail
,
complete
}
=
{})
{
uni
.
showLoading
()
/* eslint-disable no-undef */
const
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
({
title
:
confirmTitle
||
'
提示
'
,
content
:
confirmContent
||
'
是否删除该数据
'
,
showCancel
:
true
,
success
:
(
res
)
=>
{
if
(
!
res
.
confirm
)
{
return
}
this
.
_execRemove
(
id
,
action
,
success
,
fail
,
complete
)
content
:
err
.
message
,
showCancel
:
false
})
}).
finally
(()
=>
{
uni
.
hideLoading
()
complete
&&
complete
()
})
},
remove
(
id
,
{
action
,
success
,
fail
,
complete
,
confirmTitle
,
confirmContent
}
=
{})
{
if
(
!
id
||
!
id
.
length
)
{
return
}
uni
.
showModal
({
title
:
confirmTitle
||
'
提示
'
,
content
:
confirmContent
||
'
是否删除该数据
'
,
showCancel
:
true
,
success
:
(
res
)
=>
{
if
(
!
res
.
confirm
)
{
return
}
this
.
_execRemove
(
id
,
action
,
success
,
fail
,
complete
)
}
})
},
update
(
id
,
value
,
{
toastTitle
,
success
,
fail
,
complete
}
=
{})
{
uni
.
showLoading
()
/* eslint-disable no-undef */
const
db
=
uniCloud
.
database
()
return
db
.
collection
(
this
.
collection
).
doc
(
id
).
update
(
value
).
then
((
res
)
=>
{
success
&&
success
(
res
)
uni
.
showToast
({
title
:
toastTitle
||
'
修改成功
'
})
},
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
()
}).
catch
((
err
)
=>
{
fail
&&
fail
(
err
)
uni
.
showModal
({
content
:
err
.
message
,
showCancel
:
false
})
},
_execLoadData
(
callback
)
{
if
(
this
.
loading
)
{
return
}
this
.
loading
=
true
this
.
errorMessage
=
''
this
.
_getExec
().
then
((
res
)
=>
{
this
.
loading
=
false
const
{
data
,
count
}
=
res
.
result
this
.
_isEnded
=
data
.
length
<
this
.
pageSize
}).
finally
(()
=>
{
uni
.
hideLoading
()
complete
&&
complete
()
})
},
_execLoadData
(
callback
)
{
if
(
this
.
loading
)
{
return
}
this
.
loading
=
true
this
.
errorMessage
=
''
const
data2
=
this
.
getone
?
(
data
.
length
?
data
[
0
]
:
undefined
)
:
data
this
.
_getExec
().
then
((
res
)
=>
{
this
.
loading
=
false
const
{
data
,
count
}
=
res
.
result
this
.
_isEnded
=
data
.
length
<
this
.
pageSize
callback
&&
callback
(
data2
,
this
.
_isEnded
)
this
.
_dispatchEvent
(
events
.
load
,
data2
)
const
data2
=
this
.
getone
?
(
data
.
length
?
data
[
0
]
:
undefined
)
:
data
if
(
this
.
pageData
===
pageMode
.
add
)
{
this
.
dataList
.
push
(...
data2
)
if
(
this
.
dataList
.
length
)
{
this
.
paginationInternal
.
current
++
}
}
else
{
this
.
dataList
=
data2
}
callback
&&
callback
(
data2
,
this
.
_isEnded
)
this
.
_dispatchEvent
(
events
.
load
,
data2
)
if
(
this
.
getcount
)
{
this
.
paginationInternal
.
count
=
count
if
(
this
.
pageData
===
pageMode
.
add
)
{
this
.
dataList
.
push
(...
data2
)
if
(
this
.
dataList
.
length
)
{
this
.
paginationInternal
.
current
++
}
// #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
.
action
)
{
db
=
db
.
action
(
this
.
action
)
}
else
{
this
.
dataList
=
data2
}
db
=
db
.
collection
(
this
.
collection
)
if
(
!
(
!
this
.
where
||
!
Object
.
keys
(
this
.
where
).
length
))
{
db
=
db
.
where
(
this
.
where
)
if
(
this
.
getcount
)
{
this
.
paginationInternal
.
count
=
count
}
if
(
this
.
field
)
{
db
=
db
.
field
(
this
.
field
)
// #ifdef H5
if
(
process
.
env
.
NODE_ENV
===
'
development
'
)
{
this
.
_debugDataList
.
length
=
0
this
.
_debugDataList
.
push
(...
JSON
.
parse
(
JSON
.
stringify
(
this
.
dataList
)))
}
if
(
this
.
orderby
)
{
db
=
db
.
orderBy
(
this
.
orderby
)
// #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
()
const
{
current
,
size
}
=
this
.
paginationInternal
db
=
db
.
skip
(
size
*
(
current
-
1
)).
limit
(
size
).
get
({
getCount
:
this
.
getcount
})
if
(
this
.
action
)
{
db
=
db
.
action
(
this
.
action
)
}
return
db
},
_execRemove
(
id
,
action
,
success
,
fail
,
complete
)
{
if
(
!
this
.
collection
||
!
id
)
{
return
}
db
=
db
.
collection
(
this
.
collection
)
const
ids
=
Array
.
isArray
(
id
)
?
id
:
[
id
]
if
(
!
ids
.
length
)
{
return
}
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
)
}
uni
.
showLoading
({
mask
:
true
})
const
{
current
,
size
}
=
this
.
paginationInternal
db
=
db
.
skip
(
size
*
(
current
-
1
)).
limit
(
size
).
get
({
getCount
:
this
.
getcount
})
/* eslint-disable no-undef */
const
db
=
uniCloud
.
database
()
const
dbCmd
=
db
.
command
return
db
},
_execRemove
(
id
,
action
,
success
,
fail
,
complete
)
{
if
(
!
this
.
collection
||
!
id
)
{
return
}
let
exec
=
db
if
(
action
)
{
exec
=
exec
.
action
(
action
)
}
const
ids
=
Array
.
isArray
(
id
)
?
id
:
[
id
]
if
(
!
ids
.
length
)
{
return
}
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
()
})
},
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
)
uni
.
showLoading
({
mask
:
true
})
/* eslint-disable no-undef */
const
db
=
uniCloud
.
database
()
const
dbCmd
=
db
.
command
let
exec
=
db
if
(
action
)
{
exec
=
exec
.
action
(
action
)
}
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
.
$emit
(
type
,
data
,
this
.
_isEnded
)
this
.
removeData
(
ids
)
}
}).
catch
((
err
)
=>
{
fail
&&
fail
(
err
)
uni
.
showModal
({
content
:
err
.
message
,
showCancel
:
false
})
}).
finally
(()
=>
{
uni
.
hideLoading
()
complete
&&
complete
()
})
},
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
)
}
}
}
}
</
script
>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录