// 设备点检接口辅助方法,预留后台接入并提供本地缓存降级逻辑 const STORAGE_PREFIX = 'equipment_inspection_cache_'; // 本地缓存前缀,便于调试阶段存储 const DAY_COUNT = 31; const MONTH_COUNT = 12; function buildDefaultRecord() { return { dailyChecks: Array(DAY_COUNT).fill(false), monthlyChecks: Array(MONTH_COUNT).fill(false) }; } function buildStorageKey(machineNo, year) { return `${STORAGE_PREFIX}${machineNo || 'unknown'}_${year}`; } function normalizeResponse(payload) { if (!payload) { return buildDefaultRecord(); } const daily = Array.isArray(payload.dailyChecks) ? payload.dailyChecks.slice(0, DAY_COUNT) : []; const monthly = Array.isArray(payload.monthlyChecks) ? payload.monthlyChecks.slice(0, MONTH_COUNT) : []; return { dailyChecks: [...daily, ...Array(Math.max(0, DAY_COUNT - daily.length)).fill(false)], monthlyChecks: [...monthly, ...Array(Math.max(0, MONTH_COUNT - monthly.length)).fill(false)] }; } export async function queryEquipmentInspection(vueCtx, { machineNo, year }, options = {}) { const { mock = true, showLoading = false } = options; const params = { url: '/EquipmentInspection/Query', data: { machineNo, year }, showLoading }; if (!vueCtx || typeof vueCtx.$post !== 'function') { throw new Error('queryEquipmentInspection 需要传入 Vue 实例以便调用 $post'); } try { // 预留后台 POST 接口,请求成功后直接返回服务端数据 const response = await vueCtx.$post(params); if (response && response.success && response.data) { return normalizeResponse(response.data); } if (response && (response.dailyChecks || response.monthlyChecks)) { return normalizeResponse(response); } } catch (error) { // 后台尚未接入时降级使用本地缓存 if (!mock) { throw error; } } if (mock) { const cacheKey = buildStorageKey(machineNo, year); const cache = uni.getStorageSync(cacheKey); if (cache) { try { return normalizeResponse(JSON.parse(cache)); } catch (err) { console.error('解析设备点检缓存失败', err); } } return buildDefaultRecord(); } return buildDefaultRecord(); } export async function saveEquipmentInspection(vueCtx, { machineNo, year, dailyChecks, monthlyChecks }, options = {}) { const { mock = true, showLoading = true } = options; const payload = { machineNo, year, dailyChecks, monthlyChecks }; if (!vueCtx || typeof vueCtx.$post !== 'function') { throw new Error('saveEquipmentInspection 需要传入 Vue 实例以便调用 $post'); } try { // 预留后台保存接口,接口应返回 { success: true } 结构 const response = await vueCtx.$post({ url: '/EquipmentInspection/Save', data: payload, showLoading }); if (response && response.success) { return response; } if (!mock) { return response; } } catch (error) { if (!mock) { throw error; } } if (mock) { // 本地缓存模拟保存,便于前端演示与联调 const cacheKey = buildStorageKey(machineNo, year); uni.setStorageSync(cacheKey, JSON.stringify(payload)); return { success: true, message: '已保存至本地缓存' }; } return { success: false }; }