diff --git a/src/App.vue b/src/App.vue index fa09182..99990a5 100644 --- a/src/App.vue +++ b/src/App.vue @@ -8,54 +8,9 @@ import useSettingsStore from '@/store/modules/settings' import { handleThemeStyle - } from '@/utils/theme' - import { - listConvert - } from '@/api/system/convert.js'; + } from '@/utils/theme' const settingsStore = useSettingsStore() - - const unitData = ref([]); - const queryParams = ref({ - pageNum: 1, - pageSize: 1000, - unitType: null, - unitName: null, - baseUnit: null, - conversionFactor: null, - unitTypeName: null, - status: null, - unitOrder: null - }); - // 保留原有单位换算方法 - const groupByUnitType = (data) => { - return data.reduce((acc, unit) => { - const type = unit.unitType; - if (!acc[type]) acc[type] = []; - acc[type].push({ - id: unit.id, - unitType: unit.unitType, - unitName: unit.unitName, - conversionFactor: unit.conversionFactor, - unitOrder: unit.unitOrder, - baseUnit: unit.baseUnit, - status: unit.status, - unitTypeName: unit.unitTypeName - }); - return acc; - }, {}); - }; - const getList = async () => { - try { - const response = await listConvert(queryParams.value); - const unitDataGrouped = groupByUnitType(response.rows); - localStorage.setItem('unitData', JSON.stringify(unitDataGrouped)); - } catch (error) { - console.error('获取单位数据失败:', error); - } - }; - onMounted(() => { - getList(); nextTick(() => { settingsStore.initSetting(() => { // 初始化主题样式 diff --git a/src/store/modules/unitData.ts b/src/store/modules/unitData.ts new file mode 100644 index 0000000..e1a0ffe --- /dev/null +++ b/src/store/modules/unitData.ts @@ -0,0 +1,119 @@ +import { defineStore } from 'pinia' +import { listConvert } from '@/api/system/convert.js' + +// 定义计量单位Store +const useUnitStore = defineStore( + 'unit', // Store唯一标识 + { + state: () => ({ + // 分组后的单位数据(按unitType分组) + unitData: {}, + // 查询参数(与原逻辑一致) + queryParams: { + pageNum: 1, + pageSize: 1000, + unitType: null, + unitName: null, + baseUnit: null, + conversionFactor: null, + unitTypeName: null, + status: null, + unitOrder: null + }, + // 加载状态(扩展:可选) + loading: false + }), + actions: { + /** + * 数据分组核心方法(复用原有逻辑) + * @param {Array} data 原始单位列表数据 + * @returns {Object} 按unitType分组后的对象 + */ + groupByUnitType(data) { + return data.reduce((acc, unit) => { + const type = unit.unitType; + if (!acc[type]) acc[type] = []; + acc[type].push({ + id: unit.id, + unitType: unit.unitType, + unitName: unit.unitName, + conversionFactor: unit.conversionFactor, + unitOrder: unit.unitOrder, + baseUnit: unit.baseUnit, + status: unit.status, + unitTypeName: unit.unitTypeName + }); + return acc; + }, {}); + }, + + /** + * 获取计量单位列表(异步) + * @returns {Promise} 操作结果Promise + */ + getList() { + // 标记加载中 + this.loading = true; + return new Promise((resolve, reject) => { + listConvert(this.queryParams) + .then((response) => { + // 数据分组处理 + const unitDataGrouped = this.groupByUnitType(response.rows); + // 更新Store状态 + this.unitData = unitDataGrouped; + // 本地存储备份 + localStorage.setItem('unitData', JSON.stringify(unitDataGrouped)); + resolve(unitDataGrouped); + }) + .catch((error) => { + console.error('获取单位数据失败:', error); + reject(error); + }) + .finally(() => { + // 结束加载状态 + this.loading = false; + }); + }); + }, + + /** + * 重置查询参数(扩展:方便复用) + */ + resetQueryParams() { + this.queryParams = { + pageNum: 1, + pageSize: 1000, + unitType: null, + unitName: null, + baseUnit: null, + conversionFactor: null, + unitTypeName: null, + status: null, + unitOrder: null + }; + }, + + /** + * 刷新计量单位数据(扩展:重新获取最新数据) + * @returns {Promise} 操作结果Promise + */ + refreshUnitData() { + // 重置页码后重新获取 + this.queryParams.pageNum = 1; + return this.getList(); + }, + + /** + * 从本地缓存恢复单位数据(扩展:页面刷新后恢复) + */ + restoreUnitDataFromLocal() { + const localData = localStorage.getItem('unitData'); + if (localData) { + this.unitData = JSON.parse(localData); + } + } + } + } +); + +export default useUnitStore; \ No newline at end of file diff --git a/vite.config.js b/vite.config.js index a70e1cc..4a2546c 100644 --- a/vite.config.js +++ b/vite.config.js @@ -41,7 +41,7 @@ export default defineConfig(({ // https://cn.vitejs.dev/config/#server-proxy '/dev-api': { - target: 'http://10.75.166.198:9999', + target: 'https://ngtools.cn:8443', changeOrigin: true, rewrite: (p) => p.replace(/^\/dev-api/, '') },