unicloud-db.vue 10.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>
d-u-a's avatar
d-u-a 已提交
15 16 17 18 19 20 21 22
const events = {
  load: 'load',
  error: 'error'
}
const pageMode = {
  add: 'add',
  replace: 'replace'
}
d-u-a's avatar
d-u-a 已提交
23

d-u-a's avatar
d-u-a 已提交
24 25 26 27 28 29 30 31 32 33
const attrs = [
  'pageCurrent',
  'pageSize',
  'collection',
  'action',
  'field',
  'getcount',
  'orderby',
  'where'
]
d-u-a's avatar
d-u-a 已提交
34

d-u-a's avatar
d-u-a 已提交
35 36 37 38 39 40 41
export default {
  name: 'UniClouddb',
  props: {
    options: {
      type: [Object, Array],
      default () {
        return {}
d-u-a's avatar
d-u-a 已提交
42 43
      }
    },
d-u-a's avatar
d-u-a 已提交
44 45 46
    collection: {
      type: String,
      default: ''
d-u-a's avatar
d-u-a 已提交
47
    },
d-u-a's avatar
d-u-a 已提交
48 49 50 51 52 53 54 55
    action: {
      type: String,
      default: ''
    },
    field: {
      type: String,
      default: ''
    },
56 57 58 59 60 61 62 63
    orderby: {
      type: String,
      default: ''
    },
    where: {
      type: [String, Object],
      default: ''
    },
d-u-a's avatar
d-u-a 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    pageData: {
      type: String,
      default: 'add'
    },
    pageCurrent: {
      type: Number,
      default: 1
    },
    pageSize: {
      type: Number,
      default: 20
    },
    getcount: {
      type: [Boolean, String],
      default: false
    },
80 81 82 83 84 85 86 87
    getone: {
      type: [Boolean, String],
      default: false
    },
    gettree: {
      type: [Boolean, String],
      default: false
    },
88
    startwith: {
d-u-a's avatar
d-u-a 已提交
89 90 91
      type: String,
      default: ''
    },
92 93 94
    limitlevel: {
      type: Number,
      default: 10
d-u-a's avatar
d-u-a 已提交
95 96 97 98 99 100 101 102 103
    },
    manual: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      loading: false,
d-u-a's avatar
d-u-a 已提交
104
      hasMore: false,
105
      dataList: this.getone ? undefined : [],
d-u-a's avatar
d-u-a 已提交
106 107 108 109 110 111 112 113 114 115
      paginationInternal: {
        current: this.pageCurrent,
        size: this.pageSize,
        count: 0
      },
      errorMessage: ''
    }
  },
  created () {
    this._isEnded = false
d-u-a's avatar
d-u-a 已提交
116

d-u-a's avatar
d-u-a 已提交
117 118 119 120 121 122 123
    this.$watch(() => {
      var al = []
      attrs.forEach(key => {
        al.push(this[key])
      })
      return al
    }, (newValue, oldValue) => {
124
      this.paginationInternal.size = this.pageSize
d-u-a's avatar
d-u-a 已提交
125

d-u-a's avatar
d-u-a 已提交
126 127 128 129 130
      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 已提交
131
        }
d-u-a's avatar
d-u-a 已提交
132 133 134 135 136 137 138 139
      }
      if (needReset) {
        this.clear()
        this.reset()
      }
      if (newValue[0] !== oldValue[0]) {
        this.paginationInternal.current = this.pageCurrent
      }
d-u-a's avatar
d-u-a 已提交
140

d-u-a's avatar
d-u-a 已提交
141 142
      this._execLoadData()
    })
d-u-a's avatar
d-u-a 已提交
143

d-u-a's avatar
d-u-a 已提交
144 145 146 147 148 149 150
    // #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 已提交
151 152 153
          }
        }
      }
d-u-a's avatar
d-u-a 已提交
154 155 156
      window.unidev.clientDB.data.push(this._debugDataList)
    }
    // #endif
d-u-a's avatar
d-u-a 已提交
157

d-u-a's avatar
d-u-a 已提交
158 159 160 161 162 163 164
    // #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]
d-u-a's avatar
d-u-a 已提交
165
      }
d-u-a's avatar
d-u-a 已提交
166 167 168 169 170 171 172 173 174 175 176
    }
    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 已提交
177
        }
d-u-a's avatar
d-u-a 已提交
178 179
        parent = parent.$parent
        maxDepth--
d-u-a's avatar
d-u-a 已提交
180
      }
d-u-a's avatar
d-u-a 已提交
181 182
    }
    // #endif
d-u-a's avatar
d-u-a 已提交
183

d-u-a's avatar
d-u-a 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196
    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
d-u-a's avatar
d-u-a 已提交
197 198
        }
      }
d-u-a's avatar
d-u-a 已提交
199 200 201 202 203 204 205 206 207 208
    }
  },
  // #endif
  methods: {
    loadData (args1, args2) {
      let callback = null
      if (typeof args1 === 'object') {
        if (args1.clear) {
          this.clear()
          this.reset()
d-u-a's avatar
d-u-a 已提交
209
        }
d-u-a's avatar
d-u-a 已提交
210 211
        if (args1.current !== undefined) {
          this.paginationInternal.current = args1.current
d-u-a's avatar
d-u-a 已提交
212
        }
d-u-a's avatar
d-u-a 已提交
213 214
        if (typeof args2 === 'function') {
          callback = args2
d-u-a's avatar
d-u-a 已提交
215
        }
d-u-a's avatar
d-u-a 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
      } 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)
d-u-a's avatar
d-u-a 已提交
255
        uni.showModal({
d-u-a's avatar
d-u-a 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
          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
d-u-a's avatar
d-u-a 已提交
282
          }
d-u-a's avatar
d-u-a 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
          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 || '修改成功'
d-u-a's avatar
d-u-a 已提交
300
        })
