m2GenericApiPath.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { USE_M2_GENERIC_PRICING_API } from '@/settings/enums.js';
  2. /** 与 qrScene 一致:m=2 为通用价目扫码 */
  3. export function isGenericPricingM2(m) {
  4. const s = String(m ?? '').trim();
  5. if (s === '2') return true;
  6. const n = Number(s);
  7. return !Number.isNaN(n) && n === 2;
  8. }
  9. export function syncM2GenericPricingStorage(m) {
  10. try {
  11. uni.setStorageSync(USE_M2_GENERIC_PRICING_API, isGenericPricingM2(m) ? '1' : '0');
  12. } catch (_) {}
  13. }
  14. export function isM2GenericPricingApiActive() {
  15. try {
  16. return uni.getStorageSync(USE_M2_GENERIC_PRICING_API) === '1';
  17. } catch (_) {
  18. return false;
  19. }
  20. }
  21. /**
  22. * 仅改写文档 4.1 点餐域、4.2 购物车/下单/SSE;其余 URL 不变。
  23. * @param {string} fullUrl path 可含 query
  24. */
  25. export function rewriteM2DocumentedApiPath(fullUrl) {
  26. if (!fullUrl || typeof fullUrl !== 'string' || !isM2GenericPricingApiActive()) return fullUrl;
  27. const qIndex = fullUrl.indexOf('?');
  28. const path = qIndex >= 0 ? fullUrl.slice(0, qIndex) : fullUrl;
  29. const query = qIndex >= 0 ? fullUrl.slice(qIndex) : '';
  30. // 4.2 订单域:/store/order/cart*、create、sse
  31. if (path.startsWith('/store/order/cart')) {
  32. return path.replace(/^\/store\/order\/cart/, '/store/generic-order/cart') + query;
  33. }
  34. if (path === '/store/order/create') {
  35. return '/store/generic-order/create' + query;
  36. }
  37. if (path.startsWith('/store/order/sse/')) {
  38. return path.replace(/^\/store\/order\/sse\//, '/store/generic-order/sse/') + query;
  39. }
  40. // 4.1 点餐域:白名单路径 /store/dining/ -> /store/generic-dining/
  41. if (!path.startsWith('/store/dining/')) return fullUrl;
  42. const rest = path.slice('/store/dining/'.length);
  43. const allowed =
  44. rest === 'page-info' ||
  45. rest === 'search' ||
  46. rest === 'cuisines' ||
  47. rest.startsWith('cuisine/') ||
  48. rest === 'order/confirm' ||
  49. rest === 'coupons/available' ||
  50. rest === 'coupon/receive' ||
  51. rest === 'order/lock' ||
  52. rest === 'order/unlock' ||
  53. rest === 'order/check-lock' ||
  54. rest === 'order/settlement' ||
  55. rest === 'order/settlement/lock' ||
  56. rest === 'order/settlement/unlock';
  57. if (!allowed) return fullUrl;
  58. return `/store/generic-dining/${rest}${query}`;
  59. }