header.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <div
  3. class="mheader"
  4. v-if="appHide"
  5. :style="{ height: `calc(${titleHeight}px + ${navBarHeight}px)` }"
  6. >
  7. <header
  8. class="m-nav-header"
  9. :style="[styleName, headStyle]"
  10. :class="[isFixed ? 'fixed' : '', background == 'white' ? 'white' : null]"
  11. >
  12. <div class="m-nav-bar__left" @click="goBack" v-show="isBack">
  13. <van-icon class="arrow-left" name="arrow-left"></van-icon>
  14. </div>
  15. <div class="m-nav-bar__title">
  16. <slot>{{ name ? name : $route.meta.descrition }}</slot>
  17. </div>
  18. <div class="m-nav-bar__right">
  19. <slot name="right"></slot>
  20. </div>
  21. </header>
  22. </div>
  23. </template>
  24. <script>
  25. /**
  26. * 插槽使用方式
  27. * <template v-slot:right>
  28. 这里
  29. </template>
  30. */
  31. // 判断当前是否是app
  32. import { postMessage } from "@/helpers/native-message";
  33. import { browser } from "@/common/common";
  34. const backStatus = browser().isApp ? false : true;
  35. export default {
  36. name: "mheader",
  37. props: {
  38. appHide: {
  39. name: Boolean,
  40. default() {
  41. return !backStatus;
  42. },
  43. },
  44. name: String, // 标题名称
  45. isBack: {
  46. // 是否显示返回按钮
  47. type: Boolean,
  48. default: backStatus,
  49. },
  50. isFixed: {
  51. // 是否固定顶部
  52. type: Boolean,
  53. default: true,
  54. },
  55. styleName: {
  56. type: Object,
  57. default() {
  58. return {};
  59. },
  60. },
  61. background: String, // 背景色
  62. titleClass: String, // 标题颜色
  63. backUrl: {
  64. // 跳转指定的URL
  65. type: Object,
  66. default: () => {
  67. return {
  68. callBack: null, // 方法
  69. status: false, // 是否用指定的连接跳转
  70. path: "", // 跳转路径
  71. params: {}, // 跳转参数 目前只能用query的方式
  72. };
  73. },
  74. },
  75. },
  76. data() {
  77. return {
  78. title: this.name,
  79. backUrlParams: this.backUrl, // 取参数
  80. navBarHeight: 0, // 顶部导航栏高度
  81. headStyle: {},
  82. titleHeight: 44, // 顶部导航高度(默认44px)
  83. };
  84. },
  85. async created() {
  86. await postMessage({ api: "setBarStatus", content: { status: 0 } });
  87. await postMessage({
  88. api: "backIconChange",
  89. content: { iconStyle: "black" },
  90. });
  91. let sNavHeight = sessionStorage.getItem("navHeight");
  92. let sTitleHeight = sessionStorage.getItem("titleHeight");
  93. if (sNavHeight && sTitleHeight) {
  94. this.navBarHeight = sNavHeight;
  95. this.headStyle = {
  96. paddingTop: sNavHeight + "px",
  97. height: sTitleHeight + "px",
  98. lineHeight: sTitleHeight + "px",
  99. };
  100. } else {
  101. await postMessage({ api: "getNavHeight" }, (res) => {
  102. const { content } = res;
  103. let headStyle = {};
  104. const dpi = content.dpi || 2;
  105. if (content.navHeight) {
  106. const navHeight = content.navHeight / dpi;
  107. sessionStorage.setItem("navHeight", navHeight);
  108. this.navBarHeight = navHeight;
  109. headStyle = {
  110. paddingTop: navHeight + "px",
  111. };
  112. }
  113. if (content.titleHeight) {
  114. // 导航栏的高度
  115. const titleHeight = content.titleHeight / dpi;
  116. sessionStorage.setItem("titleHeight", titleHeight);
  117. this.titleHeight = titleHeight;
  118. headStyle.height = titleHeight + "px";
  119. headStyle.lineHeight = titleHeight + "px";
  120. }
  121. this.headStyle = headStyle;
  122. });
  123. }
  124. },
  125. methods: {
  126. goBack() {
  127. // 返回上层
  128. let urlObj = this.backUrlParams;
  129. // console.log(typeof urlObj.callBack)
  130. if (typeof urlObj.callBack == "function") {
  131. urlObj.callBack();
  132. } else {
  133. if (urlObj.status) {
  134. this.$router.push({
  135. path: urlObj.path,
  136. query: urlObj.params,
  137. });
  138. } else {
  139. history.go(-1);
  140. }
  141. }
  142. },
  143. },
  144. };
  145. </script>
  146. <style lang="less" scoped>
  147. @import url("../assets/commonLess/variable");
  148. .mheader {
  149. height: 44px;
  150. overflow: hidden;
  151. transition: all ease 0.2s;
  152. }
  153. .m-nav-header {
  154. position: absolute;
  155. left: 0;
  156. top: 0;
  157. width: 100%;
  158. height: 44px;
  159. line-height: 44px;
  160. background-color: #fff;
  161. color: #000000;
  162. text-align: center;
  163. user-select: none;
  164. &.fixed {
  165. position: fixed;
  166. z-index: 99;
  167. }
  168. &.white {
  169. background-color: @mColor;
  170. color: @whiteColor;
  171. .m-nav-bar__title {
  172. color: @whiteColor;
  173. }
  174. }
  175. .m-nav-bar__title {
  176. max-width: 60%;
  177. margin: 0 auto;
  178. color: #000000;
  179. font-weight: 500;
  180. font-size: 0.16rem;
  181. }
  182. .m-nav-bar__left,
  183. .m-nav-bar__right {
  184. position: absolute;
  185. bottom: 0;
  186. }
  187. .m-nav-bar__left {
  188. left: 0.12rem;
  189. .arrow-left {
  190. font-size: 0.21rem;
  191. vertical-align: middle;
  192. }
  193. }
  194. .m-nav-bar__right {
  195. right: 0.12rem;
  196. }
  197. }
  198. </style>