index.vue 5.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
<template>
fxy060608's avatar
fxy060608 已提交
2 3
  <uni-page>
    <page-head v-if="navigationBar.type !== 'none'" v-bind="navigationBar" />
fxy060608's avatar
fxy060608 已提交
4 5 6 7 8 9 10 11
    <page-refresh
      v-if="enablePullDownRefresh"
      ref="refresh"
      :color="refreshOptions.color"
      :offset="refreshOptions.offset"
    />
    <page-body
      v-if="enablePullDownRefresh"
fxy060608's avatar
fxy060608 已提交
12 13 14 15
      @touchstart.native="_touchstart"
      @touchmove.native="_touchmove"
      @touchend.native="_touchend"
      @touchcancel.native="_touchend"
fxy060608's avatar
fxy060608 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    >
      <slot name="page" />
    </page-body>
    <page-body v-else>
      <slot name="page" />
    </page-body>
  </uni-page>
</template>
<style>
uni-page {
  display: block;
  width: 100%;
  height: 100%;
}
</style>
<script>
fxy060608's avatar
fxy060608 已提交
32
import { NAVBAR_HEIGHT } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
33

fxy060608's avatar
fxy060608 已提交
34
import { isPlainObject } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
35

fxy060608's avatar
fxy060608 已提交
36
import { mergeTitleNView } from './merge-title-nview'
fxy060608's avatar
fxy060608 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50

import PageHead from './pageHead.vue'
import PageBody from './pageBody.vue'
import PageRefresh from './pageRefresh.vue'

import pullToRefresh from './pull-to-refresh'

import safeAreaInsets from 'safe-area-insets'

