unicloud-db.vue 14.1 KB
Newer Older
d-u-a's avatar
d-u-a 已提交
1 2
<template>
  <view>
d-u-a's avatar
d-u-a 已提交
3 4 5 6 7
    <slot
      :options="options"
      :data="dataList"
      :pagination="paginationInternal"
      :loading="loading"
d-u-a's avatar
d-u-a 已提交
8
      :hasMore="hasMore"
d-u-a's avatar
d-u-a 已提交
9 10
      :error="errorMessage"
    />
d-u-a's avatar
d-u-a 已提交
11 12 13 14
  </view>
</template>

<script>
fxy060608's avatar
fxy060608 已提交
15 16 17 18
import { initVueI18n } from '@dcloudio/uni-i18n'
import messages from './i18n/index'

const { t } = initVueI18n(messages)
fxy060608's avatar
fxy060608 已提交
19

d-u-a's avatar
d-u-a 已提交
20 21 22 23 24 25 26 27
const events = {
  load: 'load',
  error: 'error'
}
const pageMode = {
  add: 'add',
  replace: 'replace'
}
28 29 30 31 32
const loadMode = {
  auto: 'auto',
  onready: 'onready',
  manual: 'manual'
}
d-u-a's avatar
d-u-a 已提交
33

d-u-a's avatar
d-u-a 已提交
34 35 36 37 38 39 40 41
const attrs = [
  'pageCurrent',
  'pageSize',
  'collection',
  'action',
  'field',
  'getcount',
  'orderby',
42 43 44 45
  'where',
  'groupby',
  'groupField',
  'distinct'
d-u-a's avatar
d-u-a 已提交
46
]
d-u-a's avatar
d-u-a 已提交
47

