index.vue 2.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
<template>
  <uni-app :class="{'uni-app--showtabbar':showTabBar}">
    <!-- <transition :name="transitionName"> -->
    <!-- TODO -->
    <keep-alive :include="keepAliveInclude">
      <router-view :key="key" />
    </keep-alive>
    <!-- </transition> -->
fxy060608's avatar
fxy060608 已提交
9 10 11
    <tab-bar
      v-if="hasTabBar"
      v-show="showTabBar"
fxy060608's avatar
fxy060608 已提交
12
      v-bind="tabBar" />
fxy060608's avatar
fxy060608 已提交
13 14
    <toast
      v-bind="showToast"
fxy060608's avatar
fxy060608 已提交
15
      @close="_onModalClose" />
fxy060608's avatar
fxy060608 已提交
16 17
    <action-sheet
      v-bind="showActionSheet"
fxy060608's avatar
fxy060608 已提交
18
      @close="_onActionSheetClose" />
fxy060608's avatar
fxy060608 已提交
19 20
    <modal
      v-bind="showModal"
fxy060608's avatar
fxy060608 已提交
21
      @close="_onModalClose" />
fxy060608's avatar
fxy060608 已提交
22 23
    <picker
      v-bind="showPicker"
fxy060608's avatar
fxy060608 已提交
24 25 26 27
      @close="_onPickerClose" />
  </uni-app>
</template>
<script>
fxy060608's avatar
fxy060608 已提交
28 29 30 31
import {
  isPlainObject
} from 'uni-shared'

fxy060608's avatar
fxy060608 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
import {
  TABBAR_HEIGHT
} from 'uni-helpers/constants'

import components from './components'

import mixins from './popup/mixins'

export default {
  name: 'App',
  components,
  mixins,
  props: {
    keepAliveInclude: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  data () {
    return {
      transitionName: 'fade',
      hideTabBar: false,
      tabBar: __uniConfig.tabBar || {}
    }
  },
  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 已提交
75 76 77 78 79
      // TODO 不支持 css 变量时
      if (uni.canIUse('css.var')) {
        const windowBottom = !newVal ? (TABBAR_HEIGHT + 'px') : '0px'
        document.documentElement.style.setProperty('--window-bottom', windowBottom)
        console.debug(`uni.${windowBottom ? 'showTabBar' : 'hideTabBar'}:--window-bottom=${windowBottom}`)
fxy060608's avatar
fxy060608 已提交
80 81 82 83
      }
    }
  },
  created () {
fxy060608's avatar
fxy060608 已提交
84 85
    if (uni.canIUse('css.var')) {
      document.documentElement.style.setProperty('--status-bar-height', '0px')
fxy060608's avatar
fxy060608 已提交
86 87 88
    }
  },
  mounted () {
fxy060608's avatar
fxy060608 已提交
89 90 91 92 93
    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 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    document.addEventListener('visibilitychange', function () {
      if (document.visibilityState === 'visible') {
        UniServiceJSBridge.emit('onAppEnterForeground')
      } else {
        UniServiceJSBridge.emit('onAppEnterBackground')
      }
    })
  }
}
</script>

<style>
	@import "~uni-core/view/index.css";

	uni-app {
		display: block;
		box-sizing: border-box;
		width: 100%;
		height: 100%;
	}
fxy060608's avatar
fxy060608 已提交
114
</style>