Blame view

泰额版/Food Labeling Management App UniApp/src/utils/sqliteSync.ts 16.4 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
23
24
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
160
161
162
163
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
210
211
212
213
214
215
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
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
  declare const plus: any
  
  type JsonValue = unknown
  
  export type MutationRow = {
    id: number
    endpoint: string
    method: 'POST' | 'PUT' | 'DELETE'
    payloadJson: string
    createdAt: number
    status: number
    lastError: string | null
  }
  
  const DB_NAME = 'us_app_offline.db'
  const CACHE_TABLE = 'offline_cache'
  const MUTATION_TABLE = 'offline_mutation_queue'
  const AUTH_TABLE = 'offline_auth_account'
  
  const FALLBACK_CACHE_KEY = '__offline_cache_json__'
  const FALLBACK_MUTATION_KEY = '__offline_mutation_queue_json__'
  const FALLBACK_AUTH_KEY = '__offline_auth_accounts_json__'
  
  export type OfflineAuthAccountRow = {
    email: string
    password: string
    token: string
    refreshToken: string
    locations: import('../types/usAppBound').UsAppBoundLocationDto[]
    displayName: string
    updatedAt: number
  }
  
  let initialized = false
  
  function isAppSqliteAvailable(): boolean {
    return typeof plus !== 'undefined' && !!plus?.sqlite
  }
  
  function nowTs(): number {
    return Date.now()
  }
  
  async function getNetworkType(): Promise<string> {
    return new Promise((resolve) => {
      uni.getNetworkType({
        success: (res) => resolve(String(res.networkType || 'unknown')),
        fail: () => resolve('unknown'),
      })
    })
  }
  
  export async function isNetworkOnline(): Promise<boolean> {
    const t = (await getNetworkType()).toLowerCase()
    return t !== 'none' && t !== 'unknown'
  }
  
  function getStorageMap<T>(key: string): Record<string, T> {
    try {
      const raw = String(uni.getStorageSync(key) || '').trim()
      if (!raw) return {}
      const parsed = JSON.parse(raw) as unknown
      if (parsed && typeof parsed === 'object') return parsed as Record<string, T>
      return {}
    } catch {
      return {}
    }
  }
  
  function setStorageMap<T>(key: string, value: Record<string, T>): void {
    uni.setStorageSync(key, JSON.stringify(value))
  }
  
  function getStorageArray<T>(key: string): T[] {
    try {
      const raw = String(uni.getStorageSync(key) || '').trim()
      if (!raw) return []
      const parsed = JSON.parse(raw) as unknown
      if (Array.isArray(parsed)) return parsed as T[]
      return []
    } catch {
      return []
    }
  }
  
  function setStorageArray<T>(key: string, value: T[]): void {
    uni.setStorageSync(key, JSON.stringify(value))
  }
  
  function cachePk(module: string, cacheKey: string): string {
    return `${module}::${cacheKey}`
  }
  
  function sqliteOpen(): Promise<void> {
    if (!isAppSqliteAvailable()) return Promise.resolve()
    return new Promise((resolve, reject) => {
      plus.sqlite.openDatabase({
        name: DB_NAME,
        path: `_doc/${DB_NAME}`,
        success: () => resolve(),
        fail: (e: any) => {
          const msg = String(e?.message || e || '')
          if (msg.toLowerCase().includes('already')) {
            resolve()
            return
          }
          reject(new Error(msg || 'open sqlite failed'))
        },
      })
    })
  }
  
  function sqliteExecute(sql: string): Promise<void> {
    return new Promise((resolve, reject) => {
      plus.sqlite.executeSql({
        name: DB_NAME,
        sql,
        success: () => resolve(),
        fail: (e: any) => reject(new Error(String(e?.message || e || 'execute sql failed'))),
      })
    })
  }
  
  function sqliteSelect(sql: string): Promise<any[]> {
    return new Promise((resolve, reject) => {
      plus.sqlite.selectSql({
        name: DB_NAME,
        sql,
        success: (rows: any[]) => resolve(Array.isArray(rows) ? rows : []),
        fail: (e: any) => reject(new Error(String(e?.message || e || 'select sql failed'))),
      })
    })
  }
  
  function esc(value: string): string {
    return value.replace(/'/g, "''")
  }
  
  export async function initOfflineSqlite(): Promise<void> {
    if (initialized) return
    if (!isAppSqliteAvailable()) {
      initialized = true
      return
    }
    await sqliteOpen()
    await sqliteExecute(
      `CREATE TABLE IF NOT EXISTS ${CACHE_TABLE} (
        cache_key TEXT PRIMARY KEY,
        module_name TEXT NOT NULL,
        cache_name TEXT NOT NULL,
        payload_json TEXT NOT NULL,
        updated_at INTEGER NOT NULL
      )`
    )
    await sqliteExecute(
      `CREATE TABLE IF NOT EXISTS ${MUTATION_TABLE} (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        endpoint TEXT NOT NULL,
        method TEXT NOT NULL,
        payload_json TEXT NOT NULL,
        created_at INTEGER NOT NULL,
        status INTEGER NOT NULL DEFAULT 0,
        last_error TEXT
      )`
    )
    await sqliteExecute(
      `CREATE TABLE IF NOT EXISTS ${AUTH_TABLE} (
        email TEXT PRIMARY KEY,
        password TEXT NOT NULL,
        token TEXT NOT NULL,
        refresh_token TEXT,
        locations_json TEXT NOT NULL,
        display_name TEXT,
        updated_at INTEGER NOT NULL
      )`
    )
    initialized = true
  }
  
  export async function setOfflineCache(module: string, name: string, payload: JsonValue): Promise<void> {
    const key = cachePk(module, name)
    const payloadJson = JSON.stringify(payload ?? null)
    const updatedAt = nowTs()
    if (!isAppSqliteAvailable()) {
      const map = getStorageMap<{ module: string; name: string; payloadJson: string; updatedAt: number }>(FALLBACK_CACHE_KEY)
      map[key] = { module, name, payloadJson, updatedAt }
      setStorageMap(FALLBACK_CACHE_KEY, map)
      return
    }
    await initOfflineSqlite()
    const sql = `INSERT OR REPLACE INTO ${CACHE_TABLE}
      (cache_key, module_name, cache_name, payload_json, updated_at)
      VALUES ('${esc(key)}', '${esc(module)}', '${esc(name)}', '${esc(payloadJson)}', ${updatedAt})`
    await sqliteExecute(sql)
  }
  
  export async function hasOfflineCache(module: string, name: string): Promise<boolean> {
    const key = cachePk(module, name)
    if (!isAppSqliteAvailable()) {
      const map = getStorageMap<{ payloadJson: string }>(FALLBACK_CACHE_KEY)
      return Object.prototype.hasOwnProperty.call(map, key)
    }
    await initOfflineSqlite()
    const rows = await sqliteSelect(
      `SELECT 1 FROM ${CACHE_TABLE} WHERE cache_key='${esc(key)}' LIMIT 1`
    )
    return rows.length > 0
  }
  
  export async function getOfflineCache<T = unknown>(module: string, name: string): Promise<T | null> {
    const key = cachePk(module, name)
    if (!isAppSqliteAvailable()) {
      const map = getStorageMap<{ payloadJson: string }>(FALLBACK_CACHE_KEY)
      const row = map[key]
      if (!row) return null
      try {
        return JSON.parse(String((row as any).payloadJson || 'null')) as T
      } catch {
        return null
      }
    }
    await initOfflineSqlite()
    const rows = await sqliteSelect(
      `SELECT payload_json FROM ${CACHE_TABLE} WHERE cache_key='${esc(key)}' LIMIT 1`
    )
    if (!rows.length) return null
    try {
      return JSON.parse(String(rows[0].payload_json || 'null')) as T
    } catch {
      return null
    }
  }
  
  export async function enqueueOfflineMutation(
    endpoint: string,
    method: 'POST' | 'PUT' | 'DELETE',
    payload: JsonValue
  ): Promise<void> {
    const payloadJson = JSON.stringify(payload ?? null)
    const createdAt = nowTs()
    if (!isAppSqliteAvailable()) {
      const list = getStorageArray<MutationRow>(FALLBACK_MUTATION_KEY)
      const maxId = list.reduce((acc, x) => Math.max(acc, Number(x.id || 0)), 0)
      list.push({
        id: maxId + 1,
        endpoint,
        method,
        payloadJson,
        createdAt,
        status: 0,
        lastError: null,
      })
      setStorageArray(FALLBACK_MUTATION_KEY, list)
      return
    }
    await initOfflineSqlite()
    const sql = `INSERT INTO ${MUTATION_TABLE}
      (endpoint, method, payload_json, created_at, status, last_error)
      VALUES ('${esc(endpoint)}', '${esc(method)}', '${esc(payloadJson)}', ${createdAt}, 0, NULL)`
    await sqliteExecute(sql)
  }
  
  export async function getPendingOfflineMutations(): Promise<MutationRow[]> {
    if (!isAppSqliteAvailable()) {
      return getStorageArray<MutationRow>(FALLBACK_MUTATION_KEY).filter((x) => Number(x.status || 0) === 0)
    }
    await initOfflineSqlite()
    const rows = await sqliteSelect(
      `SELECT id, endpoint, method, payload_json, created_at, status, last_error
       FROM ${MUTATION_TABLE}
       WHERE status=0
       ORDER BY id ASC`
    )
    return rows.map((r) => ({
      id: Number(r.id || 0),
      endpoint: String(r.endpoint || ''),
      method: String(r.method || 'POST') as 'POST' | 'PUT' | 'DELETE',
      payloadJson: String(r.payload_json || 'null'),
      createdAt: Number(r.created_at || 0),
      status: Number(r.status || 0),
      lastError: r.last_error == null ? null : String(r.last_error),
    }))
  }
  
  export async function getPendingOfflineMutationsPreview(limit = 20): Promise<MutationRow[]> {
    const all = await getPendingOfflineMutations()
    return all.slice(0, Math.max(1, Math.min(100, Number(limit) || 20)))
  }
  
  export async function markOfflineMutationDone(id: number): Promise<void> {
    if (!isAppSqliteAvailable()) {
      const list = getStorageArray<MutationRow>(FALLBACK_MUTATION_KEY).filter((x) => Number(x.id) !== Number(id))
      setStorageArray(FALLBACK_MUTATION_KEY, list)
      return
    }
    await initOfflineSqlite()
    await sqliteExecute(`DELETE FROM ${MUTATION_TABLE} WHERE id=${Number(id)}`)
  }
  
  export async function markOfflineMutationFailed(id: number, message: string): Promise<void> {
    const m = String(message || '').slice(0, 500)
    if (!isAppSqliteAvailable()) {
      const list = getStorageArray<MutationRow>(FALLBACK_MUTATION_KEY)
      const idx = list.findIndex((x) => Number(x.id) === Number(id))
      if (idx >= 0) {
        list[idx].lastError = m
      }
      setStorageArray(FALLBACK_MUTATION_KEY, list)
      return
    }
    await initOfflineSqlite()
    await sqliteExecute(`UPDATE ${MUTATION_TABLE} SET last_error='${esc(m)}' WHERE id=${Number(id)}`)
  }
  
  export async function removeOfflineMutationById(id: number): Promise<void> {
    await markOfflineMutationDone(id)
  }
  
  export async function getOfflineMutationById(id: number): Promise<MutationRow | null> {
    const targetId = Number(id)
    if (!Number.isFinite(targetId) || targetId <= 0) return null
    if (!isAppSqliteAvailable()) {
      const row = getStorageArray<MutationRow>(FALLBACK_MUTATION_KEY).find((x) => Number(x.id) === targetId)
      return row ?? null
    }
    await initOfflineSqlite()
    const rows = await sqliteSelect(
      `SELECT id, endpoint, method, payload_json, created_at, status, last_error
       FROM ${MUTATION_TABLE}
       WHERE id=${targetId}
       LIMIT 1`
    )
    if (!rows.length) return null
    const r = rows[0]
    return {
      id: Number(r.id || 0),
      endpoint: String(r.endpoint || ''),
      method: String(r.method || 'POST') as 'POST' | 'PUT' | 'DELETE',
      payloadJson: String(r.payload_json || 'null'),
      createdAt: Number(r.created_at || 0),
      status: Number(r.status || 0),
      lastError: r.last_error == null ? null : String(r.last_error),
    }
  }
  
  export async function clearFailedOfflineMutations(): Promise<number> {
    if (!isAppSqliteAvailable()) {
      const list = getStorageArray<MutationRow>(FALLBACK_MUTATION_KEY)
      const failed = list.filter((x) => String(x.lastError || '').trim() !== '')
      const left = list.filter((x) => String(x.lastError || '').trim() === '')
      setStorageArray(FALLBACK_MUTATION_KEY, left)
      return failed.length
    }
    await initOfflineSqlite()
    const rows = await sqliteSelect(
      `SELECT COUNT(1) AS c FROM ${MUTATION_TABLE}
       WHERE status=0 AND last_error IS NOT NULL AND TRIM(last_error) <> ''`
    )
    const count = Number(rows?.[0]?.c || 0)
    await sqliteExecute(
      `DELETE FROM ${MUTATION_TABLE}
       WHERE status=0 AND last_error IS NOT NULL AND TRIM(last_error) <> ''`
    )
    return count
  }
  
  export async function getOfflineSyncSummary(): Promise<{
    cacheCount: number
    pendingMutationCount: number
  }> {
    if (!isAppSqliteAvailable()) {
      const cacheMap = getStorageMap<any>(FALLBACK_CACHE_KEY)
      const pending = getStorageArray<MutationRow>(FALLBACK_MUTATION_KEY).filter((x) => Number(x.status || 0) === 0)
      return {
        cacheCount: Object.keys(cacheMap).length,
        pendingMutationCount: pending.length,
      }
    }
    await initOfflineSqlite()
    const c1 = await sqliteSelect(`SELECT COUNT(1) AS c FROM ${CACHE_TABLE}`)
    const c2 = await sqliteSelect(`SELECT COUNT(1) AS c FROM ${MUTATION_TABLE} WHERE status=0`)
    return {
      cacheCount: Number(c1?.[0]?.c || 0),
      pendingMutationCount: Number(c2?.[0]?.c || 0),
    }
  }
  
  export async function fetchWithOfflineCache<T>(
    module: string,
    name: string,
    fetcher: () => Promise<T>
  ): Promise<T> {
    const online = await isNetworkOnline()
    if (online) {
      const data = await fetcher()
      await setOfflineCache(module, name, data)
      return data
    }
    if (await hasOfflineCache(module, name)) {
      return (await getOfflineCache<T>(module, name)) as T
    }
    throw new Error('No offline cache available')
  }
  
  function getFallbackAuthAccounts(): OfflineAuthAccountRow[] {
    try {
      const raw = String(uni.getStorageSync(FALLBACK_AUTH_KEY) || '').trim()
      if (!raw) return []
      const parsed = JSON.parse(raw) as unknown
      return Array.isArray(parsed) ? (parsed as OfflineAuthAccountRow[]) : []
    } catch {
      return []
    }
  }
  
  function setFallbackAuthAccounts(rows: OfflineAuthAccountRow[]): void {
    uni.setStorageSync(FALLBACK_AUTH_KEY, JSON.stringify(rows))
  }
  
  function authRowFromDb(r: Record<string, unknown>): OfflineAuthAccountRow {
    let locations: OfflineAuthAccountRow['locations'] = []
    try {
      locations = JSON.parse(String(r.locations_json || '[]')) as OfflineAuthAccountRow['locations']
    } catch {
      locations = []
    }
    return {
      email: String(r.email || ''),
      password: String(r.password || ''),
      token: String(r.token || ''),
      refreshToken: String(r.refresh_token || ''),
      locations,
      displayName: String(r.display_name || ''),
      updatedAt: Number(r.updated_at || 0),
    }
  }
  
  /** 在线登录成功后保存,供断网时用同一账号密码登录 */
  export async function saveOfflineAuthAccount(input: {
    email: string
    password: string
    token: string
    refreshToken: string
    locations: OfflineAuthAccountRow['locations']
    displayName: string
  }): Promise<void> {
    const email = input.email.trim().toLowerCase()
    if (!email || !input.password || !input.token) return
    const row: OfflineAuthAccountRow = {
      email,
      password: input.password,
      token: input.token,
      refreshToken: input.refreshToken || '',
      locations: input.locations ?? [],
      displayName: input.displayName || email,
      updatedAt: Date.now(),
    }
    if (!isAppSqliteAvailable()) {
      const list = getFallbackAuthAccounts().filter((x) => x.email !== email)
      list.push(row)
      setFallbackAuthAccounts(list)
      return
    }
    await initOfflineSqlite()
    const locationsJson = esc(JSON.stringify(row.locations))
    await sqliteExecute(
      `INSERT OR REPLACE INTO ${AUTH_TABLE}
        (email, password, token, refresh_token, locations_json, display_name, updated_at)
        VALUES (
          '${esc(email)}',
          '${esc(row.password)}',
          '${esc(row.token)}',
          '${esc(row.refreshToken)}',
          '${locationsJson}',
          '${esc(row.displayName)}',
          ${row.updatedAt}
        )`
    )
  }
  
  /** 断网登录:匹配 SQLite 中已同步过的账号 */
  export async function findOfflineAuthAccount(
    email: string,
    password: string
  ): Promise<OfflineAuthAccountRow | null> {
    const em = email.trim().toLowerCase()
    const pw = password
    if (!em || !pw) return null
    if (!isAppSqliteAvailable()) {
      return getFallbackAuthAccounts().find((x) => x.email === em && x.password === pw) ?? null
    }
    await initOfflineSqlite()
    const rows = await sqliteSelect(
      `SELECT email, password, token, refresh_token, locations_json, display_name, updated_at
       FROM ${AUTH_TABLE}
       WHERE email='${esc(em)}' AND password='${esc(pw)}'
       LIMIT 1`
    )
    if (!rows.length) return null
    return authRowFromDb(rows[0] as Record<string, unknown>)
  }
  
  /** 同步后刷新已存离线账号的 token / 门店,无需再次输入密码 */
  export async function refreshOfflineAuthAccountSession(input: {
    email: string
    token: string
    refreshToken: string
    locations: OfflineAuthAccountRow['locations']
    displayName: string
  }): Promise<void> {
    const email = input.email.trim().toLowerCase()
    if (!email || !input.token) return
    if (!isAppSqliteAvailable()) {
      const list = getFallbackAuthAccounts()
      const idx = list.findIndex((x) => x.email === email)
      if (idx < 0) return
      list[idx] = {
        ...list[idx],
        token: input.token,
        refreshToken: input.refreshToken || '',
        locations: input.locations ?? [],
        displayName: input.displayName || list[idx].displayName,
        updatedAt: Date.now(),
      }
      setFallbackAuthAccounts(list)
      return
    }
    await initOfflineSqlite()
    const locationsJson = esc(JSON.stringify(input.locations ?? []))
    await sqliteExecute(
      `UPDATE ${AUTH_TABLE}
       SET token='${esc(input.token)}',
           refresh_token='${esc(input.refreshToken || '')}',
           locations_json='${locationsJson}',
           display_name='${esc(input.displayName || email)}',
           updated_at=${Date.now()}
       WHERE email='${esc(email)}'`
    )
  }