fa-tabbar.vue 10 KB

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