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

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

import components from './components'

fxy060608's avatar
fxy060608 已提交
48
import mixins from 'uni-h5-app-mixins'
fxy060608's avatar
fxy060608 已提交
49

fxy060608's avatar
fxy060608 已提交
50 51 52 53
import {
  tabBar
} from './observable'

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

Q
qiang 已提交
133
<style>
fxy060608's avatar
fxy060608 已提交
134
  @import "~uni-core/view/index.css";
135 136 137 138 139 140 141 142

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