Blame view

泰额版/Food Labeling Management App UniApp/src/pages/more/sync.vue 13.6 KB
59e51671   “wangming”   1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  <template>
    <view class="page">
      <view class="header-hero" :style="{ paddingTop: statusBarHeight + 'px' }">
        <view class="top-bar">
          <view class="top-left" @click="goBack">
            <AppIcon name="chevronLeft" size="sm" color="white" />
          </view>
          <view class="top-center">
            <text class="page-title">{{ t('sync.title') }}</text>
            <LocationPicker />
          </view>
          <view class="top-right" @click="isMenuOpen = true">
            <AppIcon name="menu" size="sm" color="white" />
          </view>
        </view>
      </view>
  
      <view class="content">
        <view class="status-card">
          <view class="status-top">
            <view class="status-icon"><AppIcon :name="syncData.pendingMutations > 0 ? 'refresh' : 'check'" size="lg" color="green" /></view>
            <view>
f64eecbf   杨鑫   前端更新
23
24
              <text class="status-title">{{ syncData.pendingMutations > 0 ? t('sync.pendingLocalChanges') : t('sync.allSynced') }}</text>
              <text class="status-time">{{ t('sync.lastSync') }}: {{ syncData.lastSync }} · {{ syncData.online ? t('common.online') : t('common.offline') }}</text>
59e51671   “wangming”   1
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
            </view>
          </view>
          <view class="sync-btn" @click="handleSync">
            <AppIcon name="refresh" size="sm" color="white" />
            <text class="sync-btn-text">{{ t('sync.syncNow') }}</text>
          </view>
        </view>
  
        <text class="section-title">{{ t('sync.details') }}</text>
  
        <view class="detail-card">
          <view class="detail-row">
            <view class="detail-left">
              <view class="detail-icon blue"><AppIcon name="cloud" size="md" color="blue" /></view>
              <view>
                <text class="detail-name">{{ t('sync.labels') }}</text>
                <text class="detail-count">{{ syncData.labels.synced }} {{ t('sync.of') }} {{ syncData.labels.total }} {{ t('sync.synced') }}</text>
              </view>
            </view>
            <AppIcon :name="syncData.labels.pending > 0 ? 'refresh' : 'check'" size="md" color="green" />
          </view>
        </view>
  
        <view class="detail-card">
          <view class="detail-row">
            <view class="detail-left">
              <view class="detail-icon purple"><AppIcon name="cloud" size="md" color="purple" /></view>
              <view>
                <text class="detail-name">{{ t('sync.photos') }}</text>
                <text class="detail-count">{{ syncData.photos.synced }} {{ t('sync.of') }} {{ syncData.photos.total }} {{ t('sync.synced') }}</text>
              </view>
            </view>
            <AppIcon :name="syncData.photos.pending > 0 ? 'refresh' : 'check'" size="md" color="green" />
          </view>
        </view>
  
f64eecbf   杨鑫   前端更新
61
        <text class="section-title">{{ t('sync.pendingChanges') }}</text>
59e51671   “wangming”   1
62
63
        <view class="detail-card">
          <view v-if="pendingList.length === 0" class="pending-empty">
f64eecbf   杨鑫   前端更新
64
            <text class="pending-empty-text">{{ t('sync.noPendingMutations') }}</text>
59e51671   “wangming”   1
65
66
67
          </view>
          <view v-if="pendingList.length > 0" class="pending-tools">
            <view class="pending-tool-btn ghost" @click="handleClearFailed">
f64eecbf   杨鑫   前端更新
68
              <text class="pending-tool-text">{{ t('sync.clearFailed') }}</text>
59e51671   “wangming”   1
69
70
71
72
73
74
75
76
77
78
79
80
            </view>
          </view>
          <view v-if="pendingList.length > 0" class="pending-list">
            <view v-for="row in pendingList" :key="row.id" class="pending-item">
              <view class="pending-main">
                <text class="pending-method">{{ row.method }}</text>
                <text class="pending-endpoint">{{ row.endpoint }}</text>
              </view>
              <text class="pending-time">{{ formatPendingTime(row.createdAt) }}</text>
              <text v-if="row.lastError" class="pending-error">{{ row.lastError }}</text>
              <view class="pending-actions">
                <view class="pending-action-btn primary" @click="handleRetryOne(row.id)">
