AppBar.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <van-nav-bar
  3. fixed
  4. left-text=" "
  5. right-text=" "
  6. :left-arrow="false"
  7. v-if="show"
  8. z-index="20"
  9. placeholder
  10. :border="false"
  11. id="navBar"
  12. :class="{ dark: tabColor }"
  13. ref="navBar"
  14. >
  15. <template #left>
  16. <div class="back">
  17. <van-icon @click="back" size="24" name="arrow-left" :color="fontColor || $colors.font0" />
  18. </div>
  19. <van-popover
  20. v-model:show="showPopover"
  21. placement="bottom-start"
  22. theme="dark"
  23. :actions="actions"
  24. @select="onSelect"
  25. >
  26. <template #reference>
  27. <van-icon size="24" :color="fontColor || $colors.font0" name="bars" />
  28. </template>
  29. </van-popover>
  30. </template>
  31. <template #title>
  32. <span class="title" :style="{ color: fontColor || $colors.font0 }">{{ title }}</span>
  33. </template>
  34. </van-nav-bar>
  35. </template>
  36. <script>
  37. import { mapState } from 'vuex';
  38. import { useCssVar } from '@vueuse/core';
  39. export default {
  40. inject: ['setKeeps', 'safeTop', 'barHeight', 'keeps'],
  41. data() {
  42. return {
  43. show: false,
  44. showPopover: false,
  45. tabColor: '',
  46. title: ''
  47. };
  48. },
  49. computed: {
  50. ...mapState(['showConsole']),
  51. fontColor() {
  52. return this.tabColor ? '#fff' : '';
  53. },
  54. actions() {
  55. if (this.showConsole) {
  56. return [
  57. { text: '首页', icon: require('@assets/menu1.png'), value: '/home' },
  58. { text: '发现', icon: require('@assets/menu2.png'), value: '/discover' },
  59. { text: '藏品室', icon: require('@assets/menu3.png'), value: '/store' },
  60. { text: '我的', icon: require('@assets/menu4.png'), value: '/mine' },
  61. { text: '刷新', icon: 'replay', value: 'reload' }
  62. ];
  63. } else {
  64. return [
  65. { text: '首页', icon: require('@assets/menu1.png'), value: '/home' },
  66. { text: '发现', icon: require('@assets/menu2.png'), value: '/discover' },
  67. { text: '藏品室', icon: require('@assets/menu3.png'), value: '/store' },
  68. { text: '我的', icon: require('@assets/menu4.png'), value: '/mine' }
  69. ];
  70. }
  71. },
  72. height() {
  73. return this.barHeight;
  74. }
  75. },
  76. watch: {
  77. $route() {
  78. if (this.$route.meta.menuPage) {
  79. this.show = false;
  80. } else {
  81. this.show = true;
  82. }
  83. this.getColor();
  84. this.setTitle();
  85. },
  86. tabColor() {
  87. this.$nextTick(() => {
  88. if (this.$el) {
  89. let children = this.$el.children;
  90. if (children && children.length > 0) {
  91. children[0].style.backgroundColor = this.tabColor;
  92. }
  93. }
  94. });
  95. },
  96. show() {
  97. this.safeTop = useCssVar('--safe-top').value;
  98. }
  99. },
  100. mounted() {
  101. if (this.$route.meta.menuPage) {
  102. this.show = false;
  103. } else {
  104. this.show = true;
  105. }
  106. this.getColor();
  107. this.setTitle();
  108. },
  109. methods: {
  110. setTitle(title) {
  111. console.log(this.$route.meta.title);
  112. if (this.$route.meta.title) {
  113. this.title = this.$route.meta.title;
  114. } else if (title) {
  115. this.title = title;
  116. } else {
  117. this.title = '';
  118. }
  119. },
  120. getColor(color = '') {
  121. if (this.$route.meta.tabColor) {
  122. if (window.cordova && StatusBar && StatusBar.isVisible) {
  123. StatusBar.styleLightContent();
  124. }
  125. this.tabColor = this.$route.meta.tabColor;
  126. } else if (color) {
  127. if (window.cordova && StatusBar && StatusBar.isVisible) {
  128. StatusBar.styleLightContent();
  129. }
  130. this.tabColor = color;
  131. } else {
  132. if (window.cordova && StatusBar && StatusBar.isVisible) {
  133. StatusBar.styleDefault();
  134. }
  135. this.tabColor = '';
  136. }
  137. },
  138. onSelect(val) {
  139. console.log(val);
  140. if (val.value === 'back') {
  141. this.$router.go(-1);
  142. } else if (val.value === 'reload') {
  143. location.reload();
  144. } else {
  145. setTimeout(() => {
  146. this.setKeeps([], true, true);
  147. }, 10);
  148. this.$router.push(val.value);
  149. }
  150. },
  151. back() {
  152. if (window.history.length <= 1) {
  153. this.$router.push({ path: '/' });
  154. return false;
  155. } else {
  156. this.$router.go(-1);
  157. }
  158. //上面都没执行就说明卡在当前页不是最后一条, histroy记录数量大于1,又没有回退记录,只能返回首页,
  159. //如果上面都执行了 页面都跳走了,这个也就不用管了
  160. // setTimeout(() => {
  161. // this.$router.push({ path: '/' });
  162. // }, 500);
  163. }
  164. }
  165. };
  166. </script>
  167. <style lang="less">
  168. #navBar {
  169. .van-nav-bar__content {
  170. padding-top: var(--safe-top);
  171. }
  172. .van-nav-bar__left {
  173. top: var(--safe-top) !important;
  174. }
  175. .van-nav-bar {
  176. line-height: initial !important;
  177. }
  178. }
  179. .back {
  180. padding-right: 12px;
  181. }
  182. .title {
  183. font-size: 16px;
  184. font-weight: bold;
  185. }
  186. </style>