index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. const userStore = useUserStore();
  13. /** 从 scene 解析 storeId、tableId(与 App.vue parseSceneToStoreTable 逻辑一致) */
  14. function parseSceneToStoreTable(sceneStr) {
  15. const trim = (v) => (v == null ? '' : String(v).trim());
  16. let storeId = '';
  17. let tableId = '';
  18. if (!sceneStr) return { storeId, tableId };
  19. let decoded = String(sceneStr).trim();
  20. try { decoded = decodeURIComponent(decoded); } catch (_) {}
  21. const parseKv = (text) => {
  22. text.split('&').forEach((pair) => {
  23. const eq = pair.indexOf('=');
  24. if (eq > 0) {
  25. const k = pair.substring(0, eq).trim().toLowerCase();
  26. const v = pair.substring(eq + 1).trim();
  27. if (['s', 'storeid', 'store_id'].includes(k)) storeId = v;
  28. if (['t', 'tableid', 'table_id', 'tableno', 'table'].includes(k)) tableId = v;
  29. }
  30. });
  31. };
  32. try {
  33. if (decoded.startsWith('{') && decoded.endsWith('}')) {
  34. const obj = JSON.parse(decoded);
  35. return { storeId: trim(obj?.storeId ?? obj?.store_id ?? obj?.s ?? ''), tableId: trim(obj?.tableId ?? obj?.table_id ?? obj?.tableid ?? obj?.t ?? obj?.tableNo ?? obj?.table ?? '') };
  36. }
  37. if (/^\d+_\d+$/.test(decoded)) {
  38. const [s, t] = decoded.split('_');
  39. return { storeId: trim(s), tableId: trim(t) };
  40. }
  41. if (/^\d+$/.test(decoded)) return { storeId: '', tableId: decoded };
  42. parseKv(decoded);
  43. } catch (_) {}
  44. return { storeId: trim(storeId), tableId: trim(tableId) };
  45. }
  46. /** 从启动参数/query 中解析 tableid、storeId,兼容微信小程序 scene、query 等 */
  47. function getIdsFromOptions(options) {
  48. if (!options || typeof options !== 'object') return { tableid: '', storeId: '' };
  49. const q = options.query || options;
  50. const directTable = q.tableId ?? q.tableid ?? q.table_id ?? q.t ?? '';
  51. if (directTable) {
  52. const directStore = q.storeId ?? q.storeid ?? q.s ?? '';
  53. return { tableid: String(directTable).trim(), storeId: String(directStore || '').trim() };
  54. }
  55. const scene = q.scene ?? q.q ?? '';
  56. if (scene) {
  57. const { storeId: s, tableId: t } = parseSceneToStoreTable(scene);
  58. return { tableid: t, storeId: s };
  59. }
  60. return { tableid: '', storeId: '' };
  61. }
  62. async function doRedirect(options = {}) {
  63. let tableid = uni.getStorageSync('currentTableId') || '';
  64. if (!tableid) {
  65. const { tableid: t, storeId: s } = getIdsFromOptions(options);
  66. tableid = t;
  67. if (tableid) uni.setStorageSync('currentTableId', tableid);
  68. if (s) uni.setStorageSync('currentStoreId', s);
  69. }
  70. if (!tableid) {
  71. console.log('[launch] 无 tableid,跳转首页');
  72. uni.reLaunch({ url: '/pages/index/index' });
  73. return;
  74. }
  75. try {
  76. console.log('[launch] 调用 GetTableDiningStatus, tableid:', tableid);
  77. const res = await diningApi.GetTableDiningStatus(tableid);
  78. const raw = (res && typeof res === 'object') ? res : {};
  79. // 兼容多种返回:{ inDining: true }、{ data: { inDining: true } }、直接返回 true
  80. const inDining =
  81. res === true ||
  82. res === 'true' ||
  83. raw?.inDining === true ||
  84. raw?.inDining === 'true' ||
  85. raw?.data?.inDining === true ||
  86. raw?.data?.inDining === 'true';
  87. const token = userStore.getToken || uni.getStorageSync(TOKEN) || '';
  88. const dinerCount =
  89. raw?.dinerCount ?? raw?.diner ?? raw?.data?.dinerCount ?? raw?.data?.diner ?? uni.getStorageSync('currentDiners') ?? 1;
  90. if (inDining) {
  91. uni.setStorageSync('currentDiners', dinerCount);
  92. uni.reLaunch({
  93. url: `/pages/orderFood/index?tableid=${encodeURIComponent(tableid)}&diners=${encodeURIComponent(dinerCount)}`
  94. });
  95. }
  96. else{
  97. uni.reLaunch({
  98. url: `/pages/numberOfDiners/index?inDining=1&tableid=${encodeURIComponent(tableid)}&diners=${encodeURIComponent(dinerCount)}`
  99. });
  100. }
  101. } catch (err) {
  102. console.warn('查询桌位就餐状态失败,进入选择人数页', err);
  103. uni.reLaunch({ url: '/pages/numberOfDiners/index' });
  104. }
  105. }
  106. onLoad((options) => {
  107. doRedirect(options);
  108. });
  109. </script>
  110. <style scoped lang="scss">
  111. .launch-wrap {
  112. min-height: 100vh;
  113. display: flex;
  114. align-items: center;
  115. justify-content: center;
  116. background: #f7f9fa;
  117. }
  118. .launch-loading {
  119. font-size: 28rpx;
  120. color: #999;
  121. }
  122. </style>