index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <!-- 启动页:根据 table-dining-status 接口结果跳转,避免先展示错误页面 -->
  3. <view class="launch-wrap">
  4. <view class="launch-loading">正在加载...</view>
  5. </view>
  6. </template>
  7. <script setup>
  8. import { onLoad } from '@dcloudio/uni-app';
  9. import * as diningApi from '@/api/dining.js';
  10. import { useUserStore } from '@/store/user.js';
  11. import { TOKEN } from '@/settings/enums.js';
  12. import { parseSceneToStoreTable, isScanEntryAllowed } from '@/utils/qrScene.js';
  13. import { SCAN_QR_CACHE } from '@/settings/enums.js';
  14. import { syncM2GenericPricingStorage } from '@/utils/m2GenericApiPath.js';
  15. const userStore = useUserStore();
  16. function trim(v) {
  17. return v == null ? '' : String(v).trim();
  18. }
  19. /** 从启动参数/query 中解析 tableid、storeId、m(m=1 美食,走 dining) */
  20. function getIdsFromOptions(options) {
  21. if (!options || typeof options !== 'object') return { tableid: '', storeId: '', m: '' };
  22. const q = options.query || options;
  23. let m = trim(q.m ?? q.mode ?? '');
  24. const directTable = q.tableId ?? q.tableid ?? q.table_id ?? q.t ?? '';
  25. if (directTable) {
  26. const directStore = q.storeId ?? q.storeid ?? q.s ?? '';
  27. return { tableid: String(directTable).trim(), storeId: String(directStore || '').trim(), m };
  28. }
  29. const scene = q.scene ?? q.q ?? '';
  30. if (scene) {
  31. const { storeId: s, tableId: t, m: sm } = parseSceneToStoreTable(scene);
  32. return { tableid: t, storeId: s, m: m || sm || '' };
  33. }
  34. return { tableid: '', storeId: '', m };
  35. }
  36. async function doRedirect(options = {}) {
  37. let { tableid, storeId: optStore, m } = getIdsFromOptions(options);
  38. if (trim(m) === '') {
  39. try {
  40. const c = uni.getStorageSync(SCAN_QR_CACHE);
  41. if (c) m = trim(JSON.parse(c).m ?? '');
  42. } catch (_) {}
  43. }
  44. syncM2GenericPricingStorage(m);
  45. if (!isScanEntryAllowed(m)) {
  46. uni.showToast({ title: '请扫描正确的点餐二维码', icon: 'none' });
  47. uni.reLaunch({ url: '/pages/index/index' });
  48. return;
  49. }
  50. let tableidFinal = uni.getStorageSync('currentTableId') || '';
  51. if (!tableidFinal) {
  52. tableidFinal = tableid;
  53. if (tableidFinal) uni.setStorageSync('currentTableId', tableidFinal);
  54. if (optStore) uni.setStorageSync('currentStoreId', optStore);
  55. }
  56. const tableidResolved = tableidFinal;
  57. if (!tableidResolved) {
  58. console.log('[launch] 无 tableid,跳转首页');
  59. uni.reLaunch({ url: '/pages/index/index' });
  60. return;
  61. }
  62. try {
  63. console.log('[launch] 调用 GetTableDiningStatus, tableid:', tableidResolved);
  64. const res = await diningApi.GetTableDiningStatus(tableidResolved);
  65. const raw = (res && typeof res === 'object') ? res : {};
  66. // 兼容多种返回:{ inDining: true }、{ data: { inDining: true } }、直接返回 true
  67. const inDining =
  68. res === true ||
  69. res === 'true' ||
  70. raw?.inDining === true ||
  71. raw?.inDining === 'true' ||
  72. raw?.data?.inDining === true ||
  73. raw?.data?.inDining === 'true';
  74. const token = userStore.getToken || uni.getStorageSync(TOKEN) || '';
  75. const dinerCount =
  76. raw?.dinerCount ?? raw?.diner ?? raw?.data?.dinerCount ?? raw?.data?.diner ?? uni.getStorageSync('currentDiners') ?? 1;
  77. if (inDining) {
  78. uni.setStorageSync('currentDiners', dinerCount);
  79. uni.reLaunch({
  80. url: `/pages/orderFood/index?tableid=${encodeURIComponent(tableidResolved)}&diners=${encodeURIComponent(dinerCount)}`
  81. });
  82. }
  83. else{
  84. uni.reLaunch({
  85. url: `/pages/numberOfDiners/index?inDining=1&tableid=${encodeURIComponent(tableidResolved)}&diners=${encodeURIComponent(dinerCount)}`
  86. });
  87. }
  88. } catch (err) {
  89. console.warn('查询桌位就餐状态失败,进入选择人数页', err);
  90. uni.reLaunch({ url: '/pages/numberOfDiners/index' });
  91. }
  92. }
  93. onLoad((options) => {
  94. doRedirect(options);
  95. });
  96. </script>
  97. <style scoped lang="scss">
  98. .launch-wrap {
  99. min-height: 100vh;
  100. display: flex;
  101. align-items: center;
  102. justify-content: center;
  103. background: #f7f9fa;
  104. }
  105. .launch-loading {
  106. font-size: 28rpx;
  107. color: #999;
  108. }
  109. </style>