platform.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * @Author: Rindy
  3. * @Date: 2020-02-24 11:43:44
  4. * @LastEditors: Rindy
  5. * @LastEditTime: 2020-06-19 15:34:05
  6. * @Description: 判断加载平台
  7. */
  8. // import bus from './eventbus'
  9. import browser from './browser'
  10. // import { backgroundMusicPlayer } from './sounds'
  11. const ua = navigator.userAgent || navigator.vendor || window.opera
  12. function isIOS() {
  13. if (/iPad|iPhone|iPod/.test(ua) && !window.MSStream) {
  14. return true
  15. } else {
  16. return false
  17. }
  18. }
  19. function isAndroid() {
  20. return /android/i.test(ua)
  21. }
  22. var androidNative = browser.urlHasValue('android')
  23. function sendMessageToUnity(message) {
  24. try {
  25. if (isIOS()) {
  26. window.webkit.messageHandlers.inappbrowserbridge.postMessage(message)
  27. } else if (isAndroid()) {
  28. if (androidNative) {
  29. switch (message) {
  30. case 'Back':
  31. AndroidNative.Back()
  32. break
  33. case 'Share':
  34. AndroidNative.Share()
  35. break
  36. }
  37. } else {
  38. UnityInAppBrowser.sendMessageFromJS(message)
  39. }
  40. }
  41. } catch (e) {
  42. console.warn(e)
  43. }
  44. }
  45. // 注册App发送消息方法
  46. window.appSendMsgBack = function (name, data) {
  47. console.log('appSendMsgBack: ' + name, data)
  48. // bus.emit('app-' + name, data)
  49. }
  50. /** 背景音乐控制 */
  51. // bus.on('app-onResume', () => {
  52. // console.log('appMusicPlay')
  53. // backgroundMusicPlayer.resume()
  54. // })
  55. // bus.on('app-onStop', () => {
  56. // console.log('appMusicPause')
  57. // if (backgroundMusicPlayer.isPlaying) {
  58. // backgroundMusicPlayer.pause(true)
  59. // }
  60. // })
  61. /** 背景音乐控制 end */
  62. /**
  63. * APP
  64. */
  65. export const IsApp = browser.urlHasValue('app') && !browser.urlHasValue('appname')
  66. /**
  67. * 微信
  68. */
  69. export const IsWechat = /MicroMessenger/gi.test(window.navigator.userAgent)
  70. /**
  71. * 刘海高度
  72. */
  73. export const NotchHeight = browser.urlHasValue('notch', true)
  74. /**
  75. * 发送消息到APP
  76. * @param {*} message
  77. */
  78. export function MessageToApp(message) {
  79. console.log('SendToApp')
  80. console.log(message)
  81. sendMessageToUnity(message)
  82. }