diff --git a/klp-ui/src/store/modules/category.js b/klp-ui/src/store/modules/category.js index 1cc9cc85..1b7bdcba 100644 --- a/klp-ui/src/store/modules/category.js +++ b/klp-ui/src/store/modules/category.js @@ -61,15 +61,37 @@ const actions = { return Promise.resolve(state.productMap); } - const pageSize = 2000; // 每次获取2000条 + 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 + // 1. 获取第一页数据,拿到总条数total(带重试) let currentPage = 1; - const firstRes = await listProduct({ pageNum: currentPage, pageSize }); + const firstRes = await fetchWithRetry(currentPage); const total = firstRes.total || 0; const firstRows = firstRes.rows || []; @@ -79,10 +101,10 @@ const actions = { productMap[item.productId.toString()] = item; }); - // 2. 计算总页数,循环获取剩余页面数据 + // 2. 计算总页数,循环获取剩余页面数据(每一页都带重试) const totalPages = Math.ceil(total / pageSize); for (currentPage = 2; currentPage <= totalPages; currentPage++) { - const res = await listProduct({ pageNum: currentPage, pageSize }); + const res = await fetchWithRetry(currentPage); const rows = res.rows || []; // 合并当前页数据到总列表和映射表 @@ -108,15 +130,37 @@ const actions = { return Promise.resolve(state.rawMaterialMap); } - const pageSize = 2000; // 每次获取2000条 + 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. 获取第一页数据,拿到总条数total + // 1. 获取第一页数据(带重试) let currentPage = 1; - const firstRes = await listRawMaterial({ pageNum: currentPage, pageSize }); + const firstRes = await fetchWithRetry(currentPage); const total = firstRes.total || 0; const firstRows = firstRes.rows || []; @@ -126,27 +170,27 @@ const actions = { rawMaterialMap[item.rawMaterialId.toString()] = item; }); - // 2. 计算总页数,循环获取剩余页面数据 + // 2. 循环获取剩余页面数据(每一页都带重试) const totalPages = Math.ceil(total / pageSize); for (currentPage = 2; currentPage <= totalPages; currentPage++) { - const res = await listRawMaterial({ pageNum: currentPage, pageSize }); + const res = await fetchWithRetry(currentPage); const rows = res.rows || []; - // 合并当前页数据到总列表和映射表 + // 合并当前页数据 allRows.push(...rows); rows.forEach(item => { rawMaterialMap[item.rawMaterialId.toString()] = item; }); } - // 3. 所有数据获取完成后,更新状态 + // 3. 更新状态 commit('SET_RAW_MATERIAL_MAP', rawMaterialMap); commit('SET_RAW_MATERIAL_LIST', allRows); return rawMaterialMap; }; - // 返回Promise,确保外部可通过.then获取结果 + // 返回Promise供外部调用 return fetchAllRawMaterials(); }, getBomMap({ state, commit }) {