tjx
2025-11-04 149e8e706de9d913d93b2605e2df05b697e354b6
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<template>
    <view class="inspection-page">
        <!-- 顶部显示机台信息与年份选择 -->
        <view class="header">
            <view class="machine-info">
                <text>当前机台:</text>
                <text class="machine-text">{{ machineNo || '未绑定' }}</text>
            </view>
            <view class="year-picker">
                <text class="year-label">点检年份</text>
                <picker mode="selector"
                        :range="yearOptions"
                        :value="yearPickerIndex"
                        @change="handleYearChange">
                    <view class="picker-trigger">{{ currentYear }} 年</view>
                </picker>
            </view>
        </view>
 
        <view class="section">
            <view class="section-title">日点检(31 天)</view>
            <view class="grid days-grid">
                <view v-for="(day, index) in days"
                      :key="`day-${day}`"
                      class="grid-cell"
                      :class="{ checked: dailyChecks[index] }"
                      @click="toggleDay(index)">
                    <text class="grid-text">{{ day }}日</text>
                </view>
            </view>
            <view class="summary">已点检:{{ checkedDaysCount }}/31</view>
        </view>
 
        <view class="section">
            <view class="section-title">月点检(12 月)</view>
            <view class="grid months-grid">
                <view v-for="(month, index) in months"
                      :key="`month-${month}`"
                      class="grid-cell"
                      :class="{ checked: monthlyChecks[index] }"
                      @click="toggleMonth(index)">
                    <text class="grid-text">{{ month }}月</text>
                </view>
            </view>
            <view class="summary">已点检:{{ checkedMonthsCount }}/12</view>
        </view>
 
        <view class="actions">
            <button class="btn-primary"
                    :loading="saving"
                    :disabled="saving || !machineNo"
                    @click="handleSave">
                保存
            </button>
            <button class="btn-secondary"
                    :disabled="saving"
                    @click="resetChecks">
                清空
            </button>
        </view>
    </view>
</template>
 
<script>
import { queryEquipmentInspection, saveEquipmentInspection } from '@/utils/equipmentInspection.js'
 
export default {
    name: 'EquipmentInspection',
    props: {
        machineNo: {
            type: String,
            required: true
        }
    },
    data() {
        const currentYear = new Date().getFullYear()
        return {
            // 当前年份用于区分不同年度的点检表
            currentYear,
            yearOptions: this.buildYearOptions(currentYear),
            dailyChecks: Array(31).fill(false),
            monthlyChecks: Array(12).fill(false),
            saving: false,
            loading: false,
            dirty: false
        }
    },
    computed: {
        yearPickerIndex() {
            const index = this.yearOptions.indexOf(this.currentYear)
            return index >= 0 ? index : 0
        },
        days() {
            return Array.from({ length: 31 }, (_, idx) => idx + 1)
        },
        months() {
            return Array.from({ length: 12 }, (_, idx) => idx + 1)
        },
        checkedDaysCount() {
            return this.dailyChecks.filter(Boolean).length
        },
        checkedMonthsCount() {
            return this.monthlyChecks.filter(Boolean).length
        }
    },
    watch: {
        machineNo: {
            immediate: true,
            handler(newVal) {
                if (!newVal) {
                    this.$showMessage('请先绑定机台')
                    return
                }
                this.loadInspectionData()
            }
        }
    },
    methods: {
        buildYearOptions(baseYear) {
            // 生成前后各两年的年份列表,方便切换历史记录
            const range = []
            for (let offset = -2; offset <= 2; offset += 1) {
                range.push(baseYear + offset)
            }
            return range
        },
        handleYearChange(event) {
            // 切换年份后读取对应的点检数据
            const index = Number(event.detail.value || 0)
            const selected = this.yearOptions[index]
            if (selected === this.currentYear) {
                return
            }
            this.currentYear = selected
            this.loadInspectionData()
        },
        async loadInspectionData() {
            // 从后台或本地缓存加载点检记录
            if (!this.machineNo) {
                return
            }
            this.loading = true
            try {
                const record = await queryEquipmentInspection(this, {
                    machineNo: this.machineNo,
                    year: this.currentYear
                }, { mock: true, showLoading: true })
                this.dailyChecks = record.dailyChecks || Array(31).fill(false)
                this.monthlyChecks = record.monthlyChecks || Array(12).fill(false)
                this.dirty = false
            } catch (error) {
                console.error('加载设备点检信息失败', error)
                this.$showMessage('点检记录加载失败')
            } finally {
                this.loading = false
            }
        },
        toggleDay(index) {
            // 切换日点检的勾选状态
            if (!this.machineNo) {
                this.$showMessage('请先绑定机台')
                return
            }
            this.$set(this.dailyChecks, index, !this.dailyChecks[index])
            this.dirty = true
        },
        toggleMonth(index) {
            // 切换月点检的勾选状态
            if (!this.machineNo) {
                this.$showMessage('请先绑定机台')
                return
            }
            this.$set(this.monthlyChecks, index, !this.monthlyChecks[index])
            this.dirty = true
        },
        async handleSave() {
            // 保存当前点检表,预留后台 POST 接口
            if (!this.machineNo) {
                this.$showMessage('请先绑定机台')
                return
            }
            if (this.saving) {
                return
            }
            this.saving = true
            try {
                const response = await saveEquipmentInspection(this, {
                    machineNo: this.machineNo,
                    year: this.currentYear,
                    dailyChecks: this.dailyChecks,
                    monthlyChecks: this.monthlyChecks
                }, { mock: true, showLoading: true })
 
                if (response && response.success) {
                    uni.showToast({ title: '保存成功', icon: 'success' })
                    this.dirty = false
                } else {
                    this.$showMessage('保存失败,请稍后再试')
                }
            } catch (error) {
                console.error('保存设备点检失败', error)
                this.$showMessage('保存失败,请检查网络')
            } finally {
                this.saving = false
            }
        },
        resetChecks() {
            // 一键清空勾选状态,便于重新录入
            this.dailyChecks = Array(31).fill(false)
            this.monthlyChecks = Array(12).fill(false)
            this.dirty = true
        }
    }
}
</script>
 
