fa-tabbar.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <template>
  2. <!-- #ifndef APP-PLUS -->
  3. <view v-if="tabbar.isshow && showTabbar" class="u-tabbar" @touchmove.stop.prevent>
  4. <view
  5. class="u-tabbar__content safe-area-inset-bottom"
  6. :style="{
  7. height: $u.addUnit(tabbar.height),
  8. backgroundColor: tabbar.bgColor
  9. }"
  10. :class="{
  11. 'u-border-top': tabbar.borderTop
  12. }"
  13. >
  14. <view
  15. class="u-tabbar__content__item"
  16. v-for="(item, index) in tabbar.list"
  17. :key="index"
  18. v-if="index<5"
  19. :class="{
  20. 'u-tabbar__content__circle': tabbar.midButton && item.midButton
  21. }"
  22. @tap.stop="clickHandler(index)"
  23. :style="{
  24. backgroundColor: tabbar.bgColor
  25. }"
  26. >
  27. <view :class="[tabbar.midButton && item.midButton ? 'u-tabbar__content__circle__button' : 'u-tabbar__content__item__button']">
  28. <u-image
  29. :lazy-load="false"
  30. :duration="0"
  31. :show-loading="false"
  32. :fade="false"
  33. :width="tabbar.midButton && item.midButton ? tabbar.midButtonSize : tabbar.iconSize"
  34. :height="tabbar.midButton && item.midButton ? tabbar.midButtonSize : tabbar.iconSize"
  35. :src="elIconPath(index)"
  36. ></u-image>
  37. <u-badge :count="item.count" :is-dot="item.isDot" :color="item.badgeColor" :bgColor="item.badgeBgColor" :offset="offsetWz(item.count, item.isDot)"></u-badge>
  38. </view>
  39. <view
  40. class="u-tabbar__content__item__text"
  41. :style="{
  42. color: elColor(index)
  43. }"
  44. >
  45. <text class="u-line-1">{{ item.text }}</text>
  46. </view>
  47. </view>
  48. <view
  49. v-if="tabbar.midButton"
  50. class="u-tabbar__content__circle__border"
  51. :class="{
  52. 'u-border': tabbar.borderTop
  53. }"
  54. :style="{
  55. backgroundColor: tabbar.bgColor,
  56. left: tabbar.midButtonLeft
  57. }"
  58. ></view>
  59. </view>
  60. <!-- 这里加上一个48rpx的高度,是为了增高有凸起按钮时的防塌陷高度(也即按钮凸出来部分的高度) -->
  61. <view
  62. class="u-fixed-placeholder safe-area-inset-bottom"
  63. :style="{
  64. height: `calc(${$u.addUnit(tabbar.height)} + ${tabbar.midButton ? 48 : 0}rpx)`
  65. }"
  66. ></view>
  67. </view>
  68. <!-- #endif -->
  69. </template>
  70. <script>
  71. // #ifndef APP-PLUS
  72. export default {
  73. props: {
  74. // 通过v-model绑定current值
  75. value: {
  76. type: [String, Number],
  77. default: 0
  78. },
  79. // 切换前的回调
  80. beforeSwitch: {
  81. type: Function,
  82. default: null
  83. }
  84. },
  85. data() {
  86. return {
  87. pageUrl: '' // 当前
  88. };
  89. },
  90. created() {
  91. //隐藏系统菜单栏
  92. uni.hideTabBar();
  93. // 获取引入了u-tabbar页面的路由地址,该地址没有路径前面的"/"
  94. let pages = getCurrentPages();
  95. // 页面栈中的最后一个即为项为当前页面,route属性为页面路径
  96. this.pageUrl = pages[pages.length - 1].route;
  97. },
  98. computed: {
  99. elIconPath() {
  100. return index => {
  101. // 历遍u-tabbar的每一项item时,判断是否传入了pagePath参数,如果传入了
  102. // 和data中的pageUrl参数对比,如果相等,即可判断当前的item对应当前的tabbar页面,设置高亮图标
  103. // 采用这个方法,可以无需使用v-model绑定的value值
  104. let path = this.$util.getPath(this.tabbar.list[index].path);
  105. // 如果定义了pagePath属性,意味着使用系统自带tabbar方案,否则使用一个页面用几个组件模拟tabbar页面的方案
  106. // 这两个方案对处理tabbar item的激活与否方式不一样
  107. if (path) {
  108. if (path == this.pageUrl || path == '/' + this.pageUrl) {
  109. return this.tabbar.list[index].selectedImage;
  110. } else {
  111. return this.tabbar.list[index].image;
  112. }
  113. } else {
  114. // 普通方案中,索引等于v-model值时,即为激活项
  115. return index == this.value ? this.tabbar.list[index].selectedImage : this.tabbar.list[index].image;
  116. }
  117. };
  118. },
  119. elColor() {
  120. return index => {
  121. // 判断方法同理于elIconPath
  122. let path = this.$util.getPath(this.tabbar.list[index].path);
  123. if (path) {
  124. if (path == this.pageUrl || path == '/' + this.pageUrl) return this.tabbar.selectColor;
  125. else return this.tabbar.color;
  126. } else {
  127. return index == this.value ? this.tabbar.selectColor : this.tabbar.color;
  128. }
  129. };
  130. },
  131. // 计算角标的right值
  132. offsetWz() {
  133. return (count, isDot) => {
  134. // 点类型,count大于9(两位数),分别设置不同的right值,避免位置太挤
  135. if (isDot) {
  136. return [-2, -20];
  137. } else if (count > 9) {
  138. return [-2, -40];
  139. } else {
  140. return [-2, -30];
  141. }
  142. };
  143. },
  144. tabbar() {
  145. if (this.vuex_config.tabbar) {
  146. //console.log(this.vuex_config.tabbar.list);
  147. return this.vuex_config.tabbar;
  148. } else {
  149. return {
  150. isshow: false
  151. };
  152. }
  153. },
  154. showTabbar() {
  155. return(true);
  156. if (this.tabbar.list) {
  157. let status = false;
  158. this.tabbar.list.forEach(item => {
  159. let path = this.$util.getPath(item.path);
  160. console.log(item.path);
  161. if (path == this.pageUrl || path == '/' + this.pageUrl) {
  162. status = true;
  163. this.$u.vuex('vuex_table_title', item.text);
  164. }
  165. });
  166. return status;
  167. }
  168. }
  169. },
  170. mounted() {
  171. this.tabbar.midButton && this.getMidButtonLeft();
  172. },
  173. methods: {
  174. // #ifdef MP-BAIDU
  175. setTabBarStyle(e) {
  176. console.log(e);
  177. },
  178. // #endif
  179. async clickHandler(index) {
  180. uni.$emit("centerButtonClick",index);
  181. if (this.beforeSwitch && (typeof this.beforeSwitch === 'function') ){
  182. // 执行回调,同时传入索引当作参数
  183. // 在微信,支付宝等环境(H5正常),会导致父组件定义的customBack()函数体中的this变成子组件的this
  184. // 通过bind()方法,绑定父组件的this,让this.customBack()的this为父组件的上下文
  185. console.log('beforeSwitch');
  186. let beforeSwitch = this.beforeSwitch.bind(this.$u.$parent.call(this))(index);
  187. // 判断是否返回了promise
  188. if (!!beforeSwitch && typeof beforeSwitch.then === 'function') {
  189. await beforeSwitch
  190. .then(res => {
  191. // promise返回成功,
  192. this.switchTab(index);
  193. })
  194. .catch(err => {});
  195. } else if (beforeSwitch === true) {
  196. // 如果返回true
  197. this.switchTab(index);
  198. }
  199. } else {
  200. this.switchTab(index);
  201. }
  202. },
  203. // 切换tab
  204. switchTab(index) {
  205. //同一个页面不做处理
  206. let path = this.$util.getPath(this.tabbar.list[index].path);
  207. if (path == this.pageUrl || path == '/' + this.pageUrl) {
  208. return;
  209. }
  210. if (this.tabbar.list[index].path.indexOf('http') != -1) {
  211. //外部链接
  212. this.$u.vuex('vuex_webs', {
  213. url: this.tabbar.list[index].path,
  214. title: this.tabbar.list[index].text
  215. });
  216. this.$Router.push('/pages/webview/webview');
  217. return;
  218. }
  219. // 发出事件和修改v-model绑定的值
  220. this.$emit('change', index);
  221. if (this.tabbar.list[index].path) {
  222. this.$u.vuex('vuex_table_title', this.tabbar.list[index].text);
  223. // #ifndef APP-PLUS
  224. this.$Router.replaceAll(this.tabbar.list[index].path);
  225. //console.log(this.tabbar.list[index].path);
  226. // #endif
  227. // #ifdef APP-PLUS
  228. uni.redirectTo({
  229. url: this.tabbar.list[index].path
  230. });
  231. // #endif
  232. } else {
  233. // 如果配置了papgePath属性,将不会双向绑定v-model传入的value值
  234. // 因为这个模式下,不再需要v-model绑定的value值了,而是通过getCurrentPages()适配
  235. this.$emit('input', index);
  236. }
  237. },
  238. // 获取凸起按钮外层元素的left值,让其水平居中
  239. getMidButtonLeft() {
  240. let windowWidth = this.$u.sys().windowWidth;
  241. // 由于安卓中css计算left: 50%的结果不准确,故用js计算
  242. this.tabbar.midButtonLeft = windowWidth / 2 + 'px';
  243. }
  244. }
  245. };
  246. // #endif
  247. </script>
  248. <style scoped lang="scss">
  249. // #ifndef APP-PLUS
  250. .u-fixed-placeholder {
  251. box-sizing: content-box;
  252. }
  253. .u-tabbar {
  254. &__content {
  255. display: flex;
  256. align-items: center;
  257. position: relative;
  258. position: fixed;
  259. // #ifdef MP-ALIPAY
  260. bottom: -70rpx;
  261. // #endif
  262. // #ifndef MP-ALIPAY
  263. bottom: 0;
  264. // #endif
  265. left: 0;
  266. width: 100%;
  267. z-index: 998;
  268. box-sizing: content-box;
  269. &__circle__border {
  270. border-radius: 100%;
  271. width: 110rpx;
  272. height: 110rpx;
  273. top: -48rpx;
  274. position: absolute;
  275. z-index: 4;
  276. background-color: #ffffff;
  277. // 由于安卓的无能,导致只有3个tabbar item时,此css计算方式有误差
  278. // 故使用js计算的形式来定位,此处不注释,是因为js计算有延后,避免出现位置闪动
  279. left: 50%;
  280. transform: translateX(-50%);
  281. &:after {
  282. border-radius: 100px;
  283. }
  284. }
  285. &__item {
  286. flex: 1;
  287. justify-content: center;
  288. height: 100%;
  289. padding: 12rpx 0;
  290. display: flex;
  291. flex-direction: column;
  292. align-items: center;
  293. position: relative;
  294. &__button {
  295. position: absolute;
  296. top: 10rpx;
  297. }
  298. &__text {
  299. color: $u-content-color;
  300. font-size: 26rpx;
  301. line-height: 28rpx;
  302. position: absolute;
  303. bottom: 12rpx;
  304. left: 50%;
  305. transform: translateX(-50%);
  306. }
  307. }
  308. &__circle {
  309. position: relative;
  310. display: flex;
  311. flex-direction: column;
  312. justify-content: space-between;
  313. z-index: 10;
  314. height: calc(100% - 1px);
  315. &__button {
  316. width: 90rpx;
  317. height: 90rpx;
  318. border-radius: 100%;
  319. display: flex;
  320. justify-content: center;
  321. align-items: center;
  322. position: absolute;
  323. background-color: #ffffff;
  324. top: -40rpx;
  325. left: 50%;
  326. z-index: 6;
  327. transform: translateX(-50%);
  328. }
  329. }
  330. }
  331. }
  332. // #endif
  333. </style>