index.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { postMessage } from '@/helpers/native-message'
  2. import { browser } from '@/helpers/utils'
  3. import { NavBar } from 'vant'
  4. import { defineComponent, PropType } from 'vue'
  5. import styles from './index.module.less'
  6. type backIconColor = 'black' | 'white'
  7. export default defineComponent({
  8. name: 'col-header',
  9. props: {
  10. title: String,
  11. isBack: {
  12. type: Boolean,
  13. default: false
  14. },
  15. backIconColor: {
  16. // 返回按钮颜色
  17. type: String as PropType<backIconColor>,
  18. default: 'black'
  19. },
  20. isFixed: {
  21. type: Boolean,
  22. default: true
  23. },
  24. styleName: {
  25. type: Object,
  26. default: () => ({})
  27. },
  28. titleClass: String,
  29. background: {
  30. type: String,
  31. default: 'white'
  32. },
  33. color: {
  34. type: String,
  35. default: '#323233'
  36. },
  37. rightText: String,
  38. onClickRight: {
  39. type: Function,
  40. default: () => {}
  41. },
  42. border: {
  43. type: Boolean,
  44. default: true
  45. },
  46. onHeaderBack: {
  47. // 头部高度设置后返回
  48. type: Function,
  49. default: () => {}
  50. }
  51. },
  52. watch:{
  53. backIconColor(){
  54. // 设置返回按钮颜色
  55. postMessage({
  56. api: 'backIconChange',
  57. content: { iconStyle: this.backIconColor }
  58. })
  59. }
  60. },
  61. data() {
  62. return {
  63. headerTitle: null as any,
  64. navBarHeight: 0, // 顶部导航栏高度
  65. titleHeight: 44 // 顶部导航高度(默认44px)
  66. }
  67. },
  68. mounted() {
  69. this.headerTitle = this.title || this.$route.meta.title
  70. this.navBarInit(() => {
  71. this.onHeaderBack && this.onHeaderBack()
  72. })
  73. },
  74. unmounted() {
  75. // 设置是否显示导航栏 0 显示 1 不显示
  76. postMessage({ api: 'setBarStatus', content: { status: 1 } })
  77. // 设置返回按钮颜色
  78. postMessage({
  79. api: 'backIconChange',
  80. content: { iconStyle: 'black' as backIconColor }
  81. })
  82. },
  83. methods: {
  84. navBarInit(callBack?: Function) {
  85. // 设置是否显示导航栏 0 显示 1 不显示
  86. postMessage({ api: 'setBarStatus', content: { status: 0 } })
  87. // 设置返回按钮颜色
  88. postMessage({
  89. api: 'backIconChange',
  90. content: { iconStyle: this.backIconColor || 'black' }
  91. })
  92. let sNavHeight = sessionStorage.getItem('navHeight')
  93. let sTitleHeight = sessionStorage.getItem('titleHeight')
  94. if (sNavHeight && sTitleHeight) {
  95. this.navBarHeight = Number(sNavHeight)
  96. callBack && callBack()
  97. } else {
  98. postMessage({ api: 'getNavHeight' }, res => {
  99. const { content } = res as any
  100. const dpi = content.dpi || 2
  101. if (content.navHeight) {
  102. const navHeight = content.navHeight / dpi
  103. sessionStorage.setItem('navHeight', String(navHeight))
  104. this.navBarHeight = navHeight
  105. }
  106. if (content.titleHeight) {
  107. // 导航栏的高度
  108. const titleHeight = content.titleHeight / dpi
  109. sessionStorage.setItem('titleHeight', String(titleHeight))
  110. this.titleHeight = titleHeight
  111. }
  112. callBack && callBack()
  113. })
  114. }
  115. !browser().isApp && callBack && callBack()
  116. },
  117. onClickLeft() {
  118. this.$router.back()
  119. },
  120. clickRight() {
  121. this.onClickRight && this.onClickRight()
  122. }
  123. },
  124. render() {
  125. return (
  126. <div>
  127. {this.$slots.content ? (
  128. <div
  129. style={{ paddingTop: `${this.navBarHeight}px` }}
  130. class={styles.headerSection}
  131. >
  132. {this.$slots.content(this.navBarHeight)}
  133. </div>
  134. ) : (
  135. <>
  136. <div
  137. // style={{ paddingTop: `${this.navBarHeight}px` }}
  138. style={{
  139. minHeight: `calc(var(--van-nav-bar-height) + ${this.navBarHeight}px)`
  140. }}
  141. class={styles.headerSection}
  142. >
  143. <NavBar
  144. title={this.headerTitle}
  145. class={[styles.colHeader]}
  146. style={{
  147. background: this.background,
  148. color: this.color,
  149. paddingTop: `${this.navBarHeight}px`,
  150. zIndex: 99
  151. }}
  152. left-arrow={this.isBack}
  153. rightText={this.rightText}
  154. fixed={this.isFixed}
  155. border={this.border}
  156. onClick-right={this.clickRight}
  157. onClick-left={this.onClickLeft}
  158. v-slots={{
  159. right: () =>
  160. (this.$slots.right && this.$slots.right()) || this.rightText
  161. }}
  162. ></NavBar>
  163. </div>
  164. {this.$slots.default ? this.$slots.default() : null}
  165. </>
  166. )}
  167. </div>
  168. )
  169. }
  170. })