TabBar.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. import { useUserStore } from '@/store/user.js';
  32. import { TOKEN } from '@/settings/enums.js';
  33. const menus = [
  34. { title: '首页', link: '/pages/index/index', img: 'img/tabbar/index1.png', imgon: 'img/tabbar/index2.png' },
  35. { title: '扫码点餐', link: '/pages/numberOfDiners/index', img: 'img/tabbar/qr2.png', imgon: 'img/tabbar/qr.png' },
  36. { title: '我的', link: '/pages/personal/index', img: 'img/tabbar/personal1.png', imgon: 'img/tabbar/personal2.png' }
  37. ];
  38. const props = defineProps({
  39. placeholder: { type: Boolean, default: false }, // 是否显示占位
  40. fixed: { type: Boolean, default: true } // 是否定位
  41. });
  42. const getTabBarWrapStyle = computed(() => ({
  43. position: props.fixed ? 'fixed' : 'relative'
  44. }));
  45. // 解析扫码内容:支持 s=店铺id&t=桌号id、URL 的 query、scene 参数、JSON 等格式
  46. function parseScanResult(str) {
  47. const trim = (v) => (v == null ? '' : String(v).trim());
  48. let storeId = '';
  49. let tableId = '';
  50. if (!str) return { storeId, tableId };
  51. let s = trim(str);
  52. const parseKv = (text) => {
  53. const out = { storeId: '', tableId: '' };
  54. const keys = {
  55. store: ['s', 'storeid', 'store_id'],
  56. table: ['t', 'tableid', 'table_id', 'tableno', 'table']
  57. };
  58. text.split('&').forEach((pair) => {
  59. const eq = pair.indexOf('=');
  60. if (eq > 0) {
  61. const k = pair.substring(0, eq).trim().toLowerCase();
  62. let v = pair.substring(eq + 1).trim();
  63. try {
  64. v = decodeURIComponent(v);
  65. } catch (_) {}
  66. v = trim(v);
  67. if (keys.store.includes(k)) out.storeId = v;
  68. if (keys.table.includes(k)) out.tableId = v;
  69. }
  70. });
  71. return out;
  72. };
  73. try {
  74. // 1. 若为 URL 或含 ?,提取 query 或 scene
  75. const qIdx = s.indexOf('?');
  76. const hIdx = s.indexOf('#');
  77. if (qIdx >= 0) {
  78. const queryPart = hIdx >= 0 ? s.substring(qIdx + 1, hIdx) : s.substring(qIdx + 1);
  79. const params = new Map();
  80. queryPart.split('&').forEach((pair) => {
  81. const eq = pair.indexOf('=');
  82. if (eq > 0) {
  83. const k = decodeURIComponent(pair.substring(0, eq).trim());
  84. const v = decodeURIComponent(pair.substring(eq + 1).trim());
  85. params.set(k, v);
  86. }
  87. });
  88. const scene = params.get('scene') || params.get('Scene');
  89. if (scene) {
  90. const decoded = decodeURIComponent(scene);
  91. if (decoded.startsWith('{') && decoded.endsWith('}')) {
  92. const obj = JSON.parse(decoded);
  93. return {
  94. storeId: trim(obj?.storeId ?? obj?.store_id ?? obj?.s ?? ''),
  95. tableId: trim(obj?.tableId ?? obj?.table_id ?? obj?.tableid ?? obj?.t ?? obj?.tableNo ?? obj?.table ?? '')
  96. };
  97. }
  98. const fromKv = parseKv(decoded);
  99. // scene 仅为数字时,视为桌号
  100. if (!fromKv.tableId && /^\d+$/.test(trim(decoded))) {
  101. fromKv.tableId = trim(decoded);
  102. }
  103. return fromKv;
  104. }
  105. const fromQuery = parseKv(queryPart);
  106. return fromQuery;
  107. }
  108. // 2. 尝试 JSON 格式
  109. if ((s.startsWith('{') && s.endsWith('}')) || (s.startsWith('[') && s.endsWith(']'))) {
  110. const obj = JSON.parse(s);
  111. storeId = trim(obj?.storeId ?? obj?.store_id ?? obj?.s ?? '');
  112. tableId = trim(obj?.tableId ?? obj?.table_id ?? obj?.tableid ?? obj?.t ?? obj?.tableNo ?? obj?.table ?? '');
  113. return { storeId, tableId };
  114. }
  115. // 3. key=value 格式
  116. return parseKv(s);
  117. } catch (err) {
  118. console.warn('parseScanResult', err);
  119. }
  120. return { storeId, tableId };
  121. }
  122. // 点击扫码点餐:未登录提示请登录;已登录扫码后跳转点餐页
  123. function handleScanOrder() {
  124. const userStore = useUserStore();
  125. const token = userStore.getToken || uni.getStorageSync(TOKEN) || '';
  126. if (!token) {
  127. uni.showToast({ title: '请登录', icon: 'none' });
  128. return;
  129. }
  130. uni.scanCode({
  131. scanType: ['qrCode'],
  132. success: (res) => {
  133. // 普通二维码用 result;小程序码用 path(含 scene)
  134. const result = (res?.path || res?.result || '').trim();
  135. const { storeId, tableId } = parseScanResult(result);
  136. if (!tableId) {
  137. console.warn('[扫码] 未解析到桌号,原始内容:', result, '解析结果:', { storeId, tableId });
  138. }
  139. const payload = { raw: result, storeId, tableId };
  140. uni.setStorageSync(SCAN_QR_CACHE, JSON.stringify(payload));
  141. if (storeId) uni.setStorageSync('currentStoreId', storeId);
  142. if (tableId) uni.setStorageSync('currentTableId', tableId);
  143. const diners = uni.getStorageSync('currentDiners') || '1';
  144. uni.setStorageSync('currentDiners', diners);
  145. uni.reLaunch({
  146. url: `/pages/orderFood/index?tableid=${encodeURIComponent(tableId || '')}&diners=${encodeURIComponent(diners)}`
  147. });
  148. },
  149. fail: (err) => {
  150. if (err?.errMsg && !String(err.errMsg).includes('cancel')) {
  151. uni.showToast({ title: '扫码失败', icon: 'none' });
  152. }
  153. }
  154. });
  155. }
  156. // 底部 tabBar 跳转(需在 pages.json 中配置 tabBar.list)
  157. function go(url) {
  158. if (url === unref(getPath)) return;
  159. uni.switchTab({ url });
  160. }
  161. const getPath = computed(() => {
  162. const pages = getCurrentPages(); // 获取路由栈
  163. console.log('/' + pages[pages.length - 1].route);
  164. return '/' + pages[pages.length - 1].route;
  165. });
  166. </script>
  167. <style lang="scss" scoped>
  168. .placeholder {
  169. height: 140rpx;
  170. box-sizing: content-box;
  171. }
  172. .tab-bar-wrap {
  173. position: fixed;
  174. left: 0;
  175. right: 0;
  176. bottom: 0;
  177. z-index: 99;
  178. background-color: #ffffff;
  179. box-shadow: 0 -2rpx 16rpx 0 rgba(0, 0, 0, 0.08);
  180. .tab-bar {
  181. // padding: 16rpx 0;
  182. // padding-bottom: calc(16rpx + env(safe-area-inset-bottom));
  183. display: flex;
  184. align-items: flex-end;
  185. justify-content: space-around;
  186. position: relative;
  187. padding-top: 20rpx;
  188. .menu {
  189. display: flex;
  190. flex-direction: column;
  191. align-items: center;
  192. justify-content: center;
  193. flex: 1;
  194. padding: 0 40rpx;
  195. padding-bottom: 0;
  196. transition: all 0.3s ease;
  197. image {
  198. width: 48rpx;
  199. height: 48rpx;
  200. transition: transform 0.3s ease;
  201. }
  202. .text {
  203. font-size: 22rpx;
  204. color: #999999;
  205. margin-top: 8rpx;
  206. transition: all 0.3s ease;
  207. font-weight: 400;
  208. }
  209. .text.sele {
  210. color: #333333;
  211. font-weight: 500;
  212. }
  213. &:active {
  214. opacity: 0.7;
  215. transform: scale(0.95);
  216. }
  217. }
  218. .menu-center {
  219. display: flex;
  220. flex-direction: column;
  221. align-items: center;
  222. justify-content: center;
  223. position: absolute;
  224. bottom: -20rpx;
  225. margin: 0 40rpx;
  226. // transform: translateY(-35rpx);
  227. transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  228. background-color: #fff;
  229. border-radius: 50%;
  230. padding: 20rpx;
  231. .scan-btn {
  232. width: 110rpx;
  233. height: 110rpx;
  234. border-radius: 50%;
  235. display: flex;
  236. align-items: center;
  237. justify-content: center;
  238. margin-bottom: 6rpx;
  239. position: relative;
  240. transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  241. background: linear-gradient(35deg, #FCB73F 0%, #FC733D 100%);
  242. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.16);
  243. image {
  244. width: 42rpx;
  245. height: 42rpx;
  246. filter: brightness(0) invert(1);
  247. transition: transform 0.3s ease;
  248. margin-bottom: 20rpx;
  249. }
  250. }
  251. .qrT-img {
  252. width: 85rpx;
  253. height: 22rpx;
  254. position: absolute;
  255. bottom: 36rpx;
  256. left: 50%;
  257. transform: translateX(-50%);
  258. }
  259. .text-center {
  260. font-size: 22rpx;
  261. color: #ff8844;
  262. margin-top: 4rpx;
  263. font-weight: 500;
  264. transition: all 0.3s ease;
  265. }
  266. &:active {
  267. transform: translateY(-35rpx) scale(0.92);
  268. .scan-btn {
  269. box-shadow: 0 4rpx 16rpx 0 rgba(255, 136, 68, 0.3),
  270. 0 1rpx 4rpx 0 rgba(255, 136, 68, 0.15);
  271. image {
  272. transform: scale(0.9);
  273. }
  274. }
  275. }
  276. }
  277. }
  278. }
  279. </style>