global.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import store from '@/store/index'
  2. export default {
  3. windowInfo: null,
  4. deviceOrientation: 'portrait', // 设备方向(竖屏 portrait | 横屏 landscape)
  5. screenWidth: 0, // 屏幕宽度
  6. screenHeight: 0,// 屏幕高度
  7. windowHeight: 0,// 可使用窗口宽度
  8. windowWidth: 0, // 可使用窗口高度
  9. fullscreenFlag: false,
  10. // 全屏展示
  11. fullscreen() {
  12. if (document.fullscreenElement || document.webkitFullScreenElement || document.mozFullScreenElement ||
  13. document.msFullScreenElement) {
  14. // alert('当前是全屏')
  15. if (document.webkitCancelFullScreen) {
  16. document.webkitCancelFullScreen();
  17. } else if (document.mozCancelFullScreen) {
  18. document.mozCancelFullScreen();
  19. } else if (document.exitFullscreen) {
  20. document.exitFullscreen();
  21. } else {
  22. uni.showToast({
  23. title: '浏览器不支持全屏API或已被禁用',
  24. icon: 'none'
  25. })
  26. }
  27. this.fullscreenFlag = false
  28. store.commit('setFullScreen', this.fullscreenFlag)
  29. } else {
  30. let elem = document.body;
  31. if (elem.webkitRequestFullScreen) {
  32. elem.webkitRequestFullScreen();
  33. this.fullscreenFlag = true
  34. } else if (elem.mozRequestFullScreen) {
  35. elem.mozRequestFullScreen();
  36. this.fullscreenFlag = true
  37. } else if (elem.requestFullScreen) {
  38. elem.requestFullscreen();
  39. this.fullscreenFlag = true
  40. } else {
  41. uni.showToast({
  42. title: '浏览器不支持全屏API或已被禁用',
  43. icon: 'none'
  44. })
  45. this.fullscreenFlag = false
  46. }
  47. store.commit('setFullScreen', this.fullscreenFlag)
  48. }
  49. // 去除foucs红框(微信浏览器全屏时偶尔会出现foucs红框)
  50. document.activeElement.blur();
  51. },
  52. // 获取系统信息
  53. getWindowInfo(windowSize = '') {
  54. if (windowSize == '') {
  55. // this.windowInfo = uni.getSystemInfoSync()
  56. this.windowInfo = uni.getWindowInfo()
  57. this.screenWidth = this.windowInfo.screenWidth
  58. this.screenHeight = this.windowInfo.screenHeight
  59. this.windowHeight = this.windowInfo.windowHeight
  60. this.windowWidth = this.windowInfo.windowWidth
  61. // console.log('[getWindowInfo] this.windowHeight', this.windowHeight, this.windowInfo)
  62. } else {
  63. this.windowInfo = windowSize
  64. this.windowHeight = windowSize.windowHeight
  65. this.windowWidth = windowSize.windowWidth
  66. // console.log('[getWindowInfo] this.windowHeight 2', this.windowHeight)
  67. }
  68. if (this.windowHeight > this.windowWidth) {
  69. console.log('[getWindowInfo] 竖屏 windowWidth:' + this.windowWidth + ' windowHeight:' + this.windowHeight)
  70. // uni.showToast({
  71. // title: '竖屏 windowWidth=' + this.windowWidth + ' windowHeight:' + this.windowHeight,
  72. // icon: 'none',
  73. // duration: 2000
  74. // })
  75. this.deviceOrientation = 'portrait'
  76. } else { // 横屏
  77. console.log('[getWindowInfo] 横屏 windowWidth:' + this.windowWidth + ' windowHeight:' + this.windowHeight)
  78. // uni.showToast({
  79. // title: '横屏 windowWidth=' + this.windowWidth + ' windowHeight:' + this.windowHeight,
  80. // icon: 'none',
  81. // duration: 2000
  82. // })
  83. this.deviceOrientation = 'landscape'
  84. }
  85. return this.windowInfo
  86. },
  87. }