将单位缓存通过pina store方式实现。

This commit is contained in:
廖德云 2025-12-08 20:43:44 +08:00
parent 3a57b926fd
commit bfe9142db2
3 changed files with 121 additions and 47 deletions

View File

@ -8,54 +8,9 @@
import useSettingsStore from '@/store/modules/settings' import useSettingsStore from '@/store/modules/settings'
import { import {
handleThemeStyle handleThemeStyle
} from '@/utils/theme' } from '@/utils/theme'
import {
listConvert
} from '@/api/system/convert.js';
const settingsStore = useSettingsStore() 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(() => { onMounted(() => {
getList();
nextTick(() => { nextTick(() => {
settingsStore.initSetting(() => { settingsStore.initSetting(() => {
// //

View File

@ -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;

View File

@ -41,7 +41,7 @@ export default defineConfig(({
// https://cn.vitejs.dev/config/#server-proxy // https://cn.vitejs.dev/config/#server-proxy
'/dev-api': { '/dev-api': {
target: 'http://10.75.166.198:9999', target: 'https://ngtools.cn:8443',
changeOrigin: true, changeOrigin: true,
rewrite: (p) => p.replace(/^\/dev-api/, '') rewrite: (p) => p.replace(/^\/dev-api/, '')
}, },