提交 41497f8d 编写于 作者: d-u-a's avatar d-u-a

update: <unicloud-db> lint

上级 a15b2de8
<template> <template>
<view> <view>
<slot :options="options" :data="dataList" :pagination="paginationInternal" :loading="loading" :error="errorMessage" /> <slot
: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
} }
}, },
data() { collection: {
return { type: String,
loading: false, default: ''
dataList: this.getone ? {} : [],
paginationInternal: {
current: this.pageCurrent,
size: this.pageSize,
count: 0
},
errorMessage: ''
}
}, },
created() { action: {
this._isEnded = false 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(() => { 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)
} }
// #endif window.unidev.clientDB.data.push(this._debugDataList)
}
// #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) { }
let parent = this.$parent if (changeName) {
let maxDepth = 16 let parent = this.$parent
this._changeDataFunction = null let maxDepth = 16
while (parent && maxDepth > 0) { this._changeDataFunction = null
const fun = parent[changeName] while (parent && maxDepth > 0) {
if (fun && typeof fun === 'function') { const fun = parent[changeName]
this._changeDataFunction = fun if (fun && typeof fun === 'function') {
maxDepth = 0 this._changeDataFunction = fun
break maxDepth = 0
} 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 },
methods: { // #endif
loadData(args1, args2) { methods: {
let callback = null loadData (args1, args2) {
if (typeof args1 === 'object') { let callback = null
if (args1.clear) { if (typeof args1 === 'object') {
this.clear() if (args1.clear) {
this.reset() 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._execLoadData(callback) this.paginationInternal.current = args1.current
},
loadMore() {
if (this._isEnded) {
return
} }
this._execLoadData() if (typeof args2 === 'function') {
}, 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') {
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({ uni.showModal({
title: confirmTitle || '提示', content: err.message,
content: confirmContent || '是否删除该数据', showCancel: false
showCancel: true, })
success: (res) => { }).finally(() => {
if (!res.confirm) { uni.hideLoading()
return complete && complete()
} })
this._execRemove(id, action, success, fail, 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 || '修改成功'
}) })
}, }).catch((err) => {
update(id, value, { fail && fail(err)
toastTitle, uni.showModal({
success, content: err.message,
fail, showCancel: false
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()
}) })
}, }).finally(() => {
_execLoadData(callback) { uni.hideLoading()
if (this.loading) { complete && complete()
return })
} },
this.loading = true _execLoadData (callback) {
this.errorMessage = '' if (this.loading) {
return
this._getExec().then((res) => { }
this.loading = false this.loading = true
const { this.errorMessage = ''
data,
count
} = res.result
this._isEnded = data.length < this.pageSize
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) const data2 = this.getone ? (data.length ? data[0] : undefined) : data
this._dispatchEvent(events.load, data2)
if (this.pageData === pageMode.add) { callback && callback(data2, this._isEnded)
this.dataList.push(...data2) this._dispatchEvent(events.load, data2)
if (this.dataList.length) {
this.paginationInternal.current++
}
} else {
this.dataList = data2
}
if (this.getcount) { if (this.pageData === pageMode.add) {
this.paginationInternal.count = count this.dataList.push(...data2)
if (this.dataList.length) {
this.paginationInternal.current++
} }
} else {
// #ifdef H5 this.dataList = data2
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)
} }
db = db.collection(this.collection) if (this.getcount) {
this.paginationInternal.count = count
if (!(!this.where || !Object.keys(this.where).length)) {
db = db.where(this.where)
} }
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) { // #endif
db = db.orderBy(this.orderby) }).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 { if (this.action) {
current, db = db.action(this.action)
size }
} = this.paginationInternal
db = db.skip(size * (current - 1)).limit(size).get({
getCount: this.getcount
})
return db db = db.collection(this.collection)
},
_execRemove(id, action, success, fail, complete) {
if (!this.collection || !id) {
return
}
const ids = Array.isArray(id) ? id : [id] if (!(!this.where || !Object.keys(this.where).length)) {
if (!ids.length) { db = db.where(this.where)
return }
} if (this.field) {
db = db.field(this.field)
}
if (this.orderby) {
db = db.orderBy(this.orderby)
}
uni.showLoading({ const {
mask: true current,
}) size
} = this.paginationInternal
db = db.skip(size * (current - 1)).limit(size).get({
getCount: this.getcount
})
/* eslint-disable no-undef */ return db
const db = uniCloud.database() },
const dbCmd = db.command _execRemove (id, action, success, fail, complete) {
if (!this.collection || !id) {
return
}
let exec = db const ids = Array.isArray(id) ? id : [id]
if (action) { if (!ids.length) {
exec = exec.action(action) return
} }
exec.collection(this.collection).where({ uni.showLoading({
_id: dbCmd.in(ids) mask: true
}).remove().then((res) => { })
success && success(res.result)
if (this.pageData === pageMode.replace) { /* eslint-disable no-undef */
this.refresh() const db = uniCloud.database()
} else { const dbCmd = db.command
this.removeData(ids)
} let exec = db
}).catch((err) => { if (action) {
fail && fail(err) exec = exec.action(action)
uni.showModal({ }
content: err.message,
showCancel: false exec.collection(this.collection).where({
}) _id: dbCmd.in(ids)
}).finally(() => { }).remove().then((res) => {
uni.hideLoading() success && success(res.result)
complete && complete() if (this.pageData === pageMode.replace) {
}) this.refresh()
},
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 { } 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> </script>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册