dengjunjie
2025-03-12 f43b7df8400f4fcffc9f19dca0888d61e2b33d5f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<template>
  <view
    class="tn-custom-nav-bar-class tn-custom-nav-bar"
    :style="[navBarStyle]"
  >
      <view
      class="tn-custom-nav-bar__bar"
      :class="[barClass]"
      :style="[barStyle]"
    >
      <view v-if="isBack">
        <view v-if="customBack">
          <view 
            :style="{
              width: customBackStyleInfo.width + 'px',
              height: customBackStyleInfo.height + 'px',
              marginLeft: customBackStyleInfo.left + 'px'
            }"
          >
            <slot name="back"></slot>
          </view>
        </view>
        <view v-else class="tn-custom-nav-bar__bar__action" @tap="handlerBack">
            <text class="tn-custom-nav-bar__bar__action--nav-back" :class="[`tn-icon-${backIcon}`]"></text>
            <text class="tn-custom-nav-bar__bar__action--nav-back-text" v-if="backTitle">{{ backTitle }}</text>
        </view>
      </view>
          <view class="tn-custom-nav-bar__bar__content" :style="[contentStyle]">
              <slot></slot>
          </view>
          <view>
        <slot name="right"></slot>
      </view>
      </view>
  </view>
</template>
 
<script>
  import componentsColorMixin from '../../libs/mixin/components_color.js'
  export default {
    name: 'tn-nav-bar',
    mixins: [componentsColorMixin],
    props: {
      // 层级
      zIndex: {
        type: Number,
        default: 0
      },
      // 导航栏的高度
      height: {
        type: Number,
        default: 0
      },
      // 高度单位
      unit: {
        type: String,
        default: 'px'
      },
      // 是否显示返回按钮
      isBack: {
        type: Boolean,
        default: true
      },
      // 返回按钮的图标
      backIcon: {
        type: String,
        default: 'left'
      },
      // 返回按钮旁显示的文字
      backTitle: {
        type: String,
        default: '返回'
      },
      // 透明状态栏
      alpha: {
        type: Boolean,
        default: false
      },
      // 是否固定在顶部
      fixed: {
        type: Boolean,
        default: true
      },
      // 是否显示底部阴影
      bottomShadow: {
        type: Boolean,
        default: true
      },
      // 是否自定义返回按钮
      customBack: {
        type: Boolean,
        default: false
      },
      // 返回前回调
      beforeBack: {
        type: Function,
        default: null
      }
    },
    computed: {
      navBarStyle() {
        let style = {}
        style.height = this.height === 0 ? this.customBarHeight + this.unit : this.height + this.unit
        if (this.fixed) {
          style.position = 'fixed'
        }
        style.zIndex = this.elZIndex
        
        return style
      },
      barClass() {
        let clazz = ''
        if (this.backgroundColorClass) {
          clazz += ` ${this.backgroundColorClass}`
        }
        if (this.fontColorClass) {
          clazz += `${this.fontColorClass}`
        }
        if (this.fixed) {
          clazz += ' tn-custom-nav-bar__bar--fixed'
        }
        if (this.alpha) {
          clazz += ' tn-custom-nav-bar__bar--alpha'
        }
        if (this.bottomShadow) {
          clazz += ' tn-custom-nav-bar__bar--bottom-shadow'
        }
        
        return clazz
      },
      barStyle() {
        let style = {}
        style.height = this.height === 0 ? this.customBarHeight + this.unit : this.height + this.unit
        
        if (this.fixed) {
          style.paddingTop = this.statusBarHeight + 'px'
        }
        
        if(!this.backgroundColorClass) {
          style.backgroundColor = this.backgroundColor !== '' ? this.backgroundColor : '#FFFFFF'
        }
        if (!this.fontColorClass && this.fontColor) {
          style.color= this.fontColor
        }
        
        style.zIndex = this.elZIndex
        
        return style
      },
      contentStyle() {
        let style = {}
        style.top = this.fixed ? this.statusBarHeight + 'px' : '0px'
        style.height = this.height === 0 ? (this.customBarHeight - this.statusBarHeight) + this.unit : this.height + this.unit
        style.lineHeight = style.height
        
        if (this.isBack) {
          if (this.customBack) {
            const width = (this.customBackStyleInfo.width + this.customBackStyleInfo.left) * 2
            style.width = `calc(100% - ${width}px)`
          } else {
            style.width = 'calc(100% - 340rpx)'
          }
        } else {
          style.width = '100%'
        }
        
        return style
      },
      elZIndex() {
        return this.zIndex ? this.zIndex : this.$t.zIndex.navbar
      }
    },
    data() {
      return {
        // 状态栏的高度
        statusBarHeight: 0,
        // 自定义导航栏的高度
        customBarHeight: 0,
        // 自定义返回按钮时,返回容器的宽高边距信息
        customBackStyleInfo: {
          width: 86,
          height: 32,
          left: 15
        }
      }
    },
    mounted() {
      // 获取vuex中的自定义顶栏的高度
      this.updateNavBarInfo()
    },
    created() {
      // 获取胶囊信息
      // #ifdef MP-WEIXIN
      let custom = wx.getMenuButtonBoundingClientRect()
      this.customBackStyleInfo.width = custom.width
      this.customBackStyleInfo.height = custom.height
      this.customBackStyleInfo.left = uni.upx2px(750) - custom.right
      // #endif
    },
    methods: {
      // 更新导航栏的高度
      async updateNavBarInfo() {
        // 获取vuex中的自定义顶栏的高度
        let customBarHeight = this.vuex_custom_bar_height
        let statusBarHeight = this.vuex_status_bar_height
        // 如果获取失败则重新获取
        if (!customBarHeight) {
          try {
            const navBarInfo = await this.$t.updateCustomBar()
            customBarHeight = navBarInfo.customBarHeight
            statusBarHeight = navBarInfo.statusBarHeight
          } catch(e) {
            setTimeout(() => {
              this.updateNavBarInfo()
            }, 10)
            return
          }
        }
        
        // 更新vuex中的导航栏信息
        this && this.$t.vuex('vuex_status_bar_height', statusBarHeight)
        this && this.$t.vuex('vuex_custom_bar_height', customBarHeight)
        
        this.customBarHeight = customBarHeight
        this.statusBarHeight = statusBarHeight
      },
      // 处理返回事件
      async handlerBack() {
        if (this.beforeBack && typeof(this.beforeBack) === 'function') {
          // 执行回调,同时传入索引当作参数
          // 在微信,支付宝等环境(H5正常),会导致父组件定义的函数体中的this变成子组件的this
          // 通过bind()方法,绑定父组件的this,让this的this为父组件的上下文
          let beforeBack = this.beforeBack.bind(this.$t.$parent.call(this))()
          // 判断是否返回了Promise
          if (!!beforeBack && typeof beforeBack.then === 'function') {
            await beforeBack.then(res => {
              // Promise返回成功
              this.navBack()
            }).catch(err => {})
          } else if (beforeBack === true) {
            this.navBack()
          }
        } else {
          this.navBack()
        }
      },
      // 返回上一页
      navBack() {
        
        // 通过判断当前页面的页面栈信息,是否有上一页进行返回,如果没有则跳转到首页
        const pages = getCurrentPages()
        if (pages && pages.length > 0) {
          const firstPage = pages[0]
          if (pages.length == 1 && (!firstPage.route || firstPage.route != 'pages/index/index')) {
            uni.reLaunch({
              url: '/pages/index/index'
            })
          } else {
            uni.navigateBack({
              delta: 1
            })
          }
        } else {
          uni.reLaunch({
            url: '/pages/index/index'
          })
        }
      }
    }
  }
