supplierInquiryTempPrice.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * 供应商询价页「暂存价格」:按 userId 分桶,避免多账号同机串数据。
  3. * 结构:{ [userId]: { [inquiryId]: { [productId]: number } } }
  4. */
  5. export const SUPPLIER_INQUIRY_TEMP_PRICES_KEY = 'supplier_inquiry_temp_prices';
  6. /**
  7. * @param {string | undefined | null} userId
  8. * @param {string} inquiryKey
  9. * @returns {Record<string, number> | null}
  10. */
  11. export function readInquiryTempMapForUser(userId, inquiryKey) {
  12. const key = (inquiryKey && String(inquiryKey).trim()) || '_default';
  13. const uid = userId == null || String(userId).trim() === '' ? '' : String(userId).trim();
  14. if (!uid) return null;
  15. const root = uni.getStorageSync(SUPPLIER_INQUIRY_TEMP_PRICES_KEY) || {};
  16. if (!root || typeof root !== 'object') return null;
  17. const byUser = root[uid];
  18. if (!byUser || typeof byUser !== 'object') return null;
  19. const m = byUser[key];
  20. if (!m || typeof m !== 'object') return null;
  21. return m;
  22. }
  23. /**
  24. * @param {string | undefined | null} userId
  25. * @param {string} inquiryKey
  26. * @param {string} productId
  27. * @param {string|number|undefined|null} rawPrice 空或无效则删除该条暂存
  28. */
  29. export function writeOneTempPriceForUser(userId, inquiryKey, productId, rawPrice) {
  30. const key = (inquiryKey && String(inquiryKey).trim()) || '_default';
  31. const uid = userId == null || String(userId).trim() === '' ? '' : String(userId).trim();
  32. if (!uid || !productId) return;
  33. const root = { ...(uni.getStorageSync(SUPPLIER_INQUIRY_TEMP_PRICES_KEY) || {}) };
  34. if (!root[uid] || typeof root[uid] !== 'object') {
  35. root[uid] = {};
  36. }
  37. else {
  38. root[uid] = { ...root[uid] };
  39. }
  40. const byUser = root[uid];
  41. const block = (byUser[key] && typeof byUser[key] === 'object') ? { ...byUser[key] } : {};
  42. if (rawPrice === '' || rawPrice == null) {
  43. if (Object.prototype.hasOwnProperty.call(block, productId)) {
  44. delete block[productId];
  45. }
  46. }
  47. else {
  48. const num = Number(rawPrice);
  49. if (Number.isNaN(num) || num < 0) return;
  50. block[productId] = Number(num.toFixed(2));
  51. }
  52. if (Object.keys(block).length === 0) {
  53. delete byUser[key];
  54. }
  55. else {
  56. byUser[key] = block;
  57. }
  58. if (Object.keys(byUser).length === 0) {
  59. delete root[uid];
  60. }
  61. uni.setStorageSync(SUPPLIER_INQUIRY_TEMP_PRICES_KEY, root);
  62. }
  63. /**
  64. * 与「暂存价格」按钮写一整块 map 的语义(商品详情页)
  65. * @param {string | undefined | null} userId
  66. * @param {string} inquiryKey
  67. * @param {string} productId
  68. * @param {number} price
  69. */
  70. export function mergeTempProductPriceForUser(userId, inquiryKey, productId, price) {
  71. const key = (inquiryKey && String(inquiryKey).trim()) || '_default';
  72. const uid = userId == null || String(userId).trim() === '' ? '' : String(userId).trim();
  73. if (!uid || !productId) return;
  74. const root = { ...(uni.getStorageSync(SUPPLIER_INQUIRY_TEMP_PRICES_KEY) || {}) };
  75. if (!root[uid] || typeof root[uid] !== 'object') {
  76. root[uid] = {};
  77. }
  78. else {
  79. root[uid] = { ...root[uid] };
  80. }
  81. const byUser = root[uid];
  82. byUser[key] = {
  83. ...(byUser[key] && typeof byUser[key] === 'object' ? byUser[key] : {}),
  84. [productId]: price
  85. };
  86. uni.setStorageSync(SUPPLIER_INQUIRY_TEMP_PRICES_KEY, root);
  87. }