f64eecbf   杨鑫   前端更新
81
                  <text class="pending-action-text">{{ t('sync.retry') }}</text>
59e51671   “wangming”   1
82
83
                </view>
                <view class="pending-action-btn danger" @click="handleRemoveOne(row.id)">
f64eecbf   杨鑫   前端更新
84
                  <text class="pending-action-text">{{ t('sync.remove') }}</text>
59e51671   “wangming”   1
85
86
87
88
89
90
                </view>
              </view>
            </view>
          </view>
        </view>
  
f64eecbf   杨鑫   前端更新
91
        <text class="section-title">{{ t('sync.lastSyncResult') }}</text>
59e51671   “wangming”   1
92
93
        <view class="detail-card">
          <view class="stat-row">
f64eecbf   杨鑫   前端更新
94
            <text class="stat-key">{{ t('sync.total') }}</text>
59e51671   “wangming”   1
95
96
97
            <text class="stat-val">{{ syncExec.total }}</text>
          </view>
          <view class="stat-row">
f64eecbf   杨鑫   前端更新
98
            <text class="stat-key">{{ t('sync.success') }}</text>
59e51671   “wangming”   1
99
100
101
            <text class="stat-val ok">{{ syncExec.success }}</text>
          </view>
          <view class="stat-row">
f64eecbf   杨鑫   前端更新
102
            <text class="stat-key">{{ t('sync.failed') }}</text>
59e51671   “wangming”   1
103
104
105
            <text class="stat-val bad">{{ syncExec.failed }}</text>
          </view>
          <view class="stat-row">
f64eecbf   杨鑫   前端更新
106
            <text class="stat-key">{{ t('sync.at') }}</text>
