TabBar.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. // scanCode 真机需相机权限,先申请再扫码
  131. uni.authorize({
  132. scope: 'scope.camera',
  133. success: () => runScan(),
  134. fail: () => {
  135. uni.showModal({
  136. title: '提示',
  137. content: '需要相机权限才能扫描桌号二维码,请在设置中开启',
  138. confirmText: '去设置',
  139. success: (res) => {
  140. if (res.confirm) {
  141. uni.openSetting({
  142. success: (settingRes) => {
  143. if (settingRes?.authSetting?.['scope.camera']) runScan();
  144. }
  145. });
  146. }
  147. }
  148. });
  149. }
  150. });
  151. function runScan() {
  152. uni.scanCode({
  153. scanType: ['qrCode', 'barCode'],
  154. success: (res) => {
  155. const result = (res?.path || res?.result || '').trim();
  156. const { storeId, tableId } = parseScanResult(result);
  157. const payload = { raw: result, storeId, tableId };
  158. uni.setStorageSync(SCAN_QR_CACHE, JSON.stringify(payload));
  159. if (storeId) uni.setStorageSync('currentStoreId', storeId);
  160. if (tableId) uni.setStorageSync('currentTableId', tableId);
  161. const diners = uni.getStorageSync('currentDiners') || '1';
  162. uni.setStorageSync('currentDiners', diners);
  163. if (!tableId) {
  164. uni.showToast({ title: '未识别到桌号,请扫描正确的桌号二维码', icon: 'none' });
  165. console.warn('[扫码] 未解析到桌号,原始内容:', result);
  166. return;
  167. }
  168. uni.reLaunch({
  169. url: `/pages/orderFood/index?tableid=${encodeURIComponent(tableId)}&diners=${encodeURIComponent(diners)}`
  170. });
  171. },
  172. fail: (err) => {
  173. if (err?.errMsg && !String(err.errMsg).includes('cancel')) {
  174. uni.showToast({ title: err?.errMsg || '扫码失败', icon: 'none' });
  175. }
  176. }
  177. });
  178. }
  179. }
  180. // 底部 tabBar 跳转(需在 pages.json 中配置 tabBar.list)
  181. function go(url) {
  182. if (url === unref(getPath)) return;
  183. uni.switchTab({ url });
  184. }
  185. const getPath = computed(() => {
  186. const pages = getCurrentPages(); // 获取路由栈
  187. console.log('/' + pages[pages.length - 1].route);
  188. return '/' + pages[pages.length - 1].route;
  189. });
  190. </script>
  191. <style lang="scss" scoped>
  192. .placeholder {
  193. height: 140rpx;
  194. box-sizing: content-box;
  195. }
  196. .tab-bar-wrap {
  197. position: fixed;
  198. left: 0;
  199. right: 0;
  200. bottom: 0;
  201. z-index: 99;
  202. background-color: #ffffff;
  203. box-shadow: 0 -2rpx 16rpx 0 rgba(0, 0, 0, 0.08);
  204. .tab-bar {
  205. // padding: 16rpx 0;
  206. // padding-bottom: calc(16rpx + env(safe-area-inset-bottom));
  207. display: flex;
  208. align-items: flex-end;
  209. justify-content: space-around;
  210. position: relative;
  211. padding-top: 20rpx;
  212. .menu {
  213. display: flex;
  214. flex-direction: column;
  215. align-items: center;
  216. justify-content: center;
  217. flex: 1;
  218. padding: 0 40rpx;
  219. padding-bottom: 0;
  220. transition: all 0.3s ease;
  221. image {
  222. width: 48rpx;
  223. height: 48rpx;
  224. transition: transform 0.3s ease;
  225. }
  226. .text {
  227. font-size: 22rpx;
  228. color: #999999;
  229. margin-top: 8rpx;
  230. transition: all 0.3s ease;
  231. font-weight: 400;
  232. }
  233. .text.sele {
  234. color: #333333;
  235. font-weight: 500;
  236. }
  237. &:active {
  238. opacity: 0.7;
  239. transform: scale(0.95);
  240. }
  241. }
  242. .menu-center {
  243. display: flex;
  244. flex-direction: column;
  245. align-items: center;
  246. justify-content: center;
  247. position: absolute;
  248. bottom: -20rpx;
  249. margin: 0 40rpx;
  250. // transform: translateY(-35rpx);
  251. transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  252. background-color: #fff;
  253. border-radius: 50%;
  254. padding: 20rpx;
  255. .scan-btn {
  256. width: 110rpx;
  257. height: 110rpx;
  258. border-radius: 50%;
  259. display: flex;
  260. align-items: center;
  261. justify-content: center;
  262. margin-bottom: 6rpx;
  263. position: relative;
  264. transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  265. background: linear-gradient(35deg, #FCB73F 0%, #FC733D 100%);
  266. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.16);
  267. image {
  268. width: 42rpx;
  269. height: 42rpx;
  270. filter: brightness(0) invert(1);
  271. transition: transform 0.3s ease;
  272. margin-bottom: 20rpx;
  273. }
  274. }
  275. .qrT-img {
  276. width: 85rpx;
  277. height: 22rpx;
  278. position: absolute;
  279. bottom: 36rpx;
  280. left: 50%;
  281. transform: translateX(-50%);
  282. }
  283. .text-center {
  284. font-size: 22rpx;
  285. color: #ff8844;
  286. margin-top: 4rpx;
  287. font-weight: 500;
  288. transition: all 0.3s ease;
  289. }
  290. &:active {
  291. transform: translateY(-35rpx) scale(0.92);
  292. .scan-btn {
  293. box-shadow: 0 4rpx 16rpx 0 rgba(255, 136, 68, 0.3),
  294. 0 1rpx 4rpx 0 rgba(255, 136, 68, 0.15);
  295. image {
  296. transform: scale(0.9);
  297. }
  298. }
  299. }
  300. }
  301. }
  302. }
  303. </style>