layout.vue 3.9 KB

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