59e51671   “wangming”   1
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
            <text class="stat-val">{{ formatPendingTime(syncExec.at) }}</text>
          </view>
        </view>
  
        <view class="info-card">
          <text>{{ t('sync.autoSyncInfo') }}</text>
        </view>
      </view>
  
      <SideMenu v-model="isMenuOpen" />
    </view>
  </template>
  
  <script setup lang="ts">
  import { ref } from 'vue'
  import { useI18n } from 'vue-i18n'
  import { onShow } from '@dcloudio/uni-app'
  import AppIcon from '../../components/AppIcon.vue'
  import SideMenu from '../../components/SideMenu.vue'
  import LocationPicker from '../../components/LocationPicker.vue'
  import { getStatusBarHeight } from '../../utils/statusBar'
  import {
    clearFailedOfflineMutationItems,
    getOfflineLastSyncExecutionDetail,
    getOfflinePendingMutationPreviewList,
    getOfflineSyncStats,
    removeOfflineMutation,
    retryOfflineMutationById,
    syncNowAndRefreshCaches,
  } from '../../utils/offlineSyncManager'
  import type { OfflinePendingMutationPreview, OfflineSyncExecutionDetail } from '../../utils/offlineSyncManager'
  
  const { t } = useI18n()
  const statusBarHeight = getStatusBarHeight()
  const isMenuOpen = ref(false)
  const syncData = ref({
    lastSync: '—',
    labels: { total: 0, synced: 0, pending: 0 },
    photos: { total: 0, synced: 0, pending: 0 },
    pendingMutations: 0,
    online: false,
  })
  const pendingList = ref<OfflinePendingMutationPreview[]>([])
  const syncExec = ref<OfflineSyncExecutionDetail>({
    total: 0,
    success: 0,
    failed: 0,
    at: 0,
  })
  
  function formatLastSync(ts: number): string {
    if (!ts) return '—'
    const diff = Math.max(0, Math.floor((Date.now() - ts) / 1000))
f64eecbf   杨鑫   前端更新
160
161
162
163
    if (diff < 60) return t('sync.justNow')
    if (diff < 3600) return t('sync.minutesAgo', { count: Math.floor(diff / 60) })
    if (diff < 86400) return t('sync.hoursAgo', { count: Math.floor(diff / 3600) })
    return t('sync.daysAgo', { count: Math.floor(diff / 86400) })
59e51671   “wangming”   1
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
  }
  
  async function refreshStats() {
    try {
      const [s, pending] = await Promise.all([
        getOfflineSyncStats(),
        getOfflinePendingMutationPreviewList(20),
      ])
      syncData.value = {
        lastSync: formatLastSync(s.lastSyncAt),
        labels: s.labels,
        photos: s.photos,
        pendingMutations: s.pendingMutations,
        online: s.online,
      }
      pendingList.value = pending
      syncExec.value = getOfflineLastSyncExecutionDetail()
    } catch {
      // ignore
    }
  }
  
  function formatPendingTime(ts: number): string {
    if (!ts) return '—'
    const d = new Date(ts)
    if (!Number.isFinite(d.getTime())) return '—'
    return d.toLocaleString('en-US', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
    })
  }
  
  const goBack = () => {
    const pages = getCurrentPages()
    if (pages.length > 1) {
      uni.navigateBack()
    } else {
      uni.redirectTo({ url: '/pages/index/index' })
    }
  }
  
  const handleSync = async () => {
    if (!syncData.value.online) {
f64eecbf   杨鑫   前端更新
210
      uni.showToast({ title: t('sync.offlineSyncTip'), icon: 'none', duration: 2500 })
59e51671   “wangming”   1
211
212
213
214
215
216
217
218
219
      return
    }
    uni.showLoading({ title: t('sync.syncing'), mask: true })
    try {
      await syncNowAndRefreshCaches()
      await refreshStats()
      uni.showToast({ title: t('sync.syncSuccess'), icon: 'success' })
    } catch (e: unknown) {
      const msg = e instanceof Error ? e.message : String(e)
f64eecbf   杨鑫   前端更新
220
      uni.showToast({ title: msg || t('sync.syncFailed'), icon: 'none', duration: 2500 })
59e51671   “wangming”   1
221
222
223
224
225
226
227
    } finally {
      uni.hideLoading()
    }
  }
  
  const handleRetryOne = async (id: number) => {
    if (!syncData.value.online) {
f64eecbf   杨鑫   前端更新
228
      uni.showToast({ title: t('sync.offlineSyncTip'), icon: 'none', duration: 2500 })
59e51671   “wangming”   1
229
230
      return
    }
f64eecbf   杨鑫   前端更新
231
    uni.showLoading({ title: t('sync.retrying'), mask: true })
59e51671   “wangming”   1
232
233
234
    try {
      const r = await retryOfflineMutationById(id)
      if (r.ok) {
f64eecbf   杨鑫   前端更新
235
        uni.showToast({ title: t('sync.retrySuccess'), icon: 'success' })
59e51671   “wangming”   1
236
      } else {
f64eecbf   杨鑫   前端更新
237
        uni.showToast({ title: r.message || t('sync.retryFailed'), icon: 'none', duration: 2500 })
59e51671   “wangming”   1
238
239
240
241
242
243
244
245
246
      }
      await refreshStats()
    } finally {
      uni.hideLoading()
    }
  }
  
  const handleRemoveOne = async (id: number) => {
    await removeOfflineMutation(id)
f64eecbf   杨鑫   前端更新
247
    uni.showToast({ title: t('sync.removed'), icon: 'success' })
59e51671   “wangming”   1
248
249
250
251
252
    await refreshStats()
  }
  
  const handleClearFailed = async () => {
    const count = await clearFailedOfflineMutationItems()
f64eecbf   杨鑫   前端更新
253
    uni.showToast({ title: t('sync.clearedItems', { count }), icon: 'success' })
59e51671   “wangming”   1
254
255
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
    await refreshStats()
  }
  
  onShow(() => {
    refreshStats()
  })
  </script>
  
  <style scoped>
  .page {
    min-height: 100vh;
    background: #f9fafb;
  }
  
  .header-hero {
    background: linear-gradient(135deg, var(--theme-primary), var(--theme-primary-dark));
    padding: 16rpx 32rpx 24rpx;
  }
  
  .top-bar {
    height: 96rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  
  .top-left,
  .top-right {
    width: 64rpx;
    height: 64rpx;
    border-radius: 999rpx;
    background: rgba(255, 255, 255, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .top-center {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  
  .page-title {
    font-size: 34rpx;
    font-weight: 600;
    color: #fff;
  }
  
  .content {
    padding: 48rpx;
  }
  
  .status-card {
    background: #fff;
    padding: 48rpx;
    border-radius: 20rpx;
    margin-bottom: 48rpx;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
  }
  
  .status-top {
    display: flex;
    align-items: flex-start;
    gap: 32rpx;
    margin-bottom: 32rpx;
  }
  
  .status-icon {
    width: 96rpx;
    height: 96rpx;
    background: #f0fdf4;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }
  
  .status-title {
    font-size: 36rpx;
    font-weight: 600;
    color: #111827;
    display: block;
    margin-bottom: 8rpx;
  }
  
  .status-time {
    font-size: 28rpx;
    color: #6b7280;
  }
  
  .sync-btn {
    width: 100%;
    height: 96rpx;
    background: var(--theme-primary);
    border-radius: 16rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12rpx;
  }
  
  .sync-btn-text {
    font-size: 30rpx;
    font-weight: 600;
    color: #fff;
    line-height: 1;
  }
  
  .section-title {
    font-size: 32rpx;
    font-weight: 600;
    color: #111827;
    display: block;
    margin-bottom: 24rpx;
  }
  
  .detail-card {
    background: #fff;
    padding: 32rpx;
    border-radius: 20rpx;
    margin-bottom: 24rpx;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
  }
  
  .detail-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .detail-left {
    display: flex;
    align-items: center;
    gap: 24rpx;
  }
  
  .detail-icon {
    padding: 16rpx;
    border-radius: 16rpx;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .detail-icon.blue {
    background: #eff6ff;
  }
  
  .detail-icon.green {
    background: #f0fdf4;
  }
  
  .detail-icon.purple {
    background: #faf5ff;
  }
  
  .detail-name {
    font-size: 30rpx;
    font-weight: 500;
    color: #111827;
    display: block;
  }
  
  .detail-count {
    font-size: 26rpx;
    color: #6b7280;
  }
  
  .pending-empty {
    padding: 12rpx 0;
  }
  
  .pending-empty-text {
    font-size: 24rpx;
    color: #9ca3af;
  }
  
  .pending-list {
    display: flex;
    flex-direction: column;
    gap: 20rpx;
  }
  
  .pending-tools {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 12rpx;
  }
  
  .pending-tool-btn {
    min-width: 160rpx;
    height: 56rpx;
    padding: 0 16rpx;
    border-radius: 12rpx;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .pending-tool-btn.ghost {
    background: #eef2ff;
  }
  
  .pending-tool-text {
    font-size: 22rpx;
    color: #1e3a8a;
    font-weight: 600;
  }
  
  .pending-item {
    border-bottom: 1rpx solid #eef2f7;
    padding-bottom: 16rpx;
  }
  
  .pending-item:last-child {
    border-bottom: 0;
    padding-bottom: 0;
  }
  
  .pending-main {
    display: flex;
    align-items: center;
    gap: 12rpx;
  }
  
  .pending-method {
    font-size: 22rpx;
    color: #1e3a8a;
    font-weight: 700;
    min-width: 72rpx;
  }
  
  .pending-endpoint {
    font-size: 23rpx;
    color: #1f2937;
    flex: 1;
  }
  
  .pending-time {
    margin-top: 6rpx;
    font-size: 22rpx;
    color: #6b7280;
  }
  
  .pending-error {
    margin-top: 6rpx;
    font-size: 22rpx;
    color: #b91c1c;
  }
  
  .pending-actions {
    margin-top: 10rpx;
    display: flex;
    gap: 12rpx;
  }
  
  .pending-action-btn {
    min-width: 120rpx;
    height: 52rpx;
    border-radius: 10rpx;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .pending-action-btn.primary {
    background: #1e40af;
  }
  
  .pending-action-btn.danger {
    background: #b91c1c;
  }
  
  .pending-action-text {
    font-size: 22rpx;
    color: #fff;
    font-weight: 600;
  }
  
  .stat-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8rpx 0;
    border-bottom: 1rpx solid #eef2f7;
  }
  
  .stat-row:last-child {
    border-bottom: 0;
  }
  
  .stat-key {
    font-size: 24rpx;
    color: #6b7280;
  }
  
  .stat-val {
    font-size: 24rpx;
    color: #111827;
    font-weight: 600;
  }
  
  .stat-val.ok {
    color: #15803d;
  }
  
  .stat-val.bad {
    color: #b91c1c;
  }
  
  .info-card {
    padding: 32rpx;
    background: #eff6ff;
    border: 1rpx solid #bfdbfe;
    border-radius: 16rpx;
    margin-top: 16rpx;
  }
  
  .info-card text {
    font-size: 26rpx;
    color: #1e3a8a;
    line-height: 1.5;
  }
  </style>