index.vue 4.5 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}">
3 4 5 6
    <layout
      ref="layout"
      :router-key="key"
      :keep-alive-include="keepAliveInclude"
7
      @maxWidth="onMaxWidth"
8
      @layout="onLayout"
fxy060608's avatar
fxy060608 已提交
9
    />
10 11 12 13 14
    <tab-bar
      v-if="hasTabBar"
      v-show="showTabBar"
      ref="tabBar"
      v-bind="tabBarOptions"
fxy060608's avatar
fxy060608 已提交
15
    />
16 17 18
    <toast
      v-if="$options.components.Toast"
      v-bind="showToast"
fxy060608's avatar
fxy060608 已提交
19
    />
20 21 22 23
    <action-sheet
      v-if="$options.components.ActionSheet"
      v-bind="showActionSheet"
      @close="_onActionSheetClose"
fxy060608's avatar
fxy060608 已提交
24
    />
25 26 27 28 29 30 31 32 33
    <modal
      v-if="$options.components.Modal"
      v-bind="showModal"
      @close="_onModalClose"
    />
    <preview-image
      v-if="$options.components.PreviewImage"
      v-bind="previewImage"
      @close="_onPreviewClose"
fxy060608's avatar
fxy060608 已提交
34
    />
35
    <template v-if="sysComponents&&sysComponents.length">
36 37 38 39
      <component
        :is="item"
        v-for="(item, index) in sysComponents"
        :key="index"
40 41
      />
    </template>
fxy060608's avatar
fxy060608 已提交
42 43 44
  </uni-app>
</template>
<script>
fxy060608's avatar
fxy060608 已提交
45
import {
fxy060608's avatar
fxy060608 已提交
46
  hasOwn,
fxy060608's avatar
fxy060608 已提交
47 48 49
  isPlainObject
} from 'uni-shared'

fxy060608's avatar
fxy060608 已提交
50 51 52 53 54 55
import {
  TABBAR_HEIGHT
} from 'uni-helpers/constants'

import components from './components'

fxy060608's avatar
fxy060608 已提交
56
import mixins from 'uni-h5-app-mixins'
fxy060608's avatar
fxy060608 已提交
57

fxy060608's avatar
fxy060608 已提交
58 59 60 61
import {
  tabBar
} from './observable'

fxy060608's avatar
fxy060608 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
export default {
  name: 'App',
  components,
  mixins,
  props: {
    keepAliveInclude: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  data () {
    return {
      transitionName: 'fade',
      hideTabBar: false,
fxy060608's avatar
fxy060608 已提交
78
      sysComponents: this.$sysComponents,
fxy060608's avatar
fxy060608 已提交
79 80 81
      showLayout: false,
      showMaxWidth: false,
      tabBarMediaQuery: false
fxy060608's avatar
fxy060608 已提交
82 83 84 85 86 87
    }
  },
  computed: {
    key () {
      return this.$route.meta.name + '-' + this.$route.params.__id__ + '-' + (__uniConfig.reLaunch || 1)
    },
fxy060608's avatar
fxy060608 已提交
88 89 90
    tabBarOptions () {
      return tabBar
    },
fxy060608's avatar
fxy060608 已提交
91
    hasTabBar () {
fxy060608's avatar
fxy060608 已提交
92
      return tabBar.list && tabBar.list.length
fxy060608's avatar
fxy060608 已提交
93 94
    },
    showTabBar () {
fxy060608's avatar
fxy060608 已提交
95 96 97
      return !this.hideTabBar &&
          (
            this.$route.meta.isTabBar ||
fxy060608's avatar
fxy060608 已提交
98
            this.tabBarMediaQuery
fxy060608's avatar
fxy060608 已提交
99
          )
fxy060608's avatar
fxy060608 已提交
100 101 102 103 104 105 106
    }
  },
  watch: {
    $route (newRoute, oldRoute) {
      UniServiceJSBridge.emit('onHidePopup')
    },
    hideTabBar (newVal, oldVal) {
fxy060608's avatar
fxy060608 已提交
107 108
      // TODO 不支持 css 变量时
      if (uni.canIUse('css.var')) {
Q
qiang 已提交
109 110
        const windowBottomValue = !newVal ? (TABBAR_HEIGHT) : 0
        const envMethod = uni.canIUse('css.env') ? 'env' : (uni.canIUse('css.constant') ? 'constant' : '')
111 112
        const windowBottom = windowBottomValue && envMethod
          ? `calc(${windowBottomValue}px + ${envMethod}(safe-area-inset-bottom))` : `${windowBottomValue}px`
fxy060608's avatar
fxy060608 已提交
113 114
        document.documentElement.style.setProperty('--window-bottom', windowBottom)
        console.debug(`uni.${windowBottom ? 'showTabBar' : 'hideTabBar'}:--window-bottom=${windowBottom}`)
fxy060608's avatar
fxy060608 已提交
115
      }
116 117
      // 触发 resize 事件
      window.dispatchEvent(new CustomEvent('resize'))
fxy060608's avatar
fxy060608 已提交
118 119 120
    }
  },
  created () {
fxy060608's avatar
fxy060608 已提交
121 122
    if (uni.canIUse('css.var')) {
      document.documentElement.style.setProperty('--status-bar-height', '0px')
fxy060608's avatar
fxy060608 已提交
123
    }
fxy060608's avatar
fxy060608 已提交
124
    this.initMediaQuery()
fxy060608's avatar
fxy060608 已提交
125 126
  },
  mounted () {
fxy060608's avatar
fxy060608 已提交
127 128 129 130 131
    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 已提交
132 133
    document.addEventListener('visibilitychange', function () {
      if (document.visibilityState === 'visible') {
134
        UniServiceJSBridge.emit('onAppEnterForeground', {})
fxy060608's avatar
fxy060608 已提交
135 136 137 138
      } else {
        UniServiceJSBridge.emit('onAppEnterBackground')
      }
    })
fxy060608's avatar
fxy060608 已提交
139 140
  },
  methods: {
fxy060608's avatar
fxy060608 已提交
141 142 143
    onLayout (showLayout) {
      this.showLayout = showLayout
    },
fxy060608's avatar
fxy060608 已提交
144 145
    onMaxWidth (showMaxWidth) {
      this.showMaxWidth = showMaxWidth
fxy060608's avatar
fxy060608 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158
    },
    initMediaQuery () {
      if (
        window.matchMedia &&
          tabBar.matchMedia &&
          hasOwn(tabBar.matchMedia, 'minWidth')
      ) {
        const mediaQueryList = window.matchMedia('(min-width: ' + tabBar.matchMedia.minWidth + 'px)')
        mediaQueryList.addListener((e) => {
          this.tabBarMediaQuery = e.matches
        })
        this.tabBarMediaQuery = mediaQueryList.matches
      }
fxy060608's avatar
fxy060608 已提交
159
    }
fxy060608's avatar
fxy060608 已提交
160 161 162 163
  }
}
</script>

Q
qiang 已提交
164
<style>
fxy060608's avatar
fxy060608 已提交
165
  @import "~uni-core/view/index.css";
166 167 168 169 170 171 172

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