layout.vue 3.8 KB

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