usePlatformInfo.ts 986 Bytes
import { isFromEdge } from './useParseParams'
import type { PlatformInfo } from '#/store'
import { APP_LOCAL_CACHE_KEY, EDGE_PLATFORM_INFO_CACHE_KEY, PLATFORM_INFO_CACHE_KEY } from '@/enums/cacheEnum'
import { createStorage } from '@/utils/cache'
import { getPlatformInfo as getPlatform } from '@/api/sys'

const storage = createStorage(localStorage)
export const getPlatformInfo = (): PlatformInfo => {
  if (isFromEdge()) {
    const common = storage.get(APP_LOCAL_CACHE_KEY)
    return common?.[EDGE_PLATFORM_INFO_CACHE_KEY]?.value || {}
  }
  else {
    return storage.get(PLATFORM_INFO_CACHE_KEY) as PlatformInfo
  }
}

export const setPlatformInfo = (info: Recordable) => storage.set(PLATFORM_INFO_CACHE_KEY, info)

export const usePlatform = async () => {
  const platformInfo = (await getPlatform()) || {}
  setPlatformInfo(platformInfo)
  return platformInfo
}

window.getPlatformInfo = getPlatformInfo
window.setPlatformInfo = setPlatformInfo
window.usePlatform = usePlatform