layout.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <div class="layout" :class="{'layout-white': !false}">
  3. <div style="height: 12.2666666667vw" v-if="isShowNav">
  4. <van-nav-bar :title="title" left-arrow fixed @click-left="goBackFun">
  5. <template #right>
  6. <slot name="right"></slot>
  7. </template>
  8. </van-nav-bar>
  9. </div>
  10. <div class="layout-container" :style="containerStyle">
  11. <slot></slot>
  12. </div>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. name: 'layout',
  18. props: {
  19. // 是否显示头部nav
  20. isShowNav: {
  21. type: Boolean,
  22. default: true
  23. },
  24. // navbar标题
  25. title: {
  26. type: String,
  27. default: '',
  28. },
  29. padding: {
  30. type: String,
  31. default: '16px'
  32. },
  33. background: {
  34. type: String,
  35. default: '#F2F4F7'
  36. },
  37. // 用于内嵌页面,标识为最后一页
  38. isGoBack: {
  39. type: Boolean,
  40. default: false
  41. }
  42. },
  43. data() {
  44. return {
  45. isApp: null,
  46. isAndroid: null,
  47. isIos: null,
  48. };
  49. },
  50. mounted() {
  51. setTimeout(() => {
  52. // 延迟50ms执行,因为title修改时,安卓返nav会重新显示
  53. this.hideAndroidNav();
  54. }, 50)
  55. },
  56. computed: {
  57. containerStyle() {
  58. return { background: this.background, padding: this.padding};
  59. },
  60. isShowApp() {
  61. // 是否是APP
  62. const isApp = this.$userAgent.isSzx || this.$userAgent.isSzxBrowser;
  63. const isAndroid = this.$userAgent.isAndroid;
  64. const isIos = this.$userAgent.isIos;
  65. const isWx = this.$userAgent.isWx;
  66. console.log('isApp:' + isApp)
  67. console.log('isAndroid:' + isAndroid)
  68. console.log('isIos:' + isIos)
  69. console.log('isWx:' + isWx)
  70. // 如果是App并且是安卓,就不显示头部, ios显示
  71. // 如果微信小程序环境的,也不显示头部
  72. // 如果是h5 就显示头部
  73. let bool = false;
  74. bool = isApp && (isAndroid || isIos) ? true : false;
  75. return bool;
  76. },
  77. },
  78. methods: {
  79. goBackFun() {
  80. if (this?.$listeners?.goBack) {
  81. this.$emit('goBack');
  82. return;
  83. }
  84. // 如果为true是最前一页
  85. if (this.isGoBack) {
  86. if(this.isApp && this.isAndroid) {
  87. return window.native.backClick();
  88. }else if(this.isApp && this.isIos) {
  89. return window.webkit.messageHandlers.appGoBack.postMessage({});
  90. }
  91. }
  92. // 浏览器返回上一页
  93. this.$router.go(-1);
  94. },
  95. // 隐藏安卓navbar
  96. hideAndroidNav() {
  97. try {
  98. // 判断是否是App, 活动页app不显示头部,直接使用H5头部
  99. this.isApp = this.$userAgent.isSzx || this.$userAgent.isSzxBrowser
  100. this.isAndroid = this.$userAgent.isAndroid;
  101. this.isIos = this.$userAgent.isIos;
  102. // 判断如果是安卓环境,且已包含native.goneBack方法,就调用隐藏安卓navbar
  103. if (this.isApp && this.isAndroid && !!window.native.goneBack) {
  104. window.native.goneBack() // 安卓隐藏返回按钮
  105. }
  106. } catch (error) {
  107. console.error(error)
  108. }
  109. }
  110. },
  111. };
  112. </script>
  113. <style lang="less" scoped>
  114. .layout {
  115. height: 100%;
  116. display: flex;
  117. flex-direction: column;
  118. .layout-container {
  119. flex: 1;
  120. overflow-y: auto;
  121. }
  122. ::v-deep .van-nav-bar__arrow {
  123. color: #000000;
  124. font-size: 24px;
  125. }
  126. ::v-deep .van-nav-bar__title {
  127. font-weight: bold !important;
  128. font-size: 17px !important;
  129. color: #0a132b !important;
  130. line-height: 24px !important;
  131. }
  132. .van-nav-bar {
  133. z-index: 0;
  134. }
  135. .floating-back {
  136. position: fixed; /* 设定定位为固定 */
  137. left: 26px; /* 距离右侧10像素 */
  138. bottom: 98px; /* 距离底部10像素 */
  139. background-color: transparent; /* 背景颜色 */
  140. z-index: 1000; /* 确保悬浮在其他内容之上 */
  141. }
  142. }
  143. .layout-white {
  144. .van-nav-bar {
  145. background-color: #f2f4f7;
  146. }
  147. ::v-deep [class*='van-hairline']::after {
  148. border: 0px;
  149. }
  150. }
  151. </style>