TabBar.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. // 解析扫码内容:支持小程序码 path、scene,以及 s=店铺id&t=桌号id、URL、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. const parseScene = (sceneStr) => {
  74. let decoded = sceneStr;
  75. try {
  76. decoded = decodeURIComponent(decoded);
  77. } catch (_) {}
  78. decoded = trim(decoded);
  79. // scene 为 base64 编码的 JSON(小程序码常见)
  80. if (/^[A-Za-z0-9+/=]+$/.test(decoded) && decoded.length > 20) {
  81. try {
  82. const jsonStr = typeof atob === 'function' ? atob(decoded) : '';
  83. if (jsonStr && jsonStr.startsWith('{')) {
  84. const obj = JSON.parse(jsonStr);
  85. return {
  86. storeId: trim(obj?.storeId ?? obj?.store_id ?? obj?.s ?? ''),
  87. tableId: trim(obj?.tableId ?? obj?.table_id ?? obj?.tableid ?? obj?.t ?? obj?.tableNo ?? obj?.table ?? '')
  88. };
  89. }
  90. } catch (_) {}
  91. }
  92. // scene 为 JSON 字符串
  93. if (decoded.startsWith('{') && decoded.endsWith('}')) {
  94. try {
  95. const obj = JSON.parse(decoded);
  96. return {
  97. storeId: trim(obj?.storeId ?? obj?.store_id ?? obj?.s ?? ''),
  98. tableId: trim(obj?.tableId ?? obj?.table_id ?? obj?.tableid ?? obj?.t ?? obj?.tableNo ?? obj?.table ?? '')
  99. };
  100. } catch (_) {}
  101. }
  102. // scene 为 storeId_tableId 下划线分隔(常见小程序码格式)
  103. if (/^\d+_\d+$/.test(decoded)) {
  104. const [sid, tid] = decoded.split('_');
  105. return { storeId: trim(sid), tableId: trim(tid) };
  106. }
  107. // scene 仅为数字,视为桌号
  108. if (/^\d+$/.test(decoded)) {
  109. return { storeId: '', tableId: decoded };
  110. }
  111. const fromKv = parseKv(decoded);
  112. if (!fromKv.tableId && /^\d+$/.test(decoded)) fromKv.tableId = decoded;
  113. return fromKv;
  114. };
  115. try {
  116. // 1. 小程序码 path 格式:pages/xxx?scene=xxx
  117. const qIdx = s.indexOf('?');
  118. const hIdx = s.indexOf('#');
  119. if (qIdx >= 0) {
  120. const queryPart = hIdx >= 0 ? s.substring(qIdx + 1, hIdx) : s.substring(qIdx + 1);
  121. const params = new Map();
  122. queryPart.split('&').forEach((pair) => {
  123. const eq = pair.indexOf('=');
  124. if (eq > 0) {
  125. const k = pair.substring(0, eq).trim();
  126. let v = pair.substring(eq + 1).trim();
  127. try {
  128. v = decodeURIComponent(v);
  129. } catch (_) {}
  130. params.set(k, v);
  131. }
  132. });
  133. const scene = params.get('scene') || params.get('Scene');
  134. if (scene) {
  135. return parseScene(scene);
  136. }
  137. return parseKv(queryPart);
  138. }
  139. // 2. JSON 格式
  140. if ((s.startsWith('{') && s.endsWith('}')) || (s.startsWith('[') && s.endsWith(']'))) {
  141. const obj = JSON.parse(s);
  142. return {
  143. storeId: trim(obj?.storeId ?? obj?.store_id ?? obj?.s ?? ''),
  144. tableId: trim(obj?.tableId ?? obj?.table_id ?? obj?.tableid ?? obj?.t ?? obj?.tableNo ?? obj?.table ?? '')
  145. };
  146. }
  147. // 3. key=value 格式
  148. return parseKv(s);
  149. } catch (err) {
  150. console.warn('parseScanResult', err);
  151. }
  152. return { storeId, tableId };
  153. }
  154. // 点击扫码点餐:未登录提示请登录;已登录扫码后跳转点餐页
  155. function handleScanOrder() {
  156. const userStore = useUserStore();
  157. const token = userStore.getToken || uni.getStorageSync(TOKEN) || '';
  158. if (!token) {
  159. uni.showToast({ title: '请登录', icon: 'none' });
  160. return;
  161. }
  162. uni.scanCode({
  163. scanType: ['wxCode', 'qrCode', 'barCode'],
  164. success: (res) => {
  165. const result = (res?.path || res?.result || '').trim();
  166. const { storeId, tableId } = parseScanResult(result);
  167. const payload = { raw: result, storeId, tableId };
  168. uni.setStorageSync(SCAN_QR_CACHE, JSON.stringify(payload));
  169. if (storeId) uni.setStorageSync('currentStoreId', storeId);
  170. if (tableId) uni.setStorageSync('currentTableId', tableId);
  171. const diners = uni.getStorageSync('currentDiners') || '1';
  172. uni.setStorageSync('currentDiners', diners);
  173. if (!tableId) {
  174. uni.showToast({ title: '未识别到桌号,请扫描正确的桌号二维码', icon: 'none' });
  175. return;
  176. }
  177. uni.reLaunch({
  178. url: `/pages/orderFood/index?tableid=${encodeURIComponent(tableId)}&diners=${encodeURIComponent(diners)}`
  179. });
  180. },
  181. fail: (err) => {
  182. const msg = err?.errMsg || '';
  183. if (msg && !msg.includes('cancel')) {
  184. if (msg.includes('auth') || msg.includes('camera') || msg.includes('scope')) {
  185. uni.showModal({
  186. title: '提示',
  187. content: '需要相机权限才能扫码,请在设置中开启',
  188. confirmText: '去设置',
  189. success: (res) => {
  190. if (res.confirm) uni.openSetting();
  191. }
  192. });
  193. } else {
  194. uni.showToast({ title: msg || '扫码失败', icon: 'none' });
  195. }
  196. }
  197. }
  198. });
  199. }
  200. // 底部 tabBar 跳转(需在 pages.json 中配置 tabBar.list)
  201. function go(url) {
  202. if (url === unref(getPath)) return;
  203. uni.switchTab({ url });
  204. }
  205. const getPath = computed(() => {
  206. const pages = getCurrentPages(); // 获取路由栈
  207. console.log('/' + pages[pages.length - 1].route);
  208. return '/' + pages[pages.length - 1].route;
  209. });
  210. </script>
  211. <style lang="scss" scoped>
  212. .placeholder {
  213. height: 140rpx;
  214. box-sizing: content-box;
  215. }
  216. .tab-bar-wrap {
  217. position: fixed;
  218. left: 0;
  219. right: 0;
  220. bottom: 0;
  221. z-index: 99;
  222. background-color: #ffffff;
  223. box-shadow: 0 -2rpx 16rpx 0 rgba(0, 0, 0, 0.08);
  224. .tab-bar {
  225. // padding: 16rpx 0;
  226. // padding-bottom: calc(16rpx + env(safe-area-inset-bottom));
  227. display: flex;
  228. align-items: flex-end;
  229. justify-content: space-around;
  230. position: relative;
  231. padding-top: 20rpx;
  232. .menu {
  233. display: flex;
  234. flex-direction: column;
  235. align-items: center;
  236. justify-content: center;
  237. flex: 1;
  238. padding: 0 40rpx;
  239. padding-bottom: 0;
  240. transition: all 0.3s ease;
  241. image {
  242. width: 48rpx;
  243. height: 48rpx;
  244. transition: transform 0.3s ease;
  245. }
  246. .text {
  247. font-size: 22rpx;
  248. color: #999999;
  249. margin-top: 8rpx;
  250. transition: all 0.3s ease;
  251. font-weight: 400;
  252. }
  253. .text.sele {
  254. color: #333333;
  255. font-weight: 500;
  256. }
  257. &:active {
  258. opacity: 0.7;
  259. transform: scale(0.95);
  260. }
  261. }
  262. .menu-center {
  263. display: flex;
  264. flex-direction: column;
  265. align-items: center;
  266. justify-content: center;
  267. position: absolute;
  268. bottom: -20rpx;
  269. margin: 0 40rpx;
  270. // transform: translateY(-35rpx);
  271. transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  272. background-color: #fff;
  273. border-radius: 50%;
  274. padding: 20rpx;
  275. .scan-btn {
  276. width: 110rpx;
  277. height: 110rpx;
  278. border-radius: 50%;
  279. display: flex;
  280. align-items: center;
  281. justify-content: center;
  282. margin-bottom: 6rpx;
  283. position: relative;
  284. transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  285. background: linear-gradient(35deg, #FCB73F 0%, #FC733D 100%);
  286. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.16);
  287. image {
  288. width: 42rpx;
  289. height: 42rpx;
  290. filter: brightness(0) invert(1);
  291. transition: transform 0.3s ease;
  292. margin-bottom: 20rpx;
  293. }
  294. }
  295. .qrT-img {
  296. width: 85rpx;
  297. height: 22rpx;
  298. position: absolute;
  299. bottom: 36rpx;
  300. left: 50%;
  301. transform: translateX(-50%);
  302. }
  303. .text-center {
  304. font-size: 22rpx;
  305. color: #ff8844;
  306. margin-top: 4rpx;
  307. font-weight: 500;
  308. transition: all 0.3s ease;
  309. }
  310. &:active {
  311. transform: translateY(-35rpx) scale(0.92);
  312. .scan-btn {
  313. box-shadow: 0 4rpx 16rpx 0 rgba(255, 136, 68, 0.3),
  314. 0 1rpx 4rpx 0 rgba(255, 136, 68, 0.15);
  315. image {
  316. transform: scale(0.9);
  317. }
  318. }
  319. }
  320. }
  321. }
  322. }
  323. </style>