Files
klp-oa/klp-ui/src/store/modules/category.js

250 lines
7.4 KiB
JavaScript
Raw Normal View History

// import { listCategory } from '@/api/wms/category';
// import { listBomItem } from '@/api/wms/bomItem';
// import { listProduct } from '@/api/wms/product';
// import { listRawMaterial } from '@/api/wms/rawMaterial';
import veilReq from '@/utils/veilReq';
const listRawMaterial = (params) => veilReq({
url: '/wms/rawMaterial/list',
method: 'get',
params
});
const listProduct = (params) => veilReq({
url: '/wms/product/list',
method: 'get',
params
});
2025-08-02 13:38:04 +08:00
2025-07-19 17:29:15 +08:00
const state = {
2025-07-28 18:25:02 +08:00
categoryList: [],
productMap: {},
2025-07-30 10:53:06 +08:00
rawMaterialMap: {},
productList: [],
2025-07-31 18:07:51 +08:00
rawMaterialList: [],
bomMap: {}
2025-07-19 17:29:15 +08:00
};
const mutations = {
SET_CATEGORY_LIST(state, list) {
state.categoryList = list;
2025-07-28 18:25:02 +08:00
},
2025-08-02 13:38:04 +08:00
2025-07-28 18:25:02 +08:00
SET_PRODUCT_MAP(state, map) {
state.productMap = map;
},
SET_RAW_MATERIAL_MAP(state, map) {
state.rawMaterialMap = map;
2025-07-30 10:53:06 +08:00
},
SET_PRODUCT_LIST(state, list) {
state.productList = list;
},
SET_RAW_MATERIAL_LIST(state, list) {
state.rawMaterialList = list;
2025-07-31 18:07:51 +08:00
},
SET_BOM_MAP(state, map) {
state.bomMap = map;
2025-07-19 17:29:15 +08:00
}
};
const actions = {
getCategoryList({ state, commit }) {
if (state.categoryList.length > 0) {
return Promise.resolve(state.categoryList);
}
2025-07-31 18:07:51 +08:00
return listCategory({ pageNum: 1, pageSize: 10000 }).then(res => {
2025-07-19 17:29:15 +08:00
commit('SET_CATEGORY_LIST', res.rows || []);
return res.rows || [];
});
2025-07-28 18:25:02 +08:00
},
getProductMap({ state, commit }) {
// 若已有缓存数据,直接返回
2025-07-28 18:25:02 +08:00
if (Object.keys(state.productMap).length > 0) {
return Promise.resolve(state.productMap);
}
const pageSize = 5000; // 每次获取5000条
const allRows = []; // 存储所有批次的列表数据
const productMap = {}; // 最终的产品映射表
const maxRetries = 5; // 最大重试次数
// 带重试机制的请求函数
const fetchWithRetry = async (pageNum) => {
let retries = 0;
while (retries < maxRetries) {
try {
// 尝试调用接口
const res = await listProduct({ pageNum, pageSize });
return res; // 成功则返回结果
} catch (error) {
retries++;
if (retries >= maxRetries) {
// 达到最大重试次数,抛出错误
throw new Error(`获取第${pageNum}页产品数据失败,已重试${maxRetries}次: ${error.message}`);
}
// 可选添加重试间隔如1秒避免频繁请求
await new Promise(resolve => setTimeout(resolve, 1000));
console.log(`获取第${pageNum}页失败,正在进行第${retries}次重试...`);
}
}
};
// 异步处理分批次获取逻辑
const fetchAllProducts = async () => {
// 1. 获取第一页数据拿到总条数total带重试
let currentPage = 1;
const firstRes = await fetchWithRetry(currentPage);
const total = firstRes.total || 0;
const firstRows = firstRes.rows || [];
// 处理第一页数据
allRows.push(...firstRows);
firstRows.forEach(item => {
productMap[item.productId.toString()] = item;
2025-07-28 18:25:02 +08:00
});
// 2. 计算总页数,循环获取剩余页面数据(每一页都带重试)
const totalPages = Math.ceil(total / pageSize);
for (currentPage = 2; currentPage <= totalPages; currentPage++) {
const res = await fetchWithRetry(currentPage);
const rows = res.rows || [];
// 合并当前页数据到总列表和映射表
allRows.push(...rows);
rows.forEach(item => {
productMap[item.productId.toString()] = item;
});
}
// 3. 所有数据获取完成后,更新状态
commit('SET_PRODUCT_MAP', productMap);
commit('SET_PRODUCT_LIST', allRows);
return productMap;
};
// 返回Promise确保外部可通过.then获取结果
return fetchAllProducts();
2025-07-28 18:25:02 +08:00
},
getRawMaterialMap({ state, commit }) {
// 若已有缓存数据,直接返回
2025-07-28 18:25:02 +08:00
if (Object.keys(state.rawMaterialMap).length > 0) {
return Promise.resolve(state.rawMaterialMap);
}
const pageSize = 5000; // 每次获取5000条
const allRows = []; // 存储所有批次的原材料列表
const rawMaterialMap = {}; // 最终的原材料映射表
const maxRetries = 5; // 最大重试次数
// 带重试机制的请求函数(针对原材料接口)
const fetchWithRetry = async (pageNum) => {
let retries = 0;
while (retries < maxRetries) {
try {
// 尝试调用原材料列表接口
const res = await listRawMaterial({ pageNum, pageSize });
return res; // 成功则返回结果
} catch (error) {
retries++;
if (retries >= maxRetries) {
// 达到最大重试次数,抛出错误(包含具体页码)
throw new Error(`获取第${pageNum}页原材料数据失败,已重试${maxRetries}次: ${error.message}`);
}
// 重试间隔1秒避免频繁请求
await new Promise(resolve => setTimeout(resolve, 1000));
console.log(`获取第${pageNum}页原材料失败,正在进行第${retries}次重试...`);
}
}
};
// 异步处理分批次获取逻辑
const fetchAllRawMaterials = async () => {
// 1. 获取第一页数据(带重试)
let currentPage = 1;
const firstRes = await fetchWithRetry(currentPage);
const total = firstRes.total || 0;
const firstRows = firstRes.rows || [];
// 处理第一页数据
allRows.push(...firstRows);
firstRows.forEach(item => {
rawMaterialMap[item.rawMaterialId.toString()] = item;
2025-07-28 18:25:02 +08:00
});
// 2. 循环获取剩余页面数据(每一页都带重试)
const totalPages = Math.ceil(total / pageSize);
for (currentPage = 2; currentPage <= totalPages; currentPage++) {
const res = await fetchWithRetry(currentPage);
const rows = res.rows || [];
// 合并当前页数据
allRows.push(...rows);
rows.forEach(item => {
rawMaterialMap[item.rawMaterialId.toString()] = item;
});
}
// 3. 更新状态
commit('SET_RAW_MATERIAL_MAP', rawMaterialMap);
commit('SET_RAW_MATERIAL_LIST', allRows);
return rawMaterialMap;
};
// 返回Promise供外部调用
return fetchAllRawMaterials();
2025-07-31 18:07:51 +08:00
},
getBomMap({ state, commit }) {
if (Object.keys(state.bomMap).length > 0) {
return Promise.resolve(state.bomMap);
}
return listBomItem({ pageNum: 1, pageSize: 100000 }).then(res => {
2025-07-31 18:07:51 +08:00
console.log('bomItem', res)
const map = {};
res.rows.forEach(item => {
if (!map[item.bomId]) {
map[item.bomId] = [];
}
map[item.bomId].push(item);
});
commit('SET_BOM_MAP', map);
return map;
})
2025-07-19 17:29:15 +08:00
}
};
2025-08-02 13:38:04 +08:00
export function findItemWithBom(itemType, itemId) {
if (!itemType || !itemId) {
return null;
}
let map = {}
if (itemType === 'product') {
map = state.productMap;
} else if (itemType === 'raw_material') {
map = state.rawMaterialMap;
} else {
return null;
}
const item = map[itemId];
if (!item) {
return null;
}
const bomId = item.bomId
if (!bomId) {
return null;
}
const bomItems = state.bomMap[bomId];
return {
...item,
boms: bomItems || [],
itemName: itemType === 'product' ? item.productName : item.rawMaterialName,
itemType,
};
}
2025-07-19 17:29:15 +08:00
export default {
namespaced: true,
state,
mutations,
2025-07-29 15:00:15 +08:00
actions,
2025-07-19 17:29:15 +08:00
};