TabBar.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <template>
  2. <view class="placeholder safe-area" v-if="placeholder && fixed"></view>
  3. <view class="tab-bar-wrap safe-area" :style="[getTabBarWrapStyle]">
  4. <view class="tab-bar">
  5. <!-- 左侧:首页 -->
  6. <view class="menu" @click="go(menus[0].link)">
  7. <image v-if="getPath === menus[0].link" :src="getFileUrl(menus[0].img)" mode="aspectFill"></image>
  8. <image v-else :src="getFileUrl(menus[0].imgon)" mode="aspectFill"></image>
  9. <view class="text" :class="{ sele: getPath === menus[0].link }">{{ menus[0].title }}</view>
  10. </view>
  11. <!-- 中间:扫码点餐 - 点击先调起微信扫一扫 -->
  12. <view class="menu-center" @click="handleScanOrder">
  13. <view class="scan-btn">
  14. <image :src="getFileUrl(menus[1].img)" mode="aspectFill"></image>
  15. </view>
  16. <image :src="getFileUrl(menus[1].imgon)" mode="heightFix" class="qrT-img"></image>
  17. </view>
  18. <!-- 右侧:我的 -->
  19. <view class="menu" @click="go(menus[2].link)">
  20. <image v-if="getPath === menus[2].link" :src="getFileUrl(menus[2].img)" mode="aspectFill"></image>
  21. <image v-else :src="getFileUrl(menus[2].imgon)" mode="aspectFill"></image>
  22. <view class="text" :class="{ sele: getPath === menus[2].link }">{{ menus[2].title }}</view>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script setup>
  28. import { computed, unref } from 'vue';
  29. import { getFileUrl } from '@/utils/file.js';
  30. import { SCAN_QR_CACHE } from '@/settings/enums.js';
  31. const menus = [
  32. { title: '首页', link: '/pages/index/index', img: 'img/tabbar/index1.png', imgon: 'img/tabbar/index2.png' },
  33. { title: '扫码点餐', link: '/pages/numberOfDiners/index', img: 'img/tabbar/qr2.png', imgon: 'img/tabbar/qr.png' },
  34. { title: '我的', link: '/pages/personal/index', img: 'img/tabbar/personal1.png', imgon: 'img/tabbar/personal2.png' }
  35. ];
  36. const props = defineProps({
  37. placeholder: { type: Boolean, default: false }, // 是否显示占位
  38. fixed: { type: Boolean, default: true } // 是否定位
  39. });
  40. const getTabBarWrapStyle = computed(() => ({
  41. position: props.fixed ? 'fixed' : 'relative'
  42. }));
  43. // 解析扫码内容:s=店铺id,t=桌号id
  44. function parseScanResult(str) {
  45. const trim = (v) => (v == null ? '' : String(v).trim());
  46. let storeId = '';
  47. let tableId = '';
  48. if (!str) return { storeId, tableId };
  49. const s = trim(str);
  50. try {
  51. const parts = s.split('&');
  52. parts.forEach((pair) => {
  53. const [k, v] = pair.split('=').map((x) => (x ? decodeURIComponent(String(x).trim()) : ''));
  54. if (k === 's' || k === 'storeId' || k === 'store_id') storeId = trim(v);
  55. if (k === 't' || k === 'tableId' || k === 'table_id' || k === 'tableid') tableId = trim(v);
  56. });
  57. } catch (err) {
  58. console.warn('parseScanResult', err);
  59. }
  60. return { storeId, tableId };
  61. }
  62. // 点击扫码点餐:先调起微信扫描二维码,解析后缓存再跳转
  63. function handleScanOrder() {
  64. uni.scanCode({
  65. scanType: ['qrCode'],
  66. success: (res) => {
  67. const result = res?.result ?? '';
  68. const { storeId, tableId } = parseScanResult(result);
  69. const payload = { raw: result, storeId, tableId };
  70. uni.setStorageSync(SCAN_QR_CACHE, JSON.stringify(payload));
  71. if (storeId) uni.setStorageSync('currentStoreId', storeId);
  72. if (tableId) uni.setStorageSync('currentTableId', tableId);
  73. if (unref(getPath) !== menus[1].link) {
  74. uni.switchTab({ url: menus[1].link });
  75. }
  76. },
  77. fail: (err) => {
  78. if (err?.errMsg && !String(err.errMsg).includes('cancel')) {
  79. uni.showToast({ title: '扫码失败', icon: 'none' });
  80. }
  81. }
  82. });
  83. }
  84. // 底部 tabBar 跳转(需在 pages.json 中配置 tabBar.list)
  85. function go(url) {
  86. if (url === unref(getPath)) return;
  87. uni.switchTab({ url });
  88. }
  89. const getPath = computed(() => {
  90. const pages = getCurrentPages(); // 获取路由栈
  91. console.log('/' + pages[pages.length - 1].route);
  92. return '/' + pages[pages.length - 1].route;
  93. });
  94. </script>
  95. <style lang="scss" scoped>
  96. .placeholder {
  97. height: 140rpx;
  98. box-sizing: content-box;
  99. }
  100. .tab-bar-wrap {
  101. position: fixed;
  102. left: 0;
  103. right: 0;
  104. bottom: 0;
  105. z-index: 99;
  106. background-color: #ffffff;
  107. box-shadow: 0 -2rpx 16rpx 0 rgba(0, 0, 0, 0.08);
  108. .tab-bar {
  109. // padding: 16rpx 0;
  110. // padding-bottom: calc(16rpx + env(safe-area-inset-bottom));
  111. display: flex;
  112. align-items: flex-end;
  113. justify-content: space-around;
  114. position: relative;
  115. padding-top: 20rpx;
  116. .menu {
  117. display: flex;
  118. flex-direction: column;
  119. align-items: center;
  120. justify-content: center;
  121. flex: 1;
  122. padding: 0 40rpx;
  123. padding-bottom: 0;
  124. transition: all 0.3s ease;
  125. image {
  126. width: 48rpx;
  127. height: 48rpx;
  128. transition: transform 0.3s ease;
  129. }
  130. .text {
  131. font-size: 22rpx;
  132. color: #999999;
  133. margin-top: 8rpx;
  134. transition: all 0.3s ease;
  135. font-weight: 400;
  136. }
  137. .text.sele {
  138. color: #333333;
  139. font-weight: 500;
  140. }
  141. &:active {
  142. opacity: 0.7;
  143. transform: scale(0.95);
  144. }
  145. }
  146. .menu-center {
  147. display: flex;
  148. flex-direction: column;
  149. align-items: center;
  150. justify-content: center;
  151. position: absolute;
  152. bottom: -20rpx;
  153. margin: 0 40rpx;
  154. // transform: translateY(-35rpx);
  155. transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  156. background-color: #fff;
  157. border-radius: 50%;
  158. padding: 20rpx;
  159. .scan-btn {
  160. width: 110rpx;
  161. height: 110rpx;
  162. border-radius: 50%;
  163. display: flex;
  164. align-items: center;
  165. justify-content: center;
  166. margin-bottom: 6rpx;
  167. position: relative;
  168. transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  169. background: linear-gradient(35deg, #FCB73F 0%, #FC733D 100%);
  170. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.16);
  171. image {
  172. width: 42rpx;
  173. height: 42rpx;
  174. filter: brightness(0) invert(1);
  175. transition: transform 0.3s ease;
  176. margin-bottom: 20rpx;
  177. }
  178. }
  179. .qrT-img {
  180. width: 85rpx;
  181. height: 22rpx;
  182. position: absolute;
  183. bottom: 36rpx;
  184. left: 50%;
  185. transform: translateX(-50%);
  186. }
  187. .text-center {
  188. font-size: 22rpx;
  189. color: #ff8844;
  190. margin-top: 4rpx;
  191. font-weight: 500;
  192. transition: all 0.3s ease;
  193. }
  194. &:active {
  195. transform: translateY(-35rpx) scale(0.92);
  196. .scan-btn {
  197. box-shadow: 0 4rpx 16rpx 0 rgba(255, 136, 68, 0.3),
  198. 0 1rpx 4rpx 0 rgba(255, 136, 68, 0.15);
  199. image {
  200. transform: scale(0.9);
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }
  207. </style>