@dcloudio/uni-cli-shared 2.0.2-4010520240507001 → 2.0.2-4020420240722001

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,656 +1,674 @@
1
- <template>
2
- <view>
1
+ <template>
2
+ <view>
3
3
  <slot
4
4
  :options="options"
5
5
  :data="dataList"
6
6
  :pagination="paginationInternal"
7
7
  :loading="loading"
8
- :hasMore="hasMore"
8
+ :hasMore="hasMore"
9
9
  :error="errorMessage"
10
- />
11
- </view>
12
- </template>
13
-
14
- <script>
15
- import { initVueI18n } from '@dcloudio/uni-i18n'
16
- import messages from './i18n/index'
17
-
18
- const { t } = initVueI18n(messages)
19
-
20
- const events = {
21
- load: 'load',
22
- error: 'error'
23
- }
24
- const pageMode = {
25
- add: 'add',
26
- replace: 'replace'
27
- }
28
- const loadMode = {
29
- auto: 'auto',
30
- onready: 'onready',
31
- manual: 'manual'
32
- }
33
-
34
- const attrs = [
35
- 'pageCurrent',
36
- 'pageSize',
37
- 'spaceInfo',
38
- 'collection',
39
- 'action',
40
- 'field',
41
- 'getcount',
42
- 'orderby',
43
- 'where',
44
- 'groupby',
45
- 'groupField',
46
- 'distinct'
47
- ]
48
-
49
- export default {
50
- name: 'UniClouddb',
51
- props: {
52
- options: {
53
- type: [Object, Array],
54
- default () {
55
- return {}
56
- }
57
- },
58
- spaceInfo: {
59
- type: Object,
60
- default () {
61
- return {}
62
- }
63
- },
64
- collection: {
65
- type: [String, Array],
66
- default: ''
67
- },
68
- action: {
69
- type: String,
70
- default: ''
71
- },
72
- field: {
73
- type: String,
74
- default: ''
75
- },
76
- orderby: {
77
- type: String,
78
- default: ''
79
- },
80
- where: {
81
- type: [String, Object],
82
- default: ''
83
- },
84
- pageData: {
85
- type: String,
86
- default: 'add'
87
- },
88
- pageCurrent: {
89
- type: Number,
90
- default: 1
91
- },
92
- pageSize: {
93
- type: Number,
94
- default: 20
95
- },
96
- getcount: {
97
- type: [Boolean, String],
98
- default: false
99
- },
100
- getone: {
101
- type: [Boolean, String],
102
- default: false
103
- },
104
- gettree: {
105
- type: [Boolean, String, Object],
106
- default: false
107
- },
108
- gettreepath: {
109
- type: [Boolean, String],
110
- default: false
111
- },
112
- startwith: {
113
- type: String,
114
- default: ''
115
- },
116
- limitlevel: {
117
- type: Number,
118
- default: 10
119
- },
120
- groupby: {
121
- type: String,
122
- default: ''
123
- },
124
- groupField: {
125
- type: String,
126
- default: ''
127
- },
128
- distinct: {
129
- type: [Boolean, String],
130
- default: false
131
- },
132
- pageIndistinct: {
133
- type: [Boolean, String],
134
- default: false
135
- },
136
- foreignKey: {
137
- type: String,
138
- default: ''
139
- },
140
- loadtime: {
141
- type: String,
142
- default: 'auto'
143
- },
144
- manual: {
145
- type: Boolean,
146
- default: false
147
- }
148
- },
149
- data () {
150
- return {
151
- loading: false,
152
- hasMore: false,
153
- dataList: this.getone ? undefined : [],
154
- paginationInternal: {},
155
- errorMessage: ''
156
- }
157
- },
158
- computed: {
159
- collectionArgs () {
160
- return Array.isArray(this.collection) ? this.collection : [this.collection]
161
- },
162
- isLookup () {
163
- return (Array.isArray(this.collection) && this.collection.length > 1) || (typeof this.collection === 'string' && this.collection.indexOf(',') > -1)
164
- }
165
- },
166
- created () {
167
- this._isEnded = false
168
- this.paginationInternal = {
169
- current: this.pageCurrent,
170
- size: this.pageSize,
171
- count: 0
172
- }
173
-
174
- this.$watch(() => {
175
- var al = []
176
- attrs.forEach(key => {
177
- al.push(this[key])
178
- })
179
- return al
180
- }, (newValue, oldValue) => {
181
- this.paginationInternal.size = this.pageSize
182
- if (newValue[0] !== oldValue[0]) {
183
- this.paginationInternal.current = this.pageCurrent
184
- }
185
- if (this.loadtime === loadMode.manual) {
186
- return
187
- }
188
-
189
- let needReset = false
190
- for (let i = 2; i < newValue.length; i++) {
191
- if (newValue[i] !== oldValue[i]) {
192
- needReset = true
193
- break
194
- }
195
- }
196
- if (needReset) {
197
- this.clear()
198
- this.reset()
199
- }
200
-
201
- this._execLoadData()
202
- })
203
-
204
- // #ifdef H5
205
- if (process.env.NODE_ENV === 'development') {
206
- this._debugDataList = []
207
- if (!window.unidev) {
208
- window.unidev = {
209
- clientDB: {
210
- data: []
211
- }
212
- }
213
- }
214
- window.unidev.clientDB.data.push(this._debugDataList)
215
- }
216
- // #endif
217
-
218
- // #ifdef MP-TOUTIAO
219
- let changeName
220
- const events = this.$scope.dataset.eventOpts || []
221
- for (var i = 0; i < events.length; i++) {
222
- const event = events[i]
223
- if (event[0].includes('^load')) {
224
- changeName = event[1][0][0]
225
- }
226
- }
227
- if (changeName) {
228
- let parent = this.$parent
229
- let maxDepth = 16
230
- this._changeDataFunction = null
231
- while (parent && maxDepth > 0) {
232
- const fun = parent[changeName]
233
- if (fun && typeof fun === 'function') {
234
- this._changeDataFunction = fun
235
- maxDepth = 0
236
- break
237
- }
238
- parent = parent.$parent
239
- maxDepth--
240
- }
241
- }
242
- // #endif
243
-
244
- if (!this.manual && this.loadtime === loadMode.auto) {
245
- this.loadData()
246
- }
247
- },
248
- // #ifdef H5
249
- beforeDestroy () {
250
- if (process.env.NODE_ENV === 'development' && window.unidev) {
251
- var cd = this._debugDataList
252
- var dl = window.unidev.clientDB.data
253
- for (var i = dl.length - 1; i >= 0; i--) {
254
- if (dl[i] === cd) {
255
- dl.splice(i, 1)
256
- break
257
- }
258
- }
259
- }
260
- },
261
- // #endif
262
- methods: {
263
- loadData (args1, args2) {
264
- let callback = null
265
- let clear = false
266
- if (typeof args1 === 'object') {
267
- if (args1.clear) {
268
- if (this.pageData === pageMode.replace) {
269
- this.clear()
270
- } else {
271
- clear = args1.clear
272
- }
273
- this.reset()
274
- }
275
- if (args1.current !== undefined) {
276
- this.paginationInternal.current = args1.current
277
- }
278
- if (typeof args2 === 'function') {
279
- callback = args2
280
- }
281
- } else if (typeof args1 === 'function') {
282
- callback = args1
283
- }
284
-
285
- this._execLoadData(callback, clear)
286
- },
287
- loadMore () {
288
- if (this._isEnded || this.loading) {
289
- return
290
- }
291
-
292
- if (this.pageData === pageMode.add) {
293
- this.paginationInternal.current++
294
- }
295
-
296
- this._execLoadData()
297
- },
298
- refresh () {
299
- this.clear()
300
- this._execLoadData()
301
- },
302
- clear () {
303
- this._isEnded = false
304
- this.dataList = []
305
- },
306
- reset () {
307
- this.paginationInternal.current = 1
308
- },
309
- add (value, {
310
- action,
311
- showToast = true,
312
- toastTitle,
313
- success,
314
- fail,
315
- complete,
316
- needConfirm = true,
317
- needLoading = true,
318
- loadingTitle = ''
319
- } = {}) {
320
- if (needLoading) {
321
- uni.showLoading({
322
- title: loadingTitle
323
- })
324
- }
325
- /* eslint-disable no-undef */
326
- let db = uniCloud.database(this.spaceInfo)
327
- if (action) {
328
- db = db.action(action)
329
- }
330
-
331
- db.collection(this.getMainCollection()).add(value).then((res) => {
332
- success && success(res)
333
- if (showToast) {
334
- uni.showToast({
335
- title: toastTitle || t('uniCloud.component.add.success')
336
- })
337
- }
338
- }).catch((err) => {
339
- fail && fail(err)
340
- if (needConfirm) {
341
- uni.showModal({
342
- content: err.message,
343
- showCancel: false
344
- })
345
- }
346
- }).finally(() => {
347
- if (needLoading) {
348
- uni.hideLoading()
349
- }
350
- complete && complete()
351
- })
352
- },
353
- remove (id, {
354
- action,
355
- success,
356
- fail,
357
- complete,
358
- confirmTitle,
359
- confirmContent,
360
- needConfirm = true,
361
- needLoading = true,
362
- loadingTitle = ''
363
- } = {}) {
364
- if (!id || !id.length) {
365
- return
366
- }
367
- if (!needConfirm) {
368
- this._execRemove(id, action, success, fail, complete, needConfirm, needLoading, loadingTitle)
369
- return
370
- }
371
- uni.showModal({
372
- title: confirmTitle || t('uniCloud.component.remove.showModal.title'),
373
- content: confirmContent || t('uniCloud.component.remove.showModal.content'),
374
- showCancel: true,
375
- success: (res) => {
376
- if (!res.confirm) {
377
- return
378
- }
379
- this._execRemove(id, action, success, fail, complete, needConfirm, needLoading, loadingTitle)
380
- }
381
- })
382
- },
383
- update (id, value, {
384
- action,
385
- showToast = true,
386
- toastTitle,
387
- success,
388
- fail,
389
- complete,
390
- confirmTitle,
391
- confirmContent,
392
- needConfirm = true,
393
- needLoading = true,
394
- loadingTitle = ''
395
- } = {}) {
396
- if (!needConfirm) {
397
- this._doUpdate(id, value, action, success, fail, complete, needConfirm, needLoading, loadingTitle)
398
- return
399
- }
400
- uni.showModal({
401
- title: confirmTitle || t('uniCloud.component.update.showModal.title'),
402
- content: confirmContent || t('uniCloud.component.update.showModal.content'),
403
- showCancel: true,
404
- success: (res) => {
405
- if (!res.confirm) {
406
- return
407
- }
408
- this._doUpdate(id, value, action, success, fail, complete, needConfirm, needLoading, loadingTitle)
409
- }
410
- })
411
- },
412
- _doUpdate (id, value, action, success, fail, complete, needConfirm, needLoading, loadingTitle) {
413
- if (needLoading) {
414
- uni.showLoading({
415
- title: loadingTitle
416
- })
417
- }
418
- /* eslint-disable no-undef */
419
- let db = uniCloud.database(this.spaceInfo)
420
- if (action) {
421
- db = db.action(action)
422
- }
423
-
424
- db.collection(this.getMainCollection()).doc(id).update(value).then((res) => {
425
- success && success(res)
426
- if (showToast) {
427
- uni.showToast({
428
- title: toastTitle || t('uniCloud.component.update.success')
429
- })
430
- }
431
- }).catch((err) => {
432
- fail && fail(err)
433
- if (needConfirm) {
434
- uni.showModal({
435
- content: err.message,
436
- showCancel: false
437
- })
438
- }
439
- }).finally(() => {
440
- if (needLoading) {
441
- uni.hideLoading()
442
- }
443
- complete && complete()
444
- })
445
- },
446
- getMainCollection () {
447
- if (typeof this.collection === 'string') {
448
- return this.collection.split(',')[0]
449
- }
450
- const mainQuery = JSON.parse(JSON.stringify(this.collection[0]))
451
- return mainQuery.$db[0].$param[0]
452
- },
453
- getTemp (isTemp = true) {
454
- /* eslint-disable no-undef */
455
- let db = uniCloud.database(this.spaceInfo)
456
-
457
- if (this.action) {
458
- db = db.action(this.action)
459
- }
460
-
461
- db = db.collection(...this.collectionArgs)
462
-
463
- if (this.foreignKey) {
464
- db = db.foreignKey(this.foreignKey)
465
- }
466
- if (!(!this.where || !Object.keys(this.where).length)) {
467
- db = db.where(this.where)
468
- }
469
- if (this.field) {
470
- db = db.field(this.field)
471
- }
472
- if (this.groupby) {
473
- db = db.groupBy(this.groupby)
474
- }
475
- if (this.groupField) {
476
- db = db.groupField(this.groupField)
477
- }
478
- if (this.distinct === true) {
479
- db = db.distinct()
480
- }
481
- if (this.orderby) {
482
- db = db.orderBy(this.orderby)
483
- }
484
-
485
- const {
486
- current,
487
- size
488
- } = this.paginationInternal
489
- const getOptions = {}
490
- if (this.getcount) {
491
- getOptions.getCount = this.getcount
492
- }
493
- const treeOptions = {
494
- limitLevel: this.limitlevel,
495
- startWith: this.startwith
496
- }
497
- if (this.gettree) {
498
- getOptions.getTree = treeOptions
499
- }
500
- if (this.gettreepath) {
501
- getOptions.getTreePath = treeOptions
502
- }
503
- db = db.skip(size * (current - 1)).limit(size)
504
-
505
- if (isTemp) {
506
- db = db.getTemp(getOptions)
507
- db.udb = this
508
- } else {
509
- db = db.get(getOptions)
510
- }
511
-
512
- return db
513
- },
514
- setResult (result) {
515
- if (result.code === 0) {
516
- this._execLoadDataSuccess(result)
517
- } else {
518
- this._execLoadDataFail(new Error(result.message))
519
- }
520
- },
521
- _execLoadData (callback, clear) {
522
- if (this.loading) {
523
- return
524
- }
525
- this.loading = true
526
- this.errorMessage = ''
527
-
528
- this._getExec().then((res) => {
529
- this.loading = false
530
- this._execLoadDataSuccess(res.result, callback, clear)
531
-
532
- // #ifdef H5
533
- if (process.env.NODE_ENV === 'development') {
534
- this._debugDataList.length = 0
535
- const formatData = JSON.parse(JSON.stringify(this.dataList))
536
- if (Array.isArray(this.dataList)) {
537
- this._debugDataList.push(...formatData)
538
- } else {
539
- this._debugDataList.push(formatData)
540
- }
541
- }
542
- // #endif
543
- }).catch((err) => {
544
- this.loading = false
545
- this._execLoadDataFail(err, callback)
546
- })
547
- },
548
- _execLoadDataSuccess (result, callback, clear) {
549
- const {
550
- data,
551
- count
552
- } = result
553
- this._isEnded = count !== undefined ? (this.paginationInternal.current * this.paginationInternal.size >= count) : (data.length < this.pageSize)
554
- this.hasMore = !this._isEnded
555
-
556
- const data2 = this.getone ? (data.length ? data[0] : undefined) : data
557
-
558
- if (this.getcount) {
559
- this.paginationInternal.count = count
560
- }
561
-
562
- callback && callback(data2, this._isEnded, this.paginationInternal)
563
- this._dispatchEvent(events.load, data2)
564
-
565
- if (this.getone || this.pageData === pageMode.replace) {
566
- this.dataList = data2
567
- } else {
568
- if (clear) {
569
- this.dataList = data2
570
- } else {
571
- this.dataList.push(...data2)
572
- }
573
- }
574
- },
575
- _execLoadDataFail (err, callback) {
576
- this.errorMessage = err
577
- callback && callback()
578
- this.$emit(events.error, err)
579
- if (process.env.NODE_ENV === 'development') {
580
- console.error(err)
581
- }
582
- },
583
- _getExec () {
584
- return this.getTemp(false)
585
- },
586
- _execRemove (id, action, success, fail, complete, needConfirm, needLoading, loadingTitle) {
587
- if (!this.collection || !id) {
588
- return
589
- }
590
-
591
- const ids = Array.isArray(id) ? id : [id]
592
- if (!ids.length) {
593
- return
594
- }
595
-
596
- if (needLoading) {
597
- uni.showLoading({
598
- mask: true,
599
- title: loadingTitle
600
- })
601
- }
602
-
603
- /* eslint-disable no-undef */
604
- const db = uniCloud.database(this.spaceInfo)
605
- const dbCmd = db.command
606
-
607
- let exec = db
608
- if (action) {
609
- exec = exec.action(action)
610
- }
611
-
612
- exec.collection(this.getMainCollection()).where({
613
- _id: dbCmd.in(ids)
614
- }).remove().then((res) => {
615
- success && success(res.result)
616
- if (this.pageData === pageMode.replace) {
617
- this.refresh()
618
- } else {
619
- this.removeData(ids)
620
- }
621
- }).catch((err) => {
622
- fail && fail(err)
623
- if (needConfirm) {
624
- uni.showModal({
625
- content: err.message,
626
- showCancel: false
627
- })
628
- }
629
- }).finally(() => {
630
- if (needLoading) {
631
- uni.hideLoading()
632
- }
633
- complete && complete()
634
- })
635
- },
636
- removeData (ids) {
637
- const il = ids.slice(0)
638
- const dl = this.dataList
639
- for (let i = dl.length - 1; i >= 0; i--) {
640
- const index = il.indexOf(dl[i]._id)
641
- if (index >= 0) {
642
- dl.splice(i, 1)
643
- il.splice(index, 1)
644
- }
645
- }
646
- },
647
- _dispatchEvent (type, data) {
648
- if (this._changeDataFunction) {
649
- this._changeDataFunction(data, this._isEnded, this.paginationInternal)
650
- } else {
651
- this.$emit(type, data, this._isEnded, this.paginationInternal)
652
- }
653
- }
654
- }
655
- }
656
- </script>
10
+ />
11
+ </view>
12
+ </template>
13
+
14
+ <script>
15
+ import {
16
+ initVueI18n
17
+ } from '@dcloudio/uni-i18n'
18
+ import messages from './i18n/index'
19
+
20
+ const {
21
+ t
22
+ } = initVueI18n(messages)
23
+
24
+ const events = {
25
+ load: 'load',
26
+ error: 'error'
27
+ }
28
+ const pageMode = {
29
+ add: 'add',
30
+ replace: 'replace'
31
+ }
32
+ const loadMode = {
33
+ auto: 'auto',
34
+ onready: 'onready',
35
+ manual: 'manual'
36
+ }
37
+
38
+ const attrs = [
39
+ 'pageCurrent',
40
+ 'pageSize',
41
+ 'spaceInfo',
42
+ 'collection',
43
+ 'action',
44
+ 'field',
45
+ 'getcount',
46
+ 'orderby',
47
+ 'where',
48
+ 'groupby',
49
+ 'groupField',
50
+ 'distinct'
51
+ ]
52
+
53
+ export default {
54
+ name: 'UniClouddb',
55
+ props: {
56
+ options: {
57
+ type: [Object, Array],
58
+ default () {
59
+ return {}
60
+ }
61
+ },
62
+ spaceInfo: {
63
+ type: Object,
64
+ default () {
65
+ return {}
66
+ }
67
+ },
68
+ collection: {
69
+ type: [String, Array],
70
+ default: ''
71
+ },
72
+ action: {
73
+ type: String,
74
+ default: ''
75
+ },
76
+ field: {
77
+ type: String,
78
+ default: ''
79
+ },
80
+ orderby: {
81
+ type: String,
82
+ default: ''
83
+ },
84
+ where: {
85
+ type: [String, Object],
86
+ default: ''
87
+ },
88
+ pageData: {
89
+ type: String,
90
+ default: 'add'
91
+ },
92
+ pageCurrent: {
93
+ type: Number,
94
+ default: 1
95
+ },
96
+ pageSize: {
97
+ type: Number,
98
+ default: 20
99
+ },
100
+ getcount: {
101
+ type: [Boolean, String],
102
+ default: false
103
+ },
104
+ getone: {
105
+ type: [Boolean, String],
106
+ default: false
107
+ },
108
+ gettree: {
109
+ type: [Boolean, String, Object],
110
+ default: false
111
+ },
112
+ gettreepath: {
113
+ type: [Boolean, String],
114
+ default: false
115
+ },
116
+ startwith: {
117
+ type: String,
118
+ default: ''
119
+ },
120
+ limitlevel: {
121
+ type: Number,
122
+ default: 10
123
+ },
124
+ groupby: {
125
+ type: String,
126
+ default: ''
127
+ },
128
+ groupField: {
129
+ type: String,
130
+ default: ''
131
+ },
132
+ distinct: {
133
+ type: [Boolean, String],
134
+ default: false
135
+ },
136
+ pageIndistinct: {
137
+ type: [Boolean, String],
138
+ default: false
139
+ },
140
+ foreignKey: {
141
+ type: String,
142
+ default: ''
143
+ },
144
+ loadtime: {
145
+ type: String,
146
+ default: 'auto'
147
+ },
148
+ manual: {
149
+ type: Boolean,
150
+ default: false
151
+ }
152
+ },
153
+ data () {
154
+ return {
155
+ loading: false,
156
+ hasMore: false,
157
+ dataList: this.getone ? undefined : [],
158
+ paginationInternal: {},
159
+ errorMessage: ''
160
+ }
161
+ },
162
+ computed: {
163
+ collectionArgs () {
164
+ return Array.isArray(this.collection) ? this.collection : [this.collection]
165
+ },
166
+ isLookup () {
167
+ return (Array.isArray(this.collection) && this.collection.length > 1) || (typeof this.collection === 'string' &&
168
+ this.collection.indexOf(',') > -1)
169
+ }
170
+ },
171
+ created () {
172
+ this._isEnded = false
173
+ this.paginationInternal = {
174
+ current: this.pageCurrent,
175
+ size: this.pageSize,
176
+ count: 0
177
+ }
178
+
179
+ this.$watch(() => {
180
+ var al = []
181
+ attrs.forEach(key => {
182
+ al.push(this[key])
183
+ })
184
+ return al
185
+ }, (newValue, oldValue) => {
186
+ this.paginationInternal.size = this.pageSize
187
+ if (newValue[0] !== oldValue[0]) {
188
+ this.paginationInternal.current = this.pageCurrent
189
+ }
190
+ if (this.loadtime === loadMode.manual) {
191
+ return
192
+ }
193
+
194
+ let needReset = false
195
+ for (let i = 2; i < newValue.length; i++) {
196
+ if (newValue[i] !== oldValue[i]) {
197
+ needReset = true
198
+ break
199
+ }
200
+ }
201
+ if (needReset) {
202
+ this.clear()
203
+ this.reset()
204
+ }
205
+
206
+ this._execLoadData()
207
+ })
208
+
209
+ // #ifdef H5
210
+ if (process.env.NODE_ENV === 'development') {
211
+ this._debugDataList = []
212
+ if (!window.unidev) {
213
+ window.unidev = {
214
+ clientDB: {
215
+ data: []
216
+ }
217
+ }
218
+ }
219
+ window.unidev.clientDB.data.push(this._debugDataList)
220
+ }
221
+ // #endif
222
+
223
+ // #ifdef MP-TOUTIAO
224
+ let changeName
225
+ const events = this.$scope.dataset.eventOpts || []
226
+ for (var i = 0; i < events.length; i++) {
227
+ const event = events[i]
228
+ if (event[0].includes('^load')) {
229
+ changeName = event[1][0][0]
230
+ }
231
+ }
232
+ if (changeName) {
233
+ let parent = this.$parent
234
+ let maxDepth = 16
235
+ this._changeDataFunction = null
236
+ while (parent && maxDepth > 0) {
237
+ const fun = parent[changeName]
238
+ if (fun && typeof fun === 'function') {
239
+ this._changeDataFunction = fun
240
+ maxDepth = 0
241
+ break
242
+ }
243
+ parent = parent.$parent
244
+ maxDepth--
245
+ }
246
+ }
247
+ // #endif
248
+
249
+ if (!this.manual && this.loadtime === loadMode.auto) {
250
+ this.loadData()
251
+ }
252
+ },
253
+ // #ifdef H5
254
+ beforeDestroy () {
255
+ if (process.env.NODE_ENV === 'development' && window.unidev) {
256
+ var cd = this._debugDataList
257
+ var dl = window.unidev.clientDB.data
258
+ for (var i = dl.length - 1; i >= 0; i--) {
259
+ if (dl[i] === cd) {
260
+ dl.splice(i, 1)
261
+ break
262
+ }
263
+ }
264
+ }
265
+ },
266
+ // #endif
267
+ methods: {
268
+ loadData (args1, args2) {
269
+ let callback = null
270
+ let clear = false
271
+ if (typeof args1 === 'object') {
272
+ if (args1.clear) {
273
+ if (this.pageData === pageMode.replace) {
274
+ this.clear()
275
+ } else {
276
+ clear = args1.clear
277
+ }
278
+ this.reset()
279
+ }
280
+ if (args1.current !== undefined) {
281
+ this.paginationInternal.current = args1.current
282
+ }
283
+ if (typeof args2 === 'function') {
284
+ callback = args2
285
+ }
286
+ } else if (typeof args1 === 'function') {
287
+ callback = args1
288
+ }
289
+
290
+ this._execLoadData(callback, clear)
291
+ },
292
+ loadMore () {
293
+ if (this._isEnded || this.loading) {
294
+ return
295
+ }
296
+
297
+ if (this.pageData === pageMode.add) {
298
+ this.paginationInternal.current++
299
+ }
300
+
301
+ this._execLoadData()
302
+ },
303
+ refresh () {
304
+ this.clear()
305
+ this._execLoadData()
306
+ },
307
+ clear () {
308
+ this._isEnded = false
309
+ this.dataList = []
310
+ },
311
+ reset () {
312
+ this.paginationInternal.current = 1
313
+ },
314
+ add (value, {
315
+ action,
316
+ showToast = true,
317
+ toastTitle,
318
+ success,
319
+ fail,
320
+ complete,
321
+ needConfirm = true,
322
+ needLoading = true,
323
+ loadingTitle = ''
324
+ } = {}) {
325
+ if (needLoading) {
326
+ uni.showLoading({
327
+ title: loadingTitle
328
+ })
329
+ }
330
+ /* eslint-disable no-undef */
331
+ let db = uniCloud.database(this.spaceInfo)
332
+ if (action) {
333
+ db = db.action(action)
334
+ }
335
+
336
+ db.collection(this.getMainCollection()).add(value).then((res) => {
337
+ success && success(res)
338
+ if (showToast) {
339
+ uni.showToast({
340
+ title: toastTitle || t('uniCloud.component.add.success')
341
+ })
342
+ }
343
+ }).catch((err) => {
344
+ fail && fail(err)
345
+ if (needConfirm) {
346
+ uni.showModal({
347
+ content: err.message,
348
+ showCancel: false
349
+ })
350
+ }
351
+ }).finally(() => {
352
+ if (needLoading) {
353
+ uni.hideLoading()
354
+ }
355
+ complete && complete()
356
+ })
357
+ },
358
+ remove (id, {
359
+ action,
360
+ success,
361
+ fail,
362
+ complete,
363
+ confirmTitle,
364
+ confirmContent,
365
+ needConfirm = true,
366
+ needLoading = true,
367
+ loadingTitle = ''
368
+ } = {}) {
369
+ if (!id || !id.length) {
370
+ return
371
+ }
372
+ if (!needConfirm) {
373
+ this._execRemove(id, action, success, fail, complete, needConfirm, needLoading, loadingTitle)
374
+ return
375
+ }
376
+ uni.showModal({
377
+ title: confirmTitle || t('uniCloud.component.remove.showModal.title'),
378
+ content: confirmContent || t('uniCloud.component.remove.showModal.content'),
379
+ showCancel: true,
380
+ success: (res) => {
381
+ if (!res.confirm) {
382
+ return
383
+ }
384
+ this._execRemove(id, action, success, fail, complete, needConfirm, needLoading, loadingTitle)
385
+ }
386
+ })
387
+ },
388
+ update (id, value, options = {}) {
389
+ const fixOptions = Object.assign({
390
+ action: null,
391
+ showToast: true,
392
+ toastTitle: null,
393
+ success: null,
394
+ fail: null,
395
+ complete: null,
396
+ confirmTitle: null,
397
+ confirmContent: null,
398
+ needConfirm: true,
399
+ needLoading: true,
400
+ loadingTitle: ''
401
+ }, options)
402
+ if (!options.needConfirm) {
403
+ uni.showModal({
404
+ title: options.confirmTitle || t('uniCloud.component.update.showModal.title'),
405
+ content: options.confirmContent || t('uniCloud.component.update.showModal.content'),
406
+ showCancel: true,
407
+ success: (res) => {
408
+ if (res.confirm) {
409
+ this._doUpdate(id, value, fixOptions)
410
+ }
411
+ }
412
+ })
413
+ } else {
414
+ this._doUpdate(id, value, fixOptions)
415
+ }
416
+ },
417
+ _doUpdate (id, value, options) {
418
+ const {
419
+ action,
420
+ success,
421
+ fail,
422
+ complete,
423
+ showToast,
424
+ toastTitle,
425
+ needConfirm,
426
+ needLoading,
427
+ loadingTitle
428
+ } = options
429
+
430
+ if (needLoading) {
431
+ uni.showLoading({
432
+ title: loadingTitle
433
+ })
434
+ }
435
+ /* eslint-disable no-undef */
436
+ let db = uniCloud.database(this.spaceInfo)
437
+ if (action) {
438
+ db = db.action(action)
439
+ }
440
+
441
+ db.collection(this.getMainCollection()).doc(id).update(value).then((res) => {
442
+ success && success(res)
443
+ if (showToast) {
444
+ uni.showToast({
445
+ title: toastTitle || t('uniCloud.component.update.success')
446
+ })
447
+ }
448
+ }).catch((err) => {
449
+ fail && fail(err)
450
+ if (needConfirm) {
451
+ uni.showModal({
452
+ content: err.message,
453
+ showCancel: false
454
+ })
455
+ }
456
+ }).finally(() => {
457
+ if (needLoading) {
458
+ uni.hideLoading()
459
+ }
460
+ complete && complete()
461
+ })
462
+ },
463
+ getMainCollection () {
464
+ if (typeof this.collection === 'string') {
465
+ return this.collection.split(',')[0]
466
+ }
467
+ const mainQuery = JSON.parse(JSON.stringify(this.collection[0]))
468
+ return mainQuery.$db[0].$param[0]
469
+ },
470
+ getTemp (isTemp = true) {
471
+ /* eslint-disable no-undef */
472
+ let db = uniCloud.database(this.spaceInfo)
473
+
474
+ if (this.action) {
475
+ db = db.action(this.action)
476
+ }
477
+
478
+ db = db.collection(...this.collectionArgs)
479
+
480
+ if (this.foreignKey) {
481
+ db = db.foreignKey(this.foreignKey)
482
+ }
483
+ if (!(!this.where || !Object.keys(this.where).length)) {
484
+ db = db.where(this.where)
485
+ }
486
+ if (this.field) {
487
+ db = db.field(this.field)
488
+ }
489
+ if (this.groupby) {
490
+ db = db.groupBy(this.groupby)
491
+ }
492
+ if (this.groupField) {
493
+ db = db.groupField(this.groupField)
494
+ }
495
+ if (this.distinct === true) {
496
+ db = db.distinct()
497
+ }
498
+ if (this.orderby) {
499
+ db = db.orderBy(this.orderby)
500
+ }
501
+
502
+ const {
503
+ current,
504
+ size
505
+ } = this.paginationInternal
506
+ const getOptions = {}
507
+ if (this.getcount) {
508
+ getOptions.getCount = this.getcount
509
+ }
510
+ const treeOptions = {
511
+ limitLevel: this.limitlevel,
512
+ startWith: this.startwith
513
+ }
514
+ if (this.gettree) {
515
+ getOptions.getTree = treeOptions
516
+ }
517
+ if (this.gettreepath) {
518
+ getOptions.getTreePath = treeOptions
519
+ }
520
+ db = db.skip(size * (current - 1)).limit(size)
521
+
522
+ if (isTemp) {
523
+ db = db.getTemp(getOptions)
524
+ db.udb = this
525
+ } else {
526
+ db = db.get(getOptions)
527
+ }
528
+
529
+ return db
530
+ },
531
+ setResult (result) {
532
+ if (result.code === 0) {
533
+ this._execLoadDataSuccess(result)
534
+ } else {
535
+ this._execLoadDataFail(new Error(result.message))
536
+ }
537
+ },
538
+ _execLoadData (callback, clear) {
539
+ if (this.loading) {
540
+ return
541
+ }
542
+ this.loading = true
543
+ this.errorMessage = ''
544
+
545
+ this._getExec().then((res) => {
546
+ this.loading = false
547
+ this._execLoadDataSuccess(res.result, callback, clear)
548
+
549
+ // #ifdef H5
550
+ if (process.env.NODE_ENV === 'development') {
551
+ this._debugDataList.length = 0
552
+ const formatData = JSON.parse(JSON.stringify(this.dataList))
553
+ if (Array.isArray(this.dataList)) {
554
+ this._debugDataList.push(...formatData)
555
+ } else {
556
+ this._debugDataList.push(formatData)
557
+ }
558
+ }
559
+ // #endif
560
+ }).catch((err) => {
561
+ this.loading = false
562
+ this._execLoadDataFail(err, callback)
563
+ })
564
+ },
565
+ _execLoadDataSuccess (result, callback, clear) {
566
+ const {
567
+ data,
568
+ count
569
+ } = result
570
+ this._isEnded = count !== undefined ? (this.paginationInternal.current * this.paginationInternal.size >=
571
+ count) : (data.length < this.pageSize)
572
+ this.hasMore = !this._isEnded
573
+
574
+ const data2 = this.getone ? (data.length ? data[0] : undefined) : data
575
+
576
+ if (this.getcount) {
577
+ this.paginationInternal.count = count
578
+ }
579
+
580
+ callback && callback(data2, this._isEnded, this.paginationInternal)
581
+ this._dispatchEvent(events.load, data2)
582
+
583
+ if (this.getone || this.pageData === pageMode.replace) {
584
+ this.dataList = data2
585
+ } else {
586
+ if (clear) {
587
+ this.dataList = data2
588
+ } else {
589
+ this.dataList.push(...data2)
590
+ }
591
+ }
592
+ },
593
+ _execLoadDataFail (err, callback) {
594
+ this.errorMessage = err
595
+ callback && callback()
596
+ this.$emit(events.error, err)
597
+ if (process.env.NODE_ENV === 'development') {
598
+ console.error(err)
599
+ }
600
+ },
601
+ _getExec () {
602
+ return this.getTemp(false)
603
+ },
604
+ _execRemove (id, action, success, fail, complete, needConfirm, needLoading, loadingTitle) {
605
+ if (!this.collection || !id) {
606
+ return
607
+ }
608
+
609
+ const ids = Array.isArray(id) ? id : [id]
610
+ if (!ids.length) {
611
+ return
612
+ }
613
+
614
+ if (needLoading) {
615
+ uni.showLoading({
616
+ mask: true,
617
+ title: loadingTitle
618
+ })
619
+ }
620
+
621
+ /* eslint-disable no-undef */
622
+ const db = uniCloud.database(this.spaceInfo)
623
+ const dbCmd = db.command
624
+
625
+ let exec = db
626
+ if (action) {
627
+ exec = exec.action(action)
628
+ }
629
+
630
+ exec.collection(this.getMainCollection()).where({
631
+ _id: dbCmd.in(ids)
632
+ }).remove().then((res) => {
633
+ success && success(res.result)
634
+ if (this.pageData === pageMode.replace) {
635
+ this.refresh()
636
+ } else {
637
+ this.removeData(ids)
638
+ }
639
+ }).catch((err) => {
640
+ fail && fail(err)
641
+ if (needConfirm) {
642
+ uni.showModal({
643
+ content: err.message,
644
+ showCancel: false
645
+ })
646
+ }
647
+ }).finally(() => {
648
+ if (needLoading) {
649
+ uni.hideLoading()
650
+ }
651
+ complete && complete()
652
+ })
653
+ },
654
+ removeData (ids) {
655
+ const il = ids.slice(0)
656
+ const dl = this.dataList
657
+ for (let i = dl.length - 1; i >= 0; i--) {
658
+ const index = il.indexOf(dl[i]._id)
659
+ if (index >= 0) {
660
+ dl.splice(i, 1)
661
+ il.splice(index, 1)
662
+ }
663
+ }
664
+ },
665
+ _dispatchEvent (type, data) {
666
+ if (this._changeDataFunction) {
667
+ this._changeDataFunction(data, this._isEnded, this.paginationInternal)
668
+ } else {
669
+ this.$emit(type, data, this._isEnded, this.paginationInternal)
670
+ }
671
+ }
672
+ }
673
+ }
674
+ </script>