提交 65e1d7d0 编写于 作者: Q qiang

Merge branch 'dev' into alpha

<template> <template>
<view> <view>
<slot <slot :options="options" :data="dataList" :pagination="paginationInternal" :loading="loading" :error="errorMessage" />
:options="options"
:data="dataList"
:pagination="paginationInternal"
:loading="loading"
:error="errorMessage"
/>
</view> </view>
</template> </template>
<script> <script>
const events = { const events = {
load: 'load', load: 'load',
error: 'error' error: 'error'
} }
const pageMode = { const pageMode = {
add: 'add', add: 'add',
replace: 'replace' replace: 'replace'
} }
const attrs = [ const attrs = [
'pageCurrent', 'pageCurrent',
'pageSize', 'pageSize',
'collection', 'collection',
'action', 'action',
'field', 'field',
'getcount', 'getcount',
'orderby', 'orderby',
'where' 'where'
] ]
export default { export default {
name: 'UniClouddb', name: 'UniClouddb',
props: { props: {
options: { options: {
type: [Object, Array], type: [Object, Array],
default () { default () {
return {} 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: { data() {
type: String, return {
default: '' loading: false,
}, dataList: this.getone ? {} : [],
action: { paginationInternal: {
type: String, current: this.pageCurrent,
default: '' size: this.pageSize,
}, count: 0
field: { },
type: String, errorMessage: ''
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: { created() {
type: Boolean, this._isEnded = false
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(() => { this.$watch(() => {
var al = [] var al = []
attrs.forEach(key => { attrs.forEach(key => {
al.push(this[key]) al.push(this[key])
}) })
return al return al
}, (newValue, oldValue) => { }, (newValue, oldValue) => {
this.paginationInternal.pageSize = this.pageSize this.paginationInternal.pageSize = this.pageSize
let needReset = false let needReset = false
for (let i = 2; i < newValue.length; i++) { for (let i = 2; i < newValue.length; i++) {
if (newValue[i] !== oldValue[i]) { if (newValue[i] !== oldValue[i]) {
needReset = true needReset = true
break 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 // #ifdef H5
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
this._debugDataList = [] this._debugDataList = []
if (!window.unidev) { if (!window.unidev) {
window.unidev = { window.unidev = {
clientDB: { clientDB: {
data: [] data: []
}
} }
} }
window.unidev.clientDB.data.push(this._debugDataList)
} }
window.unidev.clientDB.data.push(this._debugDataList) // #endif
}
// #endif
// #ifdef MP-TOUTIAO // #ifdef MP-TOUTIAO
let changeName let changeName
const events = this.$scope.dataset.eventOpts const events = this.$scope.dataset.eventOpts
for (var i = 0; i < events.length; i++) { for (var i = 0; i < events.length; i++) {
const event = events[i] const event = events[i]
if (event[0].includes('^load')) { if (event[0].includes('^load')) {
changeName = event[1][0][0] changeName = event[1][0][0]
}
} }
} if (changeName) {
if (changeName) { let parent = this.$parent
let parent = this.$parent let maxDepth = 16
let maxDepth = 16 this._changeDataFunction = null
this._changeDataFunction = null while (parent && maxDepth > 0) {
while (parent && maxDepth > 0) { const fun = parent[changeName]
const fun = parent[changeName] if (fun && typeof fun === 'function') {
if (fun && typeof fun === 'function') { this._changeDataFunction = fun
this._changeDataFunction = fun maxDepth = 0
maxDepth = 0 break
break }
parent = parent.$parent
maxDepth--
} }
parent = parent.$parent
maxDepth--
} }
} // #endif
// #endif
if (!this.manual) { if (!this.manual) {
this.loadData() this.loadData()
} }
}, },
// #ifdef H5 // #ifdef H5
beforeDestroy () { beforeDestroy() {
if (process.env.NODE_ENV === 'development' && window.unidev) { if (process.env.NODE_ENV === 'development' && window.unidev) {
var cd = this._debugDataList var cd = this._debugDataList
var dl = window.unidev.clientDB.data var dl = window.unidev.clientDB.data
for (var i = dl.length - 1; i >= 0; i--) { for (var i = dl.length - 1; i >= 0; i--) {
if (dl[i] === cd) { if (dl[i] === cd) {
dl.splice(i, 1) dl.splice(i, 1)
break break
}
} }
} }
} },
}, // #endif
// #endif methods: {
methods: { loadData(args1, args2) {
loadData (args1, args2) { let callback = null
let callback = null if (typeof args1 === 'object') {
if (typeof args1 === 'object') { if (args1.clear) {
if (args1.clear) { this.clear()
this.clear() this.reset()
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') { this._execLoadData()
callback = args2 },
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') { uni.showModal({
callback = args1 title: confirmTitle || '提示',
} content: confirmContent || '是否删除该数据',
showCancel: true,
this._execLoadData(callback) success: (res) => {
}, if (!res.confirm) {
loadMore () { return
if (this._isEnded) { }
return this._execRemove(id, action, success, fail, complete)
}
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
} }
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
} }
}) this.loading = true
}, this.errorMessage = ''
_execLoadData (callback) {
if (this.loading) {
return
}
this.loading = true
this.errorMessage = ''
this._getExec().then((res) => { this._getExec().then((res) => {
this.loading = false this.loading = false
const { const {
data, data,
count count
} = res.result } = res.result
this._isEnded = data.length < this.pageSize 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) callback && callback(data2, this._isEnded)
this._dispatchEvent(events.load, data2) this._dispatchEvent(events.load, data2)
if (this.pageData === pageMode.add) { if (this.pageData === pageMode.add) {
this.dataList.push(...data2) this.dataList.push(...data2)
if (this.dataList.length) { if (this.dataList.length) {
this.paginationInternal.current++ this.paginationInternal.current++
}
} else {
this.dataList = data2
} }
} else {
this.dataList = data2
}
if (this.getcount) {
this.paginationInternal.count = count
}
// #ifdef H5 if (this.getcount) {
if (process.env.NODE_ENV === 'development') { this.paginationInternal.count = count
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) { // #ifdef H5
db = db.action(this.action) 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.collection(this.collection)
db = db.where(this.where)
}
if (this.field) {
db = db.field(this.field)
}
if (this.orderby) {
db = db.orderBy(this.orderby)
}
const { if (!(!this.where || !Object.keys(this.where).length)) {
current, db = db.where(this.where)
size }
} = this.paginationInternal if (this.field) {
db = db.skip(size * (current - 1)).limit(size).get({ db = db.field(this.field)
getCount: this.getcount }
}) if (this.orderby) {
db = db.orderBy(this.orderby)
}
return db const {
}, current,
_execRemove (id, action, callback) { size
if (!this.collection || !id) { } = this.paginationInternal
return db = db.skip(size * (current - 1)).limit(size).get({
} getCount: this.getcount
})
const ids = Array.isArray(id) ? id : [id] return db
if (!ids.length) { },
return _execRemove(id, action, success, fail, complete) {
} if (!this.collection || !id) {
return
}
uni.showLoading({ const ids = Array.isArray(id) ? id : [id]
mask: true if (!ids.length) {
}) return
}
/* eslint-disable no-undef */ uni.showLoading({
const db = uniCloud.database() mask: true
const dbCmd = db.command })
let exec = db /* eslint-disable no-undef */
if (action) { const db = uniCloud.database()
exec = exec.action(action) const dbCmd = db.command
}
exec.collection(this.collection).where({ let exec = db
_id: dbCmd.in(ids) if (action) {
}).remove().then((res) => { exec = exec.action(action)
callback && callback(res.result)
if (this.pageData === pageMode.replace) {
this.refresh()
} else {
this.removeData(ids)
} }
}).catch((err) => {
uni.showModal({ exec.collection(this.collection).where({
content: err.message, _id: dbCmd.in(ids)
showCancel: false }).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
removeData (ids) { for (let i = dl.length - 1; i >= 0; i--) {
const il = ids.slice(0) const index = il.indexOf(dl[i]._id)
const dl = this.dataList if (index >= 0) {
for (let i = dl.length - 1; i >= 0; i--) { dl.splice(i, 1)
const index = il.indexOf(dl[i]._id) il.splice(index, 1)
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> </script>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册