将单位缓存通过pina store方式实现。
This commit is contained in:
parent
3a57b926fd
commit
bfe9142db2
47
src/App.vue
47
src/App.vue
@ -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(() => {
|
||||||
// 初始化主题样式
|
// 初始化主题样式
|
||||||
|
|||||||
119
src/store/modules/unitData.ts
Normal file
119
src/store/modules/unitData.ts
Normal 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;
|
||||||
@ -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/, '')
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user