index.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { postMessage } from '@/helpers/native-message'
  2. import { browser } from '@/helpers/utils'
  3. import { NavBar } from 'vant'
  4. import { defineComponent, PropType, Teleport } 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: true
  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. const sNavHeight = sessionStorage.getItem('navHeight')
  93. const 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. if (browser().isApp) {
  119. postMessage({ api: 'goBack' })
  120. } else {
  121. this.$router.back()
  122. }
  123. },
  124. clickRight() {
  125. this.onClickRight && this.onClickRight()
  126. }
  127. },
  128. render() {
  129. return (
  130. <div>
  131. {this.$slots.content ? (
  132. <div
  133. style={{
  134. paddingTop: `${this.navBarHeight}px`,
  135. background: this.background
  136. }}
  137. class={styles.headerSection}
  138. >
  139. {this.$slots.content(this.navBarHeight)}
  140. </div>
  141. ) : (
  142. <>
  143. <div
  144. // style={{ paddingTop: `${this.navBarHeight}px` }}
  145. style={{
  146. minHeight: `calc(var(--van-nav-bar-height) + ${this.navBarHeight}px)`
  147. }}
  148. class={styles.headerSection}
  149. >
  150. <NavBar
  151. title={this.headerTitle}
  152. class={[styles.colHeader]}
  153. style={{
  154. background: this.background,
  155. color: this.color,
  156. paddingTop: `${this.navBarHeight}px`,
  157. zIndex: 99
  158. }}
  159. left-arrow={this.isBack}
  160. rightText={this.rightText}
  161. fixed={this.isFixed}
  162. border={this.border}
  163. onClick-right={this.clickRight}
  164. onClick-left={this.onClickLeft}
  165. v-slots={{
  166. right: () =>
  167. (this.$slots.right && this.$slots.right()) || this.rightText
  168. }}
  169. ></NavBar>
  170. </div>
  171. {this.$slots.default ? this.$slots.default() : null}
  172. </>
  173. )}
  174. </div>
  175. )
  176. }
  177. })