// 本地存储键名 export const STATION_STORAGE_KEY = "wms_selected_station"; /** * 站台管理器 - 提供站台信息的持久化存储和获取 */ export const stationManager = { /** * 保存站台信息到本地存储 * @param {string} station - 站台值 */ saveStation(station) { try { // 使用localStorage进行持久化存储 localStorage.setItem(STATION_STORAGE_KEY, station); // 同时保存到sessionStorage,作为备用 sessionStorage.setItem(STATION_STORAGE_KEY, station); // 触发自定义事件,通知其他组件站台已更新 window.dispatchEvent(new CustomEvent('station-changed', { detail: { station } })); console.log(`站台已保存: ${station}`); } catch (error) { console.error("保存站台信息失败:", error); } }, /** * 获取保存的站台信息 * @returns {string|null} 站台值或null */ getStation() { try { // 优先从localStorage获取 let station = localStorage.getItem(STATION_STORAGE_KEY); // 如果localStorage没有,尝试从sessionStorage获取 if (!station) { station = sessionStorage.getItem(STATION_STORAGE_KEY); } return station; } catch (error) { console.error("获取站台信息失败:", error); return null; } }, /** * 清除站台信息 */ clearStation() { try { localStorage.removeItem(STATION_STORAGE_KEY); sessionStorage.removeItem(STATION_STORAGE_KEY); console.log("站台信息已清除"); } catch (error) { console.error("清除站台信息失败:", error); } }, /** * 监听站台变化 * @param {Function} callback - 变化回调函数 * @returns {Function} 取消监听的函数 */ onStationChange(callback) { const handler = (event) => { if (callback && typeof callback === 'function') { callback(event.detail.station); } }; window.addEventListener('station-changed', handler); // 返回取消监听的函数 return () => { window.removeEventListener('station-changed', handler); }; }, /** * 获取站台显示标签 * @param {string} value - 站台值 * @param {Array} options - 站台选项数组 * @returns {string} 站台标签 */ getStationLabel(value, options) { if (!value || !options) return value || ''; const option = options.find(opt => opt.value === value); return option ? option.label : value; } }; // 默认导出 export default stationManager;