<style scoped>
.inspection-page {
    display: flex;
    flex-direction: column;
    padding: 2vh 2vw;
    font-size: 1.4vw;
    color: #333333;
}
 
.header {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2vh;
}
 
.machine-info {
    display: flex;
    flex-direction: row;
    align-items: center;
    font-size: 1.6vw;
}
 
.machine-text {
    font-weight: bold;
    margin-left: 0.5vw;
}
 
.year-picker {
    display: flex;
    flex-direction: row;
    align-items: center;
}
 
.year-label {
    margin-right: 1vw;
}
 
.picker-trigger {
    border: 1px solid #d8d8d8;
    border-radius: 0.8vw;
    padding: 0.6vh 1.4vw;
    background-color: #ffffff;
    font-size: 1.4vw;
}
 
.section {
    margin-bottom: 3vh;
    background-color: #ffffff;
    border-radius: 1vw;
    padding: 2vh 1.5vw;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}
 
.section-title {
    font-size: 1.8vw;
    font-weight: bold;
    margin-bottom: 1.5vh;
}
 
.grid {
    display: grid;
    grid-gap: 1vh;
}
 
.days-grid {
    grid-template-columns: repeat(7, 1fr);
}
 
.months-grid {
    grid-template-columns: repeat(4, 1fr);
}
 
.grid-cell {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 6vh;
    background-color: #f5f7fa;
    border-radius: 0.8vw;
    border: 1px solid #dcdfe6;
    color: #606266;
    transition: all 0.2s ease-in-out;
}
 
.grid-cell.checked {
    background-color: #0faeff;
    color: #ffffff;
    border-color: transparent;
}
 
.grid-cell:active {
    transform: scale(0.98);
}
 
.grid-text {
    font-size: 1.4vw;
}
 
.summary {
    margin-top: 1.5vh;
    font-size: 1.3vw;
    color: #909399;
}
 
.actions {
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    gap: 1vw;
}
 
.btn-primary,
.btn-secondary {
    min-width: 12vw;
    padding: 1.2vh 1vw;
    font-size: 1.4vw;
    border-radius: 0.8vw;
    border: none;
    text-align: center;
}
 
.btn-primary {
    background-color: #0faeff;
    color: #ffffff;
}
 
.btn-secondary {
    background-color: #ffffff;
    color: #0faeff;
    border: 1px solid #0faeff;
}
 
.btn-secondary:active,
.btn-primary:active {
    opacity: 0.8;
}
</style>