index.vue 3.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
<template>
  <uni-app :class="{'uni-app--showtabbar':showTabBar}">
fxy060608's avatar
fxy060608 已提交
3 4 5 6
    <layout
      :router-key="key"
      :keep-alive-include="keepAliveInclude"
    />
7 8 9
    <tab-bar
      v-if="hasTabBar"
      v-show="showTabBar"
fxy060608's avatar
fxy060608 已提交
10 11
      v-bind="tabBar"
    />
12 13
    <toast
      v-if="$options.components.Toast"
fxy060608's avatar
fxy060608 已提交
14 15
      v-bind="showToast"
    />
16 17 18
    <action-sheet
      v-if="$options.components.ActionSheet"
      v-bind="showActionSheet"
fxy060608's avatar
fxy060608 已提交
19 20
      @close="_onActionSheetClose"
    />
21 22 23
    <modal
      v-if="$options.components.Modal"
      v-bind="showModal"
fxy060608's avatar
fxy060608 已提交
24 25
      @close="_onModalClose"
    />
26 27 28 29 30 31 32
    <template v-if="sysComponents&&sysComponents.length">
      <component
        :is="item"
        v-for="(item, index) in sysComponents"
        :key="index"
      />
    </template>
fxy060608's avatar
fxy060608 已提交
33 34 35
  </uni-app>
</template>
<script>
fxy060608's avatar
fxy060608 已提交
36 37 38 39
import {
  isPlainObject
} from 'uni-shared'

fxy060608's avatar
fxy060608 已提交
40 41 42 43 44 45
import {
  TABBAR_HEIGHT
} from 'uni-helpers/constants'

import components from './components'

fxy060608's avatar
fxy060608 已提交
46
import mixins from 'uni-h5-app-mixins'
fxy060608's avatar
fxy060608 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

export default {
  name: 'App',
  components,
  mixins,
  props: {
    keepAliveInclude: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  data () {
    return {
      transitionName: 'fade',
      hideTabBar: false,
64 65
      tabBar: __uniConfig.tabBar || {},
      sysComponents: this.$sysComponents
fxy060608's avatar
fxy060608 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    }
  },
  computed: {
    key () {
      return this.$route.meta.name + '-' + this.$route.params.__id__ + '-' + (__uniConfig.reLaunch || 1)
    },
    hasTabBar () {
      return __uniConfig.tabBar && __uniConfig.tabBar.list && __uniConfig.tabBar.list.length
    },
    showTabBar () {
      return this.$route.meta.isTabBar && !this.hideTabBar
    }
  },
  watch: {
    $route (newRoute, oldRoute) {
      UniServiceJSBridge.emit('onHidePopup')
    },
    hideTabBar (newVal, oldVal) {
fxy060608's avatar
fxy060608 已提交
84 85
      // TODO 不支持 css 变量时
      if (uni.canIUse('css.var')) {
Q
qiang 已提交
86 87
        const windowBottomValue = !newVal ? (TABBAR_HEIGHT) : 0
        const envMethod = uni.canIUse('css.env') ? 'env' : (uni.canIUse('css.constant') ? 'constant' : '')
88 89
        const windowBottom = windowBottomValue && envMethod
          ? `calc(${windowBottomValue}px + ${envMethod}(safe-area-inset-bottom))` : `${windowBottomValue}px`
fxy060608's avatar
fxy060608 已提交
90 91
        document.documentElement.style.setProperty('--window-bottom', windowBottom)
        console.debug(`uni.${windowBottom ? 'showTabBar' : 'hideTabBar'}:--window-bottom=${windowBottom}`)
fxy060608's avatar
fxy060608 已提交
92
      }
93 94
      // 触发 resize 事件
      window.dispatchEvent(new CustomEvent('resize'))
fxy060608's avatar
fxy060608 已提交
95 96 97
    }
  },
  created () {
fxy060608's avatar
fxy060608 已提交
98 99
    if (uni.canIUse('css.var')) {
      document.documentElement.style.setProperty('--status-bar-height', '0px')
fxy060608's avatar
fxy060608 已提交
100 101 102
    }
  },
  mounted () {
fxy060608's avatar
fxy060608 已提交
103 104 105 106 107
    window.addEventListener('message', function (evt) {
      if (isPlainObject(evt.data) && evt.data.type === 'WEB_INVOKE_APPSERVICE') {
        UniServiceJSBridge.emit('onWebInvokeAppService', evt.data.data, evt.data.pageId)
      }
    })
fxy060608's avatar
fxy060608 已提交
108 109 110 111 112 113 114 115 116 117 118
    document.addEventListener('visibilitychange', function () {
      if (document.visibilityState === 'visible') {
        UniServiceJSBridge.emit('onAppEnterForeground')
      } else {
        UniServiceJSBridge.emit('onAppEnterBackground')
      }
    })
  }
}
</script>

Q
qiang 已提交
119
<style>
fxy060608's avatar
fxy060608 已提交
120
  @import "~uni-core/view/index.css";
121 122 123 124 125 126 127 128

  uni-app {
    display: block;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
  }
</style>