tableDiningLaunch.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * 扫码/太阳码进入后:调桌位就餐状态并 reLaunch 到点餐或选人数页。
  3. * 供 launch、首页(太阳码直达首页)、TabBar 等共用。
  4. */
  5. import * as diningApi from '@/api/dining.js';
  6. import { parseSceneToStoreTable, isScanEntryAllowed } from '@/utils/qrScene.js';
  7. function trim(v) {
  8. return v == null ? '' : String(v).trim();
  9. }
  10. function tryDecodeURIComponent(s) {
  11. if (s == null || s === '') return '';
  12. try {
  13. return decodeURIComponent(String(s).trim());
  14. } catch (_) {
  15. return String(s).trim();
  16. }
  17. }
  18. /**
  19. * 从页面 onLoad 参数解析 tableId、storeId、m(与 pages/launch 一致;兼容 query 嵌套与平铺)
  20. */
  21. export function getScanEntryIdsFromOptions(options) {
  22. if (!options || typeof options !== 'object') return { tableid: '', storeId: '', m: '' };
  23. const flat = { ...options, ...(options.query && typeof options.query === 'object' ? options.query : {}) };
  24. let m = trim(flat.m ?? flat.mode ?? '');
  25. const directTable =
  26. flat.tableId ?? flat.tableid ?? flat.table_id ?? flat.t ?? '';
  27. if (directTable) {
  28. const directStore = flat.storeId ?? flat.storeid ?? flat.s ?? '';
  29. return { tableid: String(directTable).trim(), storeId: String(directStore || '').trim(), m };
  30. }
  31. const sceneEnc = flat.scene ?? flat.q ?? '';
  32. const scene = sceneEnc ? tryDecodeURIComponent(sceneEnc) : '';
  33. if (scene) {
  34. const { storeId: s, tableId: t, m: sm } = parseSceneToStoreTable(scene);
  35. return { tableid: trim(t), storeId: trim(s), m: m || trim(sm) || '' };
  36. }
  37. return { tableid: '', storeId: '', m };
  38. }
  39. /**
  40. * 查询 /store/dining/table-dining-status 后跳转(与 launch 页逻辑一致)
  41. * @param {string} tableidResolved 桌台主键 id
  42. */
  43. export async function runTableDiningStatusAndRedirect(tableidResolved) {
  44. const tid = trim(tableidResolved);
  45. if (!tid) {
  46. uni.reLaunch({ url: '/pages/index/index' });
  47. return;
  48. }
  49. try {
  50. const res = await diningApi.GetTableDiningStatus(tid);
  51. const raw = res && typeof res === 'object' ? res : {};
  52. const inDining =
  53. res === true ||
  54. res === 'true' ||
  55. raw?.inDining === true ||
  56. raw?.inDining === 'true' ||
  57. raw?.data?.inDining === true ||
  58. raw?.data?.inDining === 'true';
  59. const dinerCount =
  60. raw?.dinerCount ??
  61. raw?.diner ??
  62. raw?.data?.dinerCount ??
  63. raw?.data?.diner ??
  64. uni.getStorageSync('currentDiners') ??
  65. 1;
  66. const dinersStr = String(dinerCount);
  67. if (inDining) {
  68. uni.setStorageSync('currentDiners', dinersStr);
  69. uni.reLaunch({
  70. url: `/pages/orderFood/index?tableid=${encodeURIComponent(tid)}&diners=${encodeURIComponent(dinersStr)}`
  71. });
  72. } else {
  73. uni.reLaunch({
  74. url: `/pages/numberOfDiners/index?inDining=1&tableid=${encodeURIComponent(tid)}&diners=${encodeURIComponent(dinersStr)}`
  75. });
  76. }
  77. } catch (err) {
  78. console.warn('[tableDiningLaunch] 查询桌位就餐状态失败,进入选择人数页', err);
  79. const fallbackDiners = uni.getStorageSync('currentDiners') || '1';
  80. uni.reLaunch({
  81. url: `/pages/numberOfDiners/index?inDining=1&tableid=${encodeURIComponent(tid)}&diners=${encodeURIComponent(fallbackDiners)}`
  82. });
  83. }
  84. }