TabBar.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // 由 App.vue 统一调用 table-dining-status 接口,根据结果跳转点餐页或选择人数页
  74. uni.$emit('checkTableDiningStatus');
  75. },
  76. fail: (err) => {
  77. if (err?.errMsg && !String(err.errMsg).includes('cancel')) {
  78. uni.showToast({ title: '扫码失败', icon: 'none' });
  79. }
  80. }
  81. });
  82. }
  83. // 底部 tabBar 跳转(需在 pages.json 中配置 tabBar.list)
  84. function go(url) {
  85. if (url === unref(getPath)) return;
  86. uni.switchTab({ url });
  87. }
  88. const getPath = computed(() => {
  89. const pages = getCurrentPages(); // 获取路由栈
  90. console.log('/' + pages[pages.length - 1].route);
  91. return '/' + pages[pages.length - 1].route;
  92. });
  93. </script>
  94. <style lang="scss" scoped>
  95. .placeholder {
  96. height: 140rpx;
  97. box-sizing: content-box;
  98. }
  99. .tab-bar-wrap {
  100. position: fixed;
  101. left: 0;
  102. right: 0;
  103. bottom: 0;
  104. z-index: 99;
  105. background-color: #ffffff;
  106. box-shadow: 0 -2rpx 16rpx 0 rgba(0, 0, 0, 0.08);
  107. .tab-bar {
  108. // padding: 16rpx 0;
  109. // padding-bottom: calc(16rpx + env(safe-area-inset-bottom));
  110. display: flex;
  111. align-items: flex-end;
  112. justify-content: space-around;
  113. position: relative;
  114. padding-top: 20rpx;
  115. .menu {
  116. display: flex;
  117. flex-direction: column;
  118. align-items: center;
  119. justify-content: center;
  120. flex: 1;
  121. padding: 0 40rpx;
  122. padding-bottom: 0;
  123. transition: all 0.3s ease;
  124. image {
  125. width: 48rpx;
  126. height: 48rpx;
  127. transition: transform 0.3s ease;
  128. }
  129. .text {
  130. font-size: 22rpx;
  131. color: #999999;
  132. margin-top: 8rpx;
  133. transition: all 0.3s ease;
  134. font-weight: 400;
  135. }
  136. .text.sele {
  137. color: #333333;
  138. font-weight: 500;
  139. }
  140. &:active {
  141. opacity: 0.7;
  142. transform: scale(0.95);
  143. }
  144. }
  145. .menu-center {
  146. display: flex;
  147. flex-direction: column;
  148. align-items: center;
  149. justify-content: center;
  150. position: absolute;
  151. bottom: -20rpx;
  152. margin: 0 40rpx;
  153. // transform: translateY(-35rpx);
  154. transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  155. background-color: #fff;
  156. border-radius: 50%;
  157. padding: 20rpx;
  158. .scan-btn {
  159. width: 110rpx;
  160. height: 110rpx;
  161. border-radius: 50%;
  162. display: flex;
  163. align-items: center;
  164. justify-content: center;
  165. margin-bottom: 6rpx;
  166. position: relative;
  167. transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  168. background: linear-gradient(35deg, #FCB73F 0%, #FC733D 100%);
  169. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.16);
  170. image {
  171. width: 42rpx;
  172. height: 42rpx;
  173. filter: brightness(0) invert(1);
  174. transition: transform 0.3s ease;
  175. margin-bottom: 20rpx;
  176. }
  177. }
  178. .qrT-img {
  179. width: 85rpx;
  180. height: 22rpx;
  181. position: absolute;
  182. bottom: 36rpx;
  183. left: 50%;
  184. transform: translateX(-50%);
  185. }
  186. .text-center {
  187. font-size: 22rpx;
  188. color: #ff8844;
  189. margin-top: 4rpx;
  190. font-weight: 500;
  191. transition: all 0.3s ease;
  192. }
  193. &:active {
  194. transform: translateY(-35rpx) scale(0.92);
  195. .scan-btn {
  196. box-shadow: 0 4rpx 16rpx 0 rgba(255, 136, 68, 0.3),
  197. 0 1rpx 4rpx 0 rgba(255, 136, 68, 0.15);
  198. image {
  199. transform: scale(0.9);
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }
  206. </style>