platform.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * 获取当前运行的客户端(APP H5 小程序)
  3. * https://uniapp.dcloud.io/platform
  4. */
  5. const getPlatform = () => {
  6. // #ifdef APP-PLUS
  7. const platform = 'APP'
  8. // #endif
  9. // #ifdef APP-PLUS-NVUE
  10. const platform = 'APP'
  11. // #endif
  12. // #ifdef H5
  13. const platform = weixinOfficial() ? 'H5-WEIXIN' : 'H5'
  14. // #endif
  15. // #ifdef MP-WEIXIN
  16. const platform = 'MP-WEIXIN'
  17. // #endif
  18. // #ifdef MP-ALIPAY
  19. const platform = 'MP-ALIPAY'
  20. // #endif
  21. // #ifdef MP-BAIDU
  22. const platform = 'MP-BAIDU'
  23. // #endif
  24. // #ifdef MP-TOUTIAO
  25. const platform = 'MP-TOUTIAO'
  26. // #endif
  27. // #ifdef MP-LARK
  28. const platform = 'MP-LARK'
  29. // #endif
  30. // #ifdef MP-QQ
  31. const platform = 'MP-QQ'
  32. // #endif
  33. // #ifdef MP-KUAISHOU
  34. const platform = 'MP-KUAISHOU'
  35. // #endif
  36. // #ifdef MP-360
  37. const platform = 'MP-360'
  38. // #endif
  39. // #ifdef QUICKAPP-WEBVIEW
  40. const platform = 'QUICKAPP-WEBVIEW'
  41. // #endif
  42. return platform
  43. }
  44. // 是否为微信公众号端
  45. const weixinOfficial = () => {
  46. // #ifdef H5
  47. const ua = window.navigator.userAgent.toLowerCase()
  48. return String(ua.match(/MicroMessenger/i)) === 'micromessenger'
  49. // #endif
  50. return false
  51. }
  52. const platfrom = getPlatform()
  53. export const isH5 = platfrom === 'H5'
  54. export const isApp = platfrom === 'APP'
  55. export const isMpWeixin = platfrom === 'MP-WEIXIN'
  56. // 是否为微信公众号端
  57. // 相当于H5端运行在微信内置浏览器, 但是需要使用微信的jssdk所以要单独区分
  58. export const isWeixinOfficial = platfrom === 'H5-WEIXIN'
  59. export default platfrom