You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

58 rivejä
1.6 KiB

  1. const MOBILE_UA_RE = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini|mobile/i
  2. const IOS_UA_RE = /iphone|ipad|ipod|ios/i
  3. const WECHAT_UA_RE = /micromessenger/i
  4. export function resolveClientDevice(input = {}) {
  5. const host = String(input.host || '').split(':')[0].toLowerCase()
  6. const userAgent = String(input.userAgent || '')
  7. const isMobileHost = host === 'm.aionline.cc' || host.startsWith('m.')
  8. const isMobile = isMobileHost || MOBILE_UA_RE.test(userAgent)
  9. return {
  10. client: isMobile ? 2 : 1,
  11. isMobile,
  12. isIos: IOS_UA_RE.test(userAgent),
  13. isWechat: WECHAT_UA_RE.test(userAgent),
  14. host,
  15. userAgent,
  16. }
  17. }
  18. export function useClientDevice() {
  19. function getHost() {
  20. if (import.meta.client) return window.location.hostname || ''
  21. const headers = useRequestHeaders(['host'])
  22. return headers.host || ''
  23. }
  24. function getUserAgent() {
  25. if (import.meta.client) return navigator.userAgent || ''
  26. const headers = useRequestHeaders(['user-agent'])
  27. return headers['user-agent'] || ''
  28. }
  29. function getMeta() {
  30. return resolveClientDevice({
  31. host: getHost(),
  32. userAgent: getUserAgent(),
  33. })
  34. }
  35. const device = computed(() => getMeta())
  36. const clientId = computed(() => device.value.client)
  37. const isMobileClient = computed(() => device.value.isMobile)
  38. const isIosClient = computed(() => device.value.isIos)
  39. const isWechatClient = computed(() => device.value.isWechat)
  40. return {
  41. device,
  42. clientId,
  43. isMobileClient,
  44. isIosClient,
  45. isWechatClient,
  46. getClientId: () => getMeta().client,
  47. getClientIos: () => (getMeta().isIos ? 1 : 0),
  48. getClientMeta: getMeta,
  49. }
  50. }