d-u-a's avatar
d-u-a 已提交
301 302 303 304 305
      }).catch((err) => {
        fail && fail(err)
        uni.showModal({
          content: err.message,
          showCancel: false
d-u-a's avatar
d-u-a 已提交
306
        })
d-u-a's avatar
d-u-a 已提交
307 308 309 310 311 312 313 314 315 316 317
      }).finally(() => {
        uni.hideLoading()
        complete && complete()
      })
    },
    _execLoadData (callback) {
      if (this.loading) {
        return
      }
      this.loading = true
      this.errorMessage = ''
d-u-a's avatar
d-u-a 已提交
318

d-u-a's avatar
d-u-a 已提交
319 320 321 322 323 324 325
      this._getExec().then((res) => {
        this.loading = false
        const {
          data,
          count
        } = res.result
        this._isEnded = data.length < this.pageSize
d-u-a's avatar
d-u-a 已提交
326
        this.hasMore = !this._isEnded
d-u-a's avatar
d-u-a 已提交
327

d-u-a's avatar
d-u-a 已提交
328
        const data2 = this.getone ? (data.length ? data[0] : undefined) : data
329

d-u-a's avatar
d-u-a 已提交
330 331
        callback && callback(data2, this._isEnded)
        this._dispatchEvent(events.load, data2)
d-u-a's avatar
d-u-a 已提交
332

333 334 335
        if (this.getone || this.pageData === pageMode.replace) {
          this.dataList = data2
        } else {
d-u-a's avatar
d-u-a 已提交
336 337 338
          this.dataList.push(...data2)
          if (this.dataList.length) {
            this.paginationInternal.current++
d-u-a's avatar
d-u-a 已提交
339 340
          }
        }
d-u-a's avatar
d-u-a 已提交
341

d-u-a's avatar
d-u-a 已提交
342 343
        if (this.getcount) {
          this.paginationInternal.count = count
d-u-a's avatar
d-u-a 已提交
344
        }
d-u-a's avatar
d-u-a 已提交
345 346 347 348

        // #ifdef H5
        if (process.env.NODE_ENV === 'development') {
          this._debugDataList.length = 0
349
          const formatData = JSON.parse(JSON.stringify(this.dataList))
350 351 352 353 354
          if (Array.isArray(this.dataList)) {
            this._debugDataList.push(...formatData)
          } else {
            this._debugDataList.push(formatData)
          }
d-u-a's avatar
d-u-a 已提交
355
        }
d-u-a's avatar
d-u-a 已提交
356 357 358 359 360 361 362 363
        // #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)
d-u-a's avatar
d-u-a 已提交
364
        }
d-u-a's avatar
d-u-a 已提交
365 366 367 368 369
      })
    },
    _getExec () {
      /* eslint-disable no-undef */
      let db = uniCloud.database()
d-u-a's avatar
d-u-a 已提交
370

d-u-a's avatar
d-u-a 已提交
371 372 373
      if (this.action) {
        db = db.action(this.action)
      }
d-u-a's avatar
d-u-a 已提交
374

d-u-a's avatar
d-u-a 已提交
375
      db = db.collection(this.collection)
d-u-a's avatar
d-u-a 已提交
376

d-u-a's avatar
d-u-a 已提交
377 378 379 380 381 382 383 384 385
      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)
      }
d-u-a's avatar
d-u-a 已提交
386

d-u-a's avatar
d-u-a 已提交
387 388 389 390
      const {
        current,
        size
      } = this.paginationInternal
391 392 393 394 395 396 397 398 399 400 401
      const getOptions = {}
      if (this.getcount) {
        getOptions.getCount = this.getcount
      }
      if (this.gettree) {
        getOptions.getTree = {
          limitLevel: this.limitlevel,
          startWith: this.startwith
        }
      }
      db = db.skip(size * (current - 1)).limit(size).get(getOptions)
d-u-a's avatar
d-u-a 已提交
402

d-u-a's avatar
d-u-a 已提交
403 404 405 406 407 408
      return db
    },
    _execRemove (id, action, success, fail, complete) {
      if (!this.collection || !id) {
        return
      }
d-u-a's avatar
d-u-a 已提交
409

d-u-a's avatar
d-u-a 已提交
410 411 412 413
      const ids = Array.isArray(id) ? id : [id]
      if (!ids.length) {
        return
      }
d-u-a's avatar
d-u-a 已提交
414

d-u-a's avatar
d-u-a 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
      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()
d-u-a's avatar
d-u-a 已提交
434
        } else {
d-u-a's avatar
d-u-a 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
          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)
d-u-a's avatar
d-u-a 已提交
456 457
        }
      }
d-u-a's avatar
d-u-a 已提交
458 459 460 461 462 463 464
    },
    _dispatchEvent (type, data) {
      if (this._changeDataFunction) {
        this._changeDataFunction(data, this._isEnded)
      } else {
        this.$emit(type, data, this._isEnded)
      }
d-u-a's avatar
d-u-a 已提交
465 466
    }
  }
d-u-a's avatar
d-u-a 已提交
467
}
d-u-a's avatar
d-u-a 已提交
468
</script>