tjx
2025-11-04 8f845ec72eecdbaef3badcceda75d59b6efef416
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// 设备点检接口辅助方法,预留后台接入并提供本地缓存降级逻辑
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, 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);
        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 };
}