d-u-a's avatar
d-u-a 已提交
48 49 50 51 52 53 54
export default {
  name: 'UniClouddb',
  props: {
    options: {
      type: [Object, Array],
      default () {
        return {}
d-u-a's avatar
d-u-a 已提交
55 56
      }
    },
d-u-a's avatar
d-u-a 已提交
57
    collection: {
58
      type: [String, Array],
d-u-a's avatar
d-u-a 已提交
59
      default: ''
d-u-a's avatar
d-u-a 已提交
60
    },
d-u-a's avatar
d-u-a 已提交
61 62 63 64 65 66 67 68
    action: {
      type: String,
      default: ''
    },
    field: {
      type: String,
      default: ''
    },
69 70 71 72 73 74 75 76
    orderby: {
      type: String,
      default: ''
    },
    where: {
      type: [String, Object],
      default: ''
    },
d-u-a's avatar
d-u-a 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    pageData: {
      type: String,
      default: 'add'
    },
    pageCurrent: {
      type: Number,
      default: 1
    },
    pageSize: {
      type: Number,
      default: 20
    },
    getcount: {
      type: [Boolean, String],
      default: false
    },
93 94 95 96 97
    getone: {
      type: [Boolean, String],
      default: false
    },
    gettree: {
98
      type: [Boolean, String, Object],
99 100
      default: false
    },
101 102 103 104
    gettreepath: {
      type: [Boolean, String],
      default: false
    },
105
    startwith: {
d-u-a's avatar
d-u-a 已提交
106 107 108
      type: String,
      default: ''
    },
109 110 111
    limitlevel: {
      type: Number,
      default: 10
d-u-a's avatar
d-u-a 已提交
112
    },
113 114 115 116 117 118 119 120 121 122 123
    groupby: {
      type: String,
      default: ''
    },
    groupField: {
      type: String,
      default: ''
    },
    distinct: {
      type: [Boolean, String],
      default: false
124
    },
125 126 127 128
    pageIndistinct: {
      type: [Boolean, String],
      default: false
    },
129
    foreignKey: {
130 131 132
      type: String,
      default: ''
    },
133 134 135 136
    loadtime: {
      type: String,
      default: 'auto'
    },
137 138 139
    manual: {
      type: Boolean,
      default: false
d-u-a's avatar
d-u-a 已提交
140 141 142 143 144
    }
  },
  data () {
    return {
      loading: false,
d-u-a's avatar
d-u-a 已提交
145
      hasMore: false,
146
      dataList: this.getone ? undefined : [],
147
      paginationInternal: {},
d-u-a's avatar
d-u-a 已提交
148 149 150
      errorMessage: ''
    }
  },
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  computed: {
    collectionArgs () {
      return Array.isArray(this.collection) ? this.collection : [this.collection]
    },
    isLookup () {
      return (Array.isArray(this.collection) && this.collection.length > 1) || (typeof this.collection === 'string' && this.collection.indexOf(',') > -1)
    },
    mainCollection () {
      if (typeof this.collection === 'string') {
        return this.collection.split(',')[0]
      }
      const mainQuery = JSON.parse(JSON.stringify(this.collection[0]))
      return mainQuery.$db[0].$param[0]
    }
  },
d-u-a's avatar
d-u-a 已提交
166 167
  created () {
    this._isEnded = false
168 169 170 171 172
    this.paginationInternal = {
      current: this.pageCurrent,
      size: this.pageSize,
      count: 0
    }
d-u-a's avatar
d-u-a 已提交
173

d-u-a's avatar
d-u-a 已提交
174 175 176 177 178 179 180
    this.$watch(() => {
      var al = []
      attrs.forEach(key => {
        al.push(this[key])
      })
      return al
    }, (newValue, oldValue) => {
181 182 183 184
      if (this.loadtime === loadMode.manual) {
        return
      }

185
      this.paginationInternal.size = this.pageSize
d-u-a's avatar
d-u-a 已提交
186

d-u-a's avatar
d-u-a 已提交
187 188 189 190 191
      let needReset = false
      for (let i = 2; i < newValue.length; i++) {
        if (newValue[i] !== oldValue[i]) {
          needReset = true
          break
d-u-a's avatar
d-u-a 已提交
192
        }
d-u-a's avatar
d-u-a 已提交
193 194 195 196 197 198 199 200
      }
      if (needReset) {
        this.clear()
        this.reset()
      }
      if (newValue[0] !== oldValue[0]) {
        this.paginationInternal.current = this.pageCurrent
      }
d-u-a's avatar
d-u-a 已提交
201

d-u-a's avatar
d-u-a 已提交
202 203
      this._execLoadData()
    })
d-u-a's avatar
d-u-a 已提交
204

d-u-a's avatar
d-u-a 已提交
205 206 207 208 209 210 211
    // #ifdef H5
    if (process.env.NODE_ENV === 'development') {
      this._debugDataList = []
      if (!window.unidev) {
        window.unidev = {
          clientDB: {
            data: []
d-u-a's avatar
d-u-a 已提交
212 213 214
          }
        }
      }
d-u-a's avatar
d-u-a 已提交
215 216 217
      window.unidev.clientDB.data.push(this._debugDataList)
    }
    // #endif
d-u-a's avatar
d-u-a 已提交
218

d-u-a's avatar
d-u-a 已提交
219 220
    // #ifdef MP-TOUTIAO
    let changeName
221
    const events = this.$scope.dataset.eventOpts || []
d-u-a's avatar
d-u-a 已提交
222 223 224 225
    for (var i = 0; i < events.length; i++) {
      const event = events[i]
      if (event[0].includes('^load')) {
        changeName = event[1][0][0]
d-u-a's avatar
d-u-a 已提交
226
      }
d-u-a's avatar
d-u-a 已提交
227 228 229 230 231 232 233 234 235 236 237
    }
    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
d-u-a's avatar
d-u-a 已提交
238
        }
d-u-a's avatar
d-u-a 已提交
239 240
        parent = parent.$parent
        maxDepth--
d-u-a's avatar
d-u-a 已提交
241
      }
d-u-a's avatar
d-u-a 已提交
242 243
    }
    // #endif
d-u-a's avatar
d-u-a 已提交
244

245
    if (!this.manual && this.loadtime === loadMode.auto) {
d-u-a's avatar
d-u-a 已提交
246 247 248 249 250 251 252 253 254 255 256 257
      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
d-u-a's avatar
d-u-a 已提交
258 259
        }
      }
