platform.js 1.5 KB

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