init.js 2.14 KB
/*
 * @Descripttion:
 * @version: 1.0.0
 * @Author: limin
 * @Date: 2025-01-03 18:53:43
 * @LastEditors: limin
 * @LastEditTime: 2025-12-25 14:49:12
 * @FilePath: \simgle_app\init.js
 */
import utils from './utils/utils'
import { Storage } from './utils/cache/index'
// import Track from './utils/Track'

console.warn('[boot] init.js start')

uni.$storage = new Storage()
uni.$throttle = utils.throttle
uni.$isNetWork = utils.isNetWork
uni.$api = {}
uni.$logger = {
  info: (...args) => console.log('[boot][logger][info]', ...args),
  warn: (...args) => console.warn('[boot][logger][warn]', ...args),
  error: (...args) => console.error('[boot][logger][error]', ...args),
  debug: (...args) => console.log('[boot][logger][debug]', ...args),
  local: {
    info: (...args) => console.log('[boot][local][info]', ...args),
    warn: (...args) => console.warn('[boot][local][warn]', ...args),
    error: (...args) => console.error('[boot][local][error]', ...args),
    debug: (...args) => console.log('[boot][local][debug]', ...args)
  }
}

console.warn('[boot] init.js async load start -> api/logger')
Promise.all([import('./api/index'), import('./utils/logger')])
  .then(([apiMod, loggerMod]) => {
    uni.$api = (apiMod && apiMod.default) || apiMod
    uni.$logger = (loggerMod && loggerMod.default) || loggerMod
    console.warn('[boot] init.js async load done -> api/logger')
  })
  .catch((e) => {
    console.warn('[boot] init.js async load failed -> api/logger', e)
  })
// uni.$dbTempRecord = new DBTempRecord() // 已废弃,合并至 dbRecord
// uni.$track = new Track()

// DBRecord 异步加载,避免 init 阶段同步 import 触发 SQLite/recordManager 链阻塞冷启动
if (!uni.$dbReady) {
  uni.$dbReady = (async () => {
    try {
      const { default: DBRecord } = await import('./utils/recordManager/dbRecord.js')
      if (!uni.$dbRecord) {
        uni.$dbRecord = new DBRecord()
      }
      await uni.$dbRecord.init()
      uni.$logger.local.info('数据库初始化完成')
      return true
    } catch (e) {
      uni.$logger.local.error('数据库初始化失败: ' + (e.message || e))
      return false
    }
  })()
}

console.warn('[boot] init.js done')