|
- const MOBILE_UA_RE = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini|mobile/i
- const IOS_UA_RE = /iphone|ipad|ipod|ios/i
- const WECHAT_UA_RE = /micromessenger/i
-
- export function resolveClientDevice(input = {}) {
- const host = String(input.host || '').split(':')[0].toLowerCase()
- const userAgent = String(input.userAgent || '')
- const isMobileHost = host === 'm.aionline.cc' || host.startsWith('m.')
- const isMobile = isMobileHost || MOBILE_UA_RE.test(userAgent)
-
- return {
- client: isMobile ? 2 : 1,
- isMobile,
- isIos: IOS_UA_RE.test(userAgent),
- isWechat: WECHAT_UA_RE.test(userAgent),
- host,
- userAgent,
- }
- }
-
- export function useClientDevice() {
- function getHost() {
- if (import.meta.client) return window.location.hostname || ''
- const headers = useRequestHeaders(['host'])
- return headers.host || ''
- }
-
- function getUserAgent() {
- if (import.meta.client) return navigator.userAgent || ''
- const headers = useRequestHeaders(['user-agent'])
- return headers['user-agent'] || ''
- }
-
- function getMeta() {
- return resolveClientDevice({
- host: getHost(),
- userAgent: getUserAgent(),
- })
- }
-
- const device = computed(() => getMeta())
- const clientId = computed(() => device.value.client)
- const isMobileClient = computed(() => device.value.isMobile)
- const isIosClient = computed(() => device.value.isIos)
- const isWechatClient = computed(() => device.value.isWechat)
-
- return {
- device,
- clientId,
- isMobileClient,
- isIosClient,
- isWechatClient,
- getClientId: () => getMeta().client,
- getClientIos: () => (getMeta().isIos ? 1 : 0),
- getClientMeta: getMeta,
- }
- }
|