| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- /**
- * 供应商询价页「暂存价格」:按 userId 分桶,避免多账号同机串数据。
- * 结构:{ [userId]: { [inquiryId]: { [productId]: number } } }
- */
- export const SUPPLIER_INQUIRY_TEMP_PRICES_KEY = 'supplier_inquiry_temp_prices';
- /**
- * @param {string | undefined | null} userId
- * @param {string} inquiryKey
- * @returns {Record<string, number> | null}
- */
- export function readInquiryTempMapForUser(userId, inquiryKey) {
- const key = (inquiryKey && String(inquiryKey).trim()) || '_default';
- const uid = userId == null || String(userId).trim() === '' ? '' : String(userId).trim();
- if (!uid) return null;
- const root = uni.getStorageSync(SUPPLIER_INQUIRY_TEMP_PRICES_KEY) || {};
- if (!root || typeof root !== 'object') return null;
- const byUser = root[uid];
- if (!byUser || typeof byUser !== 'object') return null;
- const m = byUser[key];
- if (!m || typeof m !== 'object') return null;
- return m;
- }
- /**
- * @param {string | undefined | null} userId
- * @param {string} inquiryKey
- * @param {string} productId
- * @param {string|number|undefined|null} rawPrice 空或无效则删除该条暂存
- */
- export function writeOneTempPriceForUser(userId, inquiryKey, productId, rawPrice) {
- const key = (inquiryKey && String(inquiryKey).trim()) || '_default';
- const uid = userId == null || String(userId).trim() === '' ? '' : String(userId).trim();
- if (!uid || !productId) return;
- const root = { ...(uni.getStorageSync(SUPPLIER_INQUIRY_TEMP_PRICES_KEY) || {}) };
- if (!root[uid] || typeof root[uid] !== 'object') {
- root[uid] = {};
- }
- else {
- root[uid] = { ...root[uid] };
- }
- const byUser = root[uid];
- const block = (byUser[key] && typeof byUser[key] === 'object') ? { ...byUser[key] } : {};
- if (rawPrice === '' || rawPrice == null) {
- if (Object.prototype.hasOwnProperty.call(block, productId)) {
- delete block[productId];
- }
- }
- else {
- const num = Number(rawPrice);
- if (Number.isNaN(num) || num < 0) return;
- block[productId] = Number(num.toFixed(2));
- }
- if (Object.keys(block).length === 0) {
- delete byUser[key];
- }
- else {
- byUser[key] = block;
- }
- if (Object.keys(byUser).length === 0) {
- delete root[uid];
- }
- uni.setStorageSync(SUPPLIER_INQUIRY_TEMP_PRICES_KEY, root);
- }
- /**
- * 与「暂存价格」按钮写一整块 map 的语义(商品详情页)
- * @param {string | undefined | null} userId
- * @param {string} inquiryKey
- * @param {string} productId
- * @param {number} price
- */
- export function mergeTempProductPriceForUser(userId, inquiryKey, productId, price) {
- const key = (inquiryKey && String(inquiryKey).trim()) || '_default';
- const uid = userId == null || String(userId).trim() === '' ? '' : String(userId).trim();
- if (!uid || !productId) return;
- const root = { ...(uni.getStorageSync(SUPPLIER_INQUIRY_TEMP_PRICES_KEY) || {}) };
- if (!root[uid] || typeof root[uid] !== 'object') {
- root[uid] = {};
- }
- else {
- root[uid] = { ...root[uid] };
- }
- const byUser = root[uid];
- byUser[key] = {
- ...(byUser[key] && typeof byUser[key] === 'object' ? byUser[key] : {}),
- [productId]: price
- };
- uni.setStorageSync(SUPPLIER_INQUIRY_TEMP_PRICES_KEY, root);
- }
|