index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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: '#131415'
  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. hideHeader: {
  52. type: Boolean,
  53. default: false
  54. },
  55. leftClickDefault: {
  56. type: Boolean,
  57. default: true
  58. }
  59. },
  60. emits: ['leftClick'],
  61. watch: {
  62. backIconColor() {
  63. // 设置返回按钮颜色
  64. postMessage({
  65. api: 'backIconChange',
  66. content: { iconStyle: this.backIconColor }
  67. })
  68. }
  69. },
  70. data() {
  71. return {
  72. headerTitle: null as any,
  73. navBarHeight: 0, // 顶部导航栏高度
  74. titleHeight: 44 // 顶部导航高度(默认44px)
  75. }
  76. },
  77. mounted() {
  78. this.headerTitle = this.title || this.$route.meta.title
  79. this.navBarInit(() => {
  80. this.onHeaderBack && this.onHeaderBack()
  81. })
  82. },
  83. unmounted() {
  84. // 设置是否显示导航栏 0 显示 1 不显示
  85. postMessage({ api: 'setBarStatus', content: { status: 1 } })
  86. // 设置返回按钮颜色
  87. postMessage({
  88. api: 'backIconChange',
  89. content: { iconStyle: 'black' as backIconColor }
  90. })
  91. },
  92. methods: {
  93. navBarInit(callBack?: Function) {
  94. // 设置是否显示导航栏 0 显示 1 不显示
  95. postMessage({ api: 'setBarStatus', content: { status: 0 } })
  96. // 设置返回按钮颜色
  97. postMessage({
  98. api: 'backIconChange',
  99. content: { iconStyle: this.backIconColor || 'black' }
  100. })
  101. const sNavHeight = sessionStorage.getItem('navHeight')
  102. const sTitleHeight = sessionStorage.getItem('titleHeight')
  103. if (sNavHeight && sTitleHeight) {
  104. this.navBarHeight = Number(sNavHeight)
  105. callBack && callBack()
  106. } else {
  107. postMessage({ api: 'getNavHeight' }, res => {
  108. const { content } = res as any
  109. const dpi = content.dpi || 2
  110. if (content.navHeight) {
  111. const navHeight = content.navHeight / dpi
  112. sessionStorage.setItem('navHeight', String(navHeight))
  113. this.navBarHeight = navHeight
  114. }
  115. if (content.titleHeight) {
  116. // 导航栏的高度
  117. const titleHeight = content.titleHeight / dpi
  118. sessionStorage.setItem('titleHeight', String(titleHeight))
  119. this.titleHeight = titleHeight
  120. }
  121. callBack && callBack()
  122. })
  123. }
  124. !browser().isApp && callBack && callBack()
  125. },
  126. onClickLeft() {
  127. this.$emit('leftClick', null)
  128. postMessage({
  129. api: 'setStatusBarTextColor',
  130. content: { statusBarTextColor: false }
  131. })
  132. if(this.leftClickDefault) {
  133. if (browser().isApp) {
  134. postMessage({ api: 'goBack' })
  135. } else {
  136. this.$router.back()
  137. }
  138. }
  139. },
  140. clickRight() {
  141. this.onClickRight && this.onClickRight()
  142. }
  143. },
  144. render() {
  145. // 只有app里面才显示头部
  146. return !this.hideHeader ? (
  147. <div>
  148. {this.$slots.content ? (
  149. <div
  150. style={{
  151. paddingTop: `${this.navBarHeight}px`,
  152. background: this.background
  153. }}
  154. class={styles.headerSection}
  155. >
  156. {this.$slots.content(this.navBarHeight)}
  157. </div>
  158. ) : (
  159. <>
  160. <div
  161. // style={{ paddingTop: `${this.navBarHeight}px` }}
  162. style={{
  163. minHeight: `calc(var(--van-nav-bar-height) + ${this.navBarHeight}px)`
  164. }}
  165. class={styles.headerSection}
  166. >
  167. <NavBar
  168. title={this.headerTitle}
  169. class={[styles.colHeader]}
  170. style={{
  171. background: this.background,
  172. color: this.color,
  173. paddingTop: `${this.navBarHeight}px`,
  174. zIndex: 99
  175. }}
  176. left-arrow={this.isBack}
  177. rightText={this.rightText}
  178. fixed={this.isFixed}
  179. border={this.border}
  180. onClick-right={this.clickRight}
  181. onClick-left={this.onClickLeft}
  182. v-slots={{
  183. right: () =>
  184. (this.$slots.right && this.$slots.right()) || this.rightText
  185. }}
  186. ></NavBar>
  187. </div>
  188. {this.$slots.default ? this.$slots.default() : null}
  189. </>
  190. )}
  191. </div>
  192. ) : (
  193. <div></div>
  194. )
  195. }
  196. })