index.vue 4.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
<template>
D
DCloud_LXH 已提交
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
    />
D
DCloud_LXH 已提交
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
import {
D
DCloud_LXH 已提交
51 52
  TABBAR_HEIGHT,
  ON_THEME_CHANGE
fxy060608's avatar
fxy060608 已提交
53 54 55 56
} from 'uni-helpers/constants'

import components from './components'

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

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

D
DCloud_LXH 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
function onThemeChange () {
  let mediaQueryList = null

  try {
    mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)')
  } catch (error) { }

  if (mediaQueryList) {
    mediaQueryList.addEventListener('change', (e) => {
      UniServiceJSBridge.emit('api.' + ON_THEME_CHANGE, {
        theme: e.matches ? 'dark' : 'light'
      })
    })
  }
}

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

Q
qiang 已提交
182
<style>
D
DCloud_LXH 已提交
183
@import "~uni-core/view/index.css";
184

D
DCloud_LXH 已提交
185 186 187 188 189 190
uni-app {
  display: block;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
}
191
</style>