d-u-a's avatar
d-u-a 已提交
260 261 262 263 264 265
    }
  },
  // #endif
  methods: {
    loadData (args1, args2) {
      let callback = null
266
      let clear = false
d-u-a's avatar
d-u-a 已提交
267 268
      if (typeof args1 === 'object') {
        if (args1.clear) {
269 270 271 272 273
          if (this.pageData === pageMode.replace) {
            this.clear()
          } else {
            clear = args1.clear
          }
d-u-a's avatar
d-u-a 已提交
274
          this.reset()
d-u-a's avatar
d-u-a 已提交
275
        }
d-u-a's avatar
d-u-a 已提交
276 277
        if (args1.current !== undefined) {
          this.paginationInternal.current = args1.current
d-u-a's avatar
d-u-a 已提交
278
        }
d-u-a's avatar
d-u-a 已提交
279 280
        if (typeof args2 === 'function') {
          callback = args2
d-u-a's avatar
d-u-a 已提交
281
        }
d-u-a's avatar
d-u-a 已提交
282 283 284 285
      } else if (typeof args1 === 'function') {
        callback = args1
      }

286
      this._execLoadData(callback, clear)
d-u-a's avatar
d-u-a 已提交
287 288
    },
    loadMore () {
289
      if (this._isEnded || this.loading) {
d-u-a's avatar
d-u-a 已提交
290 291
        return
      }
292 293 294 295 296

      if (this.pageData === pageMode.add) {
        this.paginationInternal.current++
      }

d-u-a's avatar
d-u-a 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310
      this._execLoadData()
    },
    refresh () {
      this.clear()
      this._execLoadData()
    },
    clear () {
      this._isEnded = false
      this.dataList = []
    },
    reset () {
      this.paginationInternal.current = 1
    },
    add (value, {
311 312
      action,
      showToast = true,
d-u-a's avatar
d-u-a 已提交
313 314 315
      toastTitle,
      success,
      fail,
316 317 318 319
      complete,
      needConfirm = true,
      needLoading = true,
      loadingTitle = ''
d-u-a's avatar
d-u-a 已提交
320
    } = {}) {
321 322 323 324 325
      if (needLoading) {
        uni.showLoading({
          title: loadingTitle
        })
      }
d-u-a's avatar
d-u-a 已提交
326
      /* eslint-disable no-undef */
327 328 329 330 331
      let db = uniCloud.database()
      if (action) {
        db = db.action(action)
      }

332
      db.collection(this.mainCollection).add(value).then((res) => {
d-u-a's avatar
d-u-a 已提交
333
        success && success(res)
334 335
        if (showToast) {
          uni.showToast({
fxy060608's avatar
fxy060608 已提交
336
            title: toastTitle || t('uniCloud.component.add.success')
337 338
          })
        }
d-u-a's avatar
d-u-a 已提交
339 340
      }).catch((err) => {
        fail && fail(err)
341 342 343 344 345 346
        if (needConfirm) {
          uni.showModal({
            content: err.message,
            showCancel: false
          })
        }
d-u-a's avatar
d-u-a 已提交
347
      }).finally(() => {
348 349 350
        if (needLoading) {
          uni.hideLoading()
        }
d-u-a's avatar
d-u-a 已提交
351 352 353 354 355 356 357 358 359
        complete && complete()
      })
    },
    remove (id, {
      action,
      success,
      fail,
      complete,
      confirmTitle,
360 361 362 363
      confirmContent,
      needConfirm = true,
      needLoading = true,
      loadingTitle = ''
d-u-a's avatar
d-u-a 已提交
364 365 366 367
    } = {}) {
      if (!id || !id.length) {
        return
      }
368 369 370 371
      if (!needConfirm) {
        this._execRemove(id, action, success, fail, complete, needConfirm, needLoading, loadingTitle)
        return
      }
d-u-a's avatar
d-u-a 已提交
372
      uni.showModal({
fxy060608's avatar
fxy060608 已提交
373 374
        title: confirmTitle || t('uniCloud.component.remove.showModal.title'),
        content: confirmContent || t('uniCloud.component.remove.showModal.content'),
d-u-a's avatar
d-u-a 已提交
375 376 377 378
        showCancel: true,
        success: (res) => {
          if (!res.confirm) {
            return
d-u-a's avatar
d-u-a 已提交
379
          }
380
          this._execRemove(id, action, success, fail, complete, needConfirm, needLoading, loadingTitle)
d-u-a's avatar
d-u-a 已提交
381 382 383 384
        }
      })
    },
    update (id, value, {
385 386
      action,
      showToast = true,
d-u-a's avatar
d-u-a 已提交
387 388 389
      toastTitle,
      success,
      fail,
390 391 392 393
      complete,
      needConfirm = true,
      needLoading = true,
      loadingTitle = ''
d-u-a's avatar
d-u-a 已提交
394
    } = {}) {
395 396 397 398 399
      if (needLoading) {
        uni.showLoading({
          title: loadingTitle
        })
      }
d-u-a's avatar
d-u-a 已提交
400
      /* eslint-disable no-undef */
401 402 403 404 405
      let db = uniCloud.database()
      if (action) {
        db = db.action(action)
      }

406
      return db.collection(this.mainCollection).doc(id).update(value).then((res) => {
d-u-a's avatar
d-u-a 已提交
407
        success && success(res)
408 409
        if (showToast) {
          uni.showToast({
fxy060608's avatar
fxy060608 已提交
410
            title: toastTitle || t('uniCloud.component.update.success')
411 412
          })
        }
d-u-a's avatar
d-u-a 已提交
413 414
      }).catch((err) => {
        fail && fail(err)
415 416 417 418 419 420
        if (needConfirm) {
          uni.showModal({
            content: err.message,
            showCancel: false
          })
        }
d-u-a's avatar
d-u-a 已提交
421
      }).finally(() => {
422 423 424
        if (needLoading) {
          uni.hideLoading()
        }
d-u-a's avatar
d-u-a 已提交
425 426 427
        complete && complete()
      })
    },
Q
qiang 已提交
428
    getTemp (isTemp = true) {
d-u-a's avatar
d-u-a 已提交
429 430
      /* eslint-disable no-undef */
      let db = uniCloud.database()
d-u-a's avatar
d-u-a 已提交
431

d-u-a's avatar
d-u-a 已提交
432 433 434
      if (this.action) {
        db = db.action(this.action)
      }
d-u-a's avatar
d-u-a 已提交
435

436
      db = db.collection(...this.collectionArgs)
d-u-a's avatar
d-u-a 已提交
437

d-u-a's avatar
d-u-a 已提交
438 439 440 441 442 443
      if (!(!this.where || !Object.keys(this.where).length)) {
        db = db.where(this.where)
      }
      if (this.field) {
        db = db.field(this.field)
      }
444 445
      if (this.foreignKey) {
        db = db.foreignKey(this.foreignKey)
446
      }
447
      if (this.groupby) {
448
        db = db.groupBy(this.groupby)
449 450 451 452 453 454 455
      }
      if (this.groupField) {
        db = db.groupField(this.groupField)
      }
      if (this.distinct === true) {
        db = db.distinct()
      }
d-u-a's avatar
d-u-a 已提交
456 457 458
      if (this.orderby) {
        db = db.orderBy(this.orderby)
      }
d-u-a's avatar
d-u-a 已提交
459

d-u-a's avatar
d-u-a 已提交
460 461 462 463
      const {
        current,
        size
      } = this.paginationInternal
464 465 466 467
      const getOptions = {}
      if (this.getcount) {
        getOptions.getCount = this.getcount
      }
468 469 470 471
      const treeOptions = {
        limitLevel: this.limitlevel,
        startWith: this.startwith
      }
472
      if (this.gettree) {
473 474 475 476
        getOptions.getTree = treeOptions
      }
      if (this.gettreepath) {
        getOptions.getTreePath = treeOptions
477
      }
478 479 480 481 482 483 484 485
      db = db.skip(size * (current - 1)).limit(size)

      if (isTemp) {
        db = db.getTemp(getOptions)
        db.udb = this
      } else {
        db = db.get(getOptions)
      }
d-u-a's avatar
d-u-a 已提交
486

d-u-a's avatar
d-u-a 已提交
487 488
      return db
    },
Q
qiang 已提交
489
    setResult (result) {
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
      if (result.code === 0) {
        this._execLoadDataSuccess(result)
      } else {
        this._execLoadDataFail(new Error(result.message))
      }
    },
    _execLoadData (callback, clear) {
      if (this.loading) {
        return
      }
      this.loading = true
      this.errorMessage = ''

      this._getExec().then((res) => {
        this.loading = false
        this._execLoadDataSuccess(res.result, callback, clear)

        // #ifdef H5
        if (process.env.NODE_ENV === 'development') {
          this._debugDataList.length = 0
          const formatData = JSON.parse(JSON.stringify(this.dataList))
          if (Array.isArray(this.dataList)) {
            this._debugDataList.push(...formatData)
          } else {
            this._debugDataList.push(formatData)
          }
        }
        // #endif
      }).catch((err) => {
        this.loading = false
        this._execLoadDataFail(err, callback)
      })
    },
Q
qiang 已提交
523
    _execLoadDataSuccess (result, callback, clear) {
524 525 526 527
      const {
        data,
        count
      } = result
d-u-a's avatar
d-u-a 已提交
528
      this._isEnded = count !== undefined ? (this.paginationInternal.current * this.paginationInternal.size >= count) : (data.length < this.pageSize)
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
      this.hasMore = !this._isEnded

      const data2 = this.getone ? (data.length ? data[0] : undefined) : data

      if (this.getcount) {
        this.paginationInternal.count = count
      }

      callback && callback(data2, this._isEnded, this.paginationInternal)
      this._dispatchEvent(events.load, data2)

      if (this.getone || this.pageData === pageMode.replace) {
        this.dataList = data2
      } else {
        if (clear) {
          this.dataList = data2
        } else {
          this.dataList.push(...data2)
        }
      }
    },
Q
qiang 已提交
550
    _execLoadDataFail (err, callback) {
551 552 553 554 555 556 557 558 559 560
      this.errorMessage = err
      callback && callback()
      this.$emit(events.error, err)
      if (process.env.NODE_ENV === 'development') {
        console.error(err)
      }
    },
    _getExec () {
      return this.getTemp(false)
    },
561
    _execRemove (id, action, success, fail, complete, needConfirm, needLoading, loadingTitle) {
d-u-a's avatar
d-u-a 已提交
562 563 564
      if (!this.collection || !id) {
        return
      }
d-u-a's avatar
d-u-a 已提交
565

d-u-a's avatar
d-u-a 已提交
566 567 568 569
      const ids = Array.isArray(id) ? id : [id]
      if (!ids.length) {
        return
      }
d-u-a's avatar
d-u-a 已提交
570

571 572 573 574 575 576
      if (needLoading) {
        uni.showLoading({
          mask: true,
          title: loadingTitle
        })
      }
d-u-a's avatar
d-u-a 已提交
577 578 579 580 581 582 583 584 585 586

      /* eslint-disable no-undef */
      const db = uniCloud.database()
      const dbCmd = db.command

      let exec = db
      if (action) {
        exec = exec.action(action)
      }

587
      exec.collection(this.mainCollection).where({
d-u-a's avatar
d-u-a 已提交
588 589 590 591 592
        _id: dbCmd.in(ids)
      }).remove().then((res) => {
        success && success(res.result)
        if (this.pageData === pageMode.replace) {
          this.refresh()
d-u-a's avatar
d-u-a 已提交
593
        } else {
d-u-a's avatar
d-u-a 已提交
594 595 596 597
          this.removeData(ids)
        }
      }).catch((err) => {
        fail && fail(err)
598 599 600 601 602 603
        if (needConfirm) {
          uni.showModal({
            content: err.message,
            showCancel: false
          })
        }
d-u-a's avatar
d-u-a 已提交
604
      }).finally(() => {
605 606 607
        if (needLoading) {
          uni.hideLoading()
        }
d-u-a's avatar
d-u-a 已提交
608 609 610 611 612 613 614 615 616 617 618
        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)
d-u-a's avatar
d-u-a 已提交
619 620
        }
      }
d-u-a's avatar
d-u-a 已提交
621 622 623
    },
    _dispatchEvent (type, data) {
      if (this._changeDataFunction) {
624
        this._changeDataFunction(data, this._isEnded, this.paginationInternal)
d-u-a's avatar
d-u-a 已提交
625
      } else {
626
        this.$emit(type, data, this._isEnded, this.paginationInternal)
d-u-a's avatar
d-u-a 已提交
627
      }
d-u-a's avatar
d-u-a 已提交
628 629
    }
  }
d-u-a's avatar
d-u-a 已提交
630
}
d-u-a's avatar
d-u-a 已提交
631
</script>