</script>
 
<style lang="scss" scoped>
  
  .tn-custom-nav-bar {
    display: block;
    position: relative;
    
    &__bar {
      display: flex;
      position: relative;
      align-items: center;
      min-height: 100rpx;
      justify-content: space-between;
      min-height: 0px;
      /* #ifdef MP-WEIXIN */
      padding-right: 220rpx;
      /* #endif */
      /* #ifdef MP-ALIPAY */
      padding-right: 150rpx;
      /* #endif */
      box-shadow: 0rpx 0rpx 0rpx;
      z-index: 9999;
      
      &--fixed {
        position: fixed;
        width: 100%;
        top: 0;
      }
      
      &--alpha {
        background: transparent !important;
        box-shadow: none !important;
      }
      
      &--bottom-shadow {
        box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.05);
      }
      
      &__action {
        display: flex;
        align-items: center;
        height: 100%;
        justify-content: center;
        max-width: 100%;
        
        &--nav-back {
          /* position: absolute; */
          /* top: 50%; */
          /* left: 20rpx; */
          /* margin-top: -15rpx; */
          // width: 25rpx;
          // height: 25rpx;
          padding: 20rpx;
          font-size: 38rpx;
          line-height: 100%;
          // border-width: 0 0 4rpx 4rpx;
          // border-color: #000000;
          // border-style: solid;
          // transform: matrix(0.5, 0.5, -0.5, 0.5, 0, 0);
        }
        
        &--nav-back-text {
          padding: 20rpx 20rpx 20rpx 0rpx;
        }
      }
      
      &__content {
        position: absolute;
        text-align: center;
        left: 0;
        right: 0;
        bottom: 0;
        margin: auto;
        font-size: 32rpx;
        cursor: none;
        // pointer-events: none;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }
    }
  }
  
</style>