export default {
  name: 'Page',
  components: {
    PageHead,
    PageBody,
fxy060608's avatar
fxy060608 已提交
51
    PageRefresh,
fxy060608's avatar
fxy060608 已提交
52 53 54 55 56
  },
  mixins: [pullToRefresh],
  props: {
    isQuit: {
      type: Boolean,
fxy060608's avatar
fxy060608 已提交
57
      default: false,
fxy060608's avatar
fxy060608 已提交
58 59 60
    },
    isEntry: {
      type: Boolean,
fxy060608's avatar
fxy060608 已提交
61
      default: false,
fxy060608's avatar
fxy060608 已提交
62 63 64
    },
    isTabBar: {
      type: Boolean,
fxy060608's avatar
fxy060608 已提交
65
      default: false,
fxy060608's avatar
fxy060608 已提交
66 67 68
    },
    tabBarIndex: {
      type: Number,
fxy060608's avatar
fxy060608 已提交
69
      default: -1,
fxy060608's avatar
fxy060608 已提交
70 71 72
    },
    navigationBarBackgroundColor: {
      type: String,
fxy060608's avatar
fxy060608 已提交
73
      default: '#000',
fxy060608's avatar
fxy060608 已提交
74 75 76
    },
    navigationBarTextStyle: {
      default: 'white',
fxy060608's avatar
fxy060608 已提交
77
      validator(value) {
fxy060608's avatar
fxy060608 已提交
78
        return ['white', 'black'].indexOf(value) !== -1
fxy060608's avatar
fxy060608 已提交
79
      },
fxy060608's avatar
fxy060608 已提交
80 81 82
    },
    navigationBarTitleText: {
      type: String,
fxy060608's avatar
fxy060608 已提交
83
      default: '',
fxy060608's avatar
fxy060608 已提交
84 85 86
    },
    navigationStyle: {
      default: 'default',
fxy060608's avatar
fxy060608 已提交
87
      validator(value) {
fxy060608's avatar
fxy060608 已提交
88
        return ['default', 'custom'].indexOf(value) !== -1
fxy060608's avatar
fxy060608 已提交
89
      },
fxy060608's avatar
fxy060608 已提交
90 91 92
    },
    backgroundColor: {
      type: String,
fxy060608's avatar
fxy060608 已提交
93
      default: '#ffffff',
fxy060608's avatar
fxy060608 已提交
94 95 96
    },
    backgroundTextStyle: {
      default: 'dark',
fxy060608's avatar
fxy060608 已提交
97
      validator(value) {
fxy060608's avatar
fxy060608 已提交
98
        return ['dark', 'light'].indexOf(value) !== -1
fxy060608's avatar
fxy060608 已提交
99
      },
fxy060608's avatar
fxy060608 已提交
100 101 102
    },
    backgroundColorTop: {
      type: String,
fxy060608's avatar
fxy060608 已提交
103
      default: '#fff',
fxy060608's avatar
fxy060608 已提交
104 105 106
    },
    backgroundColorBottom: {
      type: String,
fxy060608's avatar
fxy060608 已提交
107
      default: '#fff',
fxy060608's avatar
fxy060608 已提交
108 109 110
    },
    enablePullDownRefresh: {
      type: Boolean,
fxy060608's avatar
fxy060608 已提交
111
      default: false,
fxy060608's avatar
fxy060608 已提交
112 113 114
    },
    onReachBottomDistance: {
      type: Number,
fxy060608's avatar
fxy060608 已提交
115
      default: 50,
fxy060608's avatar
fxy060608 已提交
116 117 118
    },
    disableScroll: {
      type: Boolean,
fxy060608's avatar
fxy060608 已提交
119
      default: false,
fxy060608's avatar
fxy060608 已提交
120 121 122
    },
    titleNView: {
      type: [Boolean, Object, String],
fxy060608's avatar
fxy060608 已提交
123
      default: '',
fxy060608's avatar
fxy060608 已提交
124 125 126
    },
    pullToRefresh: {
      type: Object,
fxy060608's avatar
fxy060608 已提交
127
      default() {
fxy060608's avatar
fxy060608 已提交
128
        return {}
fxy060608's avatar
fxy060608 已提交
129
      },
fxy060608's avatar
fxy060608 已提交
130 131 132
    },
    titleImage: {
      type: String,
fxy060608's avatar
fxy060608 已提交
133
      default: '',
fxy060608's avatar
fxy060608 已提交
134 135 136
    },
    transparentTitle: {
      type: String,
fxy060608's avatar
fxy060608 已提交
137
      default: '',
fxy060608's avatar
fxy060608 已提交
138 139 140
    },
    titlePenetrate: {
      type: String,
fxy060608's avatar
fxy060608 已提交
141
      default: 'NO',
fxy060608's avatar
fxy060608 已提交
142 143 144
    },
    navigationBarShadow: {
      type: Object,
fxy060608's avatar
fxy060608 已提交
145
      default() {
fxy060608's avatar
fxy060608 已提交
146
        return {}
fxy060608's avatar
fxy060608 已提交
147 148 149 150 151 152
      },
    },
    topWindow: {
      type: Boolean,
      default: true,
    },
fxy060608's avatar
fxy060608 已提交
153
  },
fxy060608's avatar
fxy060608 已提交
154 155 156
  data() {
    // 目前简单处理,存在topWindow时,始终不显示page head
    let navigationBar = {}
fxy060608's avatar
fxy060608 已提交
157 158 159
    const titleNViewTypeList = {
      none: 'default',
      auto: 'transparent',
fxy060608's avatar
fxy060608 已提交
160
      always: 'float',
fxy060608's avatar
fxy060608 已提交
161 162 163
    }
    // 将 navigationStyle 和 transparentTitle 都合并到 titleNView
    let titleNView = this.titleNView
fxy060608's avatar
fxy060608 已提交
164 165
    if (
      // 无头
fxy060608's avatar
fxy060608 已提交
166
      titleNView === false ||
fxy060608's avatar
fxy060608 已提交
167 168 169
      titleNView === 'false' ||
      (this.navigationStyle === 'custom' && !isPlainObject(titleNView)) ||
      (this.transparentTitle === 'always' && !isPlainObject(titleNView))
fxy060608's avatar
fxy060608 已提交
170 171
    ) {
      titleNView = {
fxy060608's avatar
fxy060608 已提交
172
        type: 'none',
fxy060608's avatar
fxy060608 已提交
173 174
      }
    } else {
fxy060608's avatar
fxy060608 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
      titleNView = Object.assign(
        {},
        {
          type: this.navigationStyle === 'custom' ? 'none' : 'default',
        },
        this.transparentTitle in titleNViewTypeList
          ? {
              type: titleNViewTypeList[this.transparentTitle],
            }
          : null,
        typeof titleNView === 'object'
          ? titleNView
          : typeof titleNView === 'boolean'
          ? {
              type: titleNView ? 'default' : 'none',
            }
          : null
      )
fxy060608's avatar
fxy060608 已提交
193 194 195 196
    }

    const yesNoParseList = {
      YES: true,
fxy060608's avatar
fxy060608 已提交
197
      NO: false,
fxy060608's avatar
fxy060608 已提交
198 199
    }

fxy060608's avatar
fxy060608 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213
    navigationBar = mergeTitleNView(
      {
        loading: false,
        backButton: !this.isQuit && !this.$route.meta.isQuit, // redirectTo,reLaunch时可能动态修改 meta.isQuit
        backgroundColor: this.navigationBarBackgroundColor,
        textColor: this.navigationBarTextStyle === 'black' ? '#000' : '#fff',
        titleText: this.navigationBarTitleText,
        titleImage: this.titleImage,
        duration: '0',
        timingFunc: '',
        titlePenetrate: yesNoParseList[this.titlePenetrate],
      },
      titleNView
    )
fxy060608's avatar
fxy060608 已提交
214 215
    navigationBar.shadow = this.navigationBarShadow

fxy060608's avatar
fxy060608 已提交
216 217 218 219 220 221 222 223 224 225 226
    const refreshOptions = Object.assign(
      {
        support: true,
        color: '#2BD009',
        style: 'circle',
        height: 70,
        range: 150,
        offset: 0,
      },
      this.pullToRefresh
    )
fxy060608's avatar
fxy060608 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239

    let offset = uni.upx2px(refreshOptions.offset)

    if (titleNView.type !== 'none' && titleNView.type !== 'transparent') {
      offset += NAVBAR_HEIGHT + safeAreaInsets.top
    }

    refreshOptions.offset = offset
    refreshOptions.height = uni.upx2px(refreshOptions.height)
    refreshOptions.range = uni.upx2px(refreshOptions.range)

    return {
      navigationBar,
fxy060608's avatar
fxy060608 已提交
240
      refreshOptions,
fxy060608's avatar
fxy060608 已提交
241 242
    }
  },
fxy060608's avatar
fxy060608 已提交
243
  created() {
fxy060608's avatar
fxy060608 已提交
244 245 246
    const navigationBar = this.navigationBar
    document.title = navigationBar.titleText
    UniServiceJSBridge.emit('onNavigationBarChange', navigationBar)
fxy060608's avatar
fxy060608 已提交
247
  },
fxy060608's avatar
fxy060608 已提交
248 249
}
</script>