// 设备点检接口辅助方法,预留后台接入并提供本地缓存降级逻辑
|
const STORAGE_PREFIX = 'equipment_inspection_cache_'; // 本地缓存前缀,便于调试阶段存储
|
const DAY_COUNT = 31;
|
const DAILY_ITEM_COUNT = 6; // 日常点检项目数量
|
const MONTHLY_ITEM_COUNT = 2; // 月度点检项目数量
|
|
function buildDefaultRecord() {
|
return {
|
dailyChecks: Array(DAILY_ITEM_COUNT).fill(null).map(() => Array(DAY_COUNT).fill(false)),
|
monthlyChecks: Array(MONTHLY_ITEM_COUNT).fill(null).map(() => Array(DAY_COUNT).fill(false))
|
};
|
}
|
|
function buildStorageKey(machineNo, year) {
|
return `${STORAGE_PREFIX}${machineNo || 'unknown'}_${year}`;
|
}
|
|
function normalizeResponse(payload) {
|
if (!payload) {
|
return buildDefaultRecord();
|
}
|
|
// 验证并规范化日常点检数据(6项×31天)
|
let dailyChecks = [];
|
if (Array.isArray(payload.dailyChecks)) {
|
dailyChecks = payload.dailyChecks.slice(0, DAILY_ITEM_COUNT).map(item => {
|
if (Array.isArray(item)) {
|
// 确保每项都是31天的数组
|
const normalized = item.slice(0, DAY_COUNT);
|
while (normalized.length < DAY_COUNT) {
|
normalized.push(false);
|
}
|
return normalized;
|
}
|
// 如果不是数组,返回全false的31天数组
|
return Array(DAY_COUNT).fill(false);
|
});
|
}
|
// 补齐到6项
|
while (dailyChecks.length < DAILY_ITEM_COUNT) {
|
dailyChecks.push(Array(DAY_COUNT).fill(false));
|
}
|
|
// 验证并规范化月度点检数据(2项×31天)
|
let monthlyChecks = [];
|
if (Array.isArray(payload.monthlyChecks)) {
|
monthlyChecks = payload.monthlyChecks.slice(0, MONTHLY_ITEM_COUNT).map(item => {
|
if (Array.isArray(item)) {
|
// 确保每项都是31天的数组
|
const normalized = item.slice(0, DAY_COUNT);
|
while (normalized.length < DAY_COUNT) {
|
normalized.push(false);
|
}
|
return normalized;
|
}
|
// 如果不是数组,返回全false的31天数组
|
return Array(DAY_COUNT).fill(false);
|
});
|
}
|
// 补齐到2项
|
while (monthlyChecks.length < MONTHLY_ITEM_COUNT) {
|
monthlyChecks.push(Array(DAY_COUNT).fill(false));
|
}
|
|
return {
|
dailyChecks,
|
monthlyChecks
|
};
|
}
|
|
export async function queryEquipmentInspection(vueCtx, { machineNo, date }, options = {}) {
|
const { mock = true, showLoading = false } = options;
|
const params = {
|
url: '/EquipmentInspection/Query',
|
data: { machineNo, date },
|
showLoading
|
};
|
|
if (!vueCtx || typeof vueCtx.$post !== 'function') {
|
throw new Error('queryEquipmentInspection 需要传入 Vue 实例以便调用 $post');
|
}
|
|
try {
|
// 预留后台 POST 接口,请求成功后直接返回服务端数据
|
const response = await vueCtx.$post(params);
|
|
// 处理标准的 MES API 响应格式 { status: 0, data: {...} }
|
if (response && response.status === 0 && response.data) {
|
return normalizeResponse(response.data);
|
}
|
|
// 处理 { success: true, data: {...} } 格式
|
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) {
|
// 从 date (yyyy-MM) 提取年份用于缓存key
|
const year = date ? date.split('-')[0] : new Date().getFullYear();
|
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, date, dailyChecks, monthlyChecks }, options = {}) {
|
const { mock = true, showLoading = true } = options;
|
const payload = {
|
machineNo,
|
date,
|
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) {
|
// 本地缓存模拟保存,便于前端演示与联调
|
// 从 date (yyyy-MM) 提取年份用于缓存key
|
const year = date ? date.split('-')[0] : new Date().getFullYear();
|
const cacheKey = buildStorageKey(machineNo, year);
|
uni.setStorageSync(cacheKey, JSON.stringify(payload));
|
return { success: true, message: '已保存至本地缓存' };
|
}
|
|
return { success: false };
|
}
|