import { postMessage } from '@/helpers/native-message' import { browser } from '@/helpers/utils' import { NavBar } from 'vant' import { defineComponent, PropType } from 'vue' import styles from './index.module.less' type backIconColor = 'black' | 'white' export default defineComponent({ name: 'col-header', props: { title: String, isBack: { type: Boolean, default: false }, backIconColor: { // 返回按钮颜色 type: String as PropType, default: 'black' }, isFixed: { type: Boolean, default: true }, styleName: { type: Object, default: () => ({}) }, titleClass: String, background: { type: String, default: 'white' }, color: { type: String, default: '#323233' }, rightText: String, onClickRight: { type: Function, default: () => {} }, border: { type: Boolean, default: true }, onHeaderBack: { // 头部高度设置后返回 type: Function, default: () => {} } }, watch:{ backIconColor(){ // 设置返回按钮颜色 postMessage({ api: 'backIconChange', content: { iconStyle: this.backIconColor } }) } }, data() { return { headerTitle: null as any, navBarHeight: 0, // 顶部导航栏高度 titleHeight: 44 // 顶部导航高度(默认44px) } }, mounted() { this.headerTitle = this.title || this.$route.meta.title this.navBarInit(() => { this.onHeaderBack && this.onHeaderBack() }) }, unmounted() { // 设置是否显示导航栏 0 显示 1 不显示 postMessage({ api: 'setBarStatus', content: { status: 1 } }) // 设置返回按钮颜色 postMessage({ api: 'backIconChange', content: { iconStyle: 'black' as backIconColor } }) }, methods: { navBarInit(callBack?: Function) { // 设置是否显示导航栏 0 显示 1 不显示 postMessage({ api: 'setBarStatus', content: { status: 0 } }) // 设置返回按钮颜色 postMessage({ api: 'backIconChange', content: { iconStyle: this.backIconColor || 'black' } }) let sNavHeight = sessionStorage.getItem('navHeight') let sTitleHeight = sessionStorage.getItem('titleHeight') if (sNavHeight && sTitleHeight) { this.navBarHeight = Number(sNavHeight) callBack && callBack() } else { postMessage({ api: 'getNavHeight' }, res => { const { content } = res as any const dpi = content.dpi || 2 if (content.navHeight) { const navHeight = content.navHeight / dpi sessionStorage.setItem('navHeight', String(navHeight)) this.navBarHeight = navHeight } if (content.titleHeight) { // 导航栏的高度 const titleHeight = content.titleHeight / dpi sessionStorage.setItem('titleHeight', String(titleHeight)) this.titleHeight = titleHeight } callBack && callBack() }) } !browser().isApp && callBack && callBack() }, onClickLeft() { this.$router.back() }, clickRight() { this.onClickRight && this.onClickRight() } }, render() { return (
{this.$slots.content ? (
{this.$slots.content(this.navBarHeight)}
) : ( <>
(this.$slots.right && this.$slots.right()) || this.rightText }} >
{this.$slots.default ? this.$slots.default() : null} )}
) } })