快乐的昕的电脑
2025-10-31 6b80b1382df821da347fac967777841032b9904f
components/mold.vue
@@ -4,26 +4,26 @@
        <view class="top-section-grid">
            <view class="form-cell">
                <label class="form-label">刀具编号:</label>
                <input class="input" v-model="selectedToolNo" placeholder="请通过刀具目录选择" disabled />
                <input class="input small-font" v-model="selectedToolNo" placeholder="请通过刀具目录选择" disabled />
                <button class="btn-blue" @click="openToolDialog" :disabled="loadingTools">刀具目录</button>
            </view>
            <view class="form-cell">
                <label class="form-label">设置使用上限:</label>
                <input class="input" type="number" v-model="useLimitInput" placeholder="每次换刀后手填" :disabled="!selectedToolNo || loadingForm" />
                <input class="input small-font" type="number" v-model="useLimitInput" placeholder="每次换刀后手填" :disabled="!selectedToolNo || loadingForm" />
            </view>
            <!-- 新增:寿命比预警值输入框 -->
            <view class="form-cell">
                <label class="form-label">寿命比预警值:</label>
                <input class="input"
                <input class="input small-font"
                       v-model="lifeWarnInput"
                       placeholder="如0.9或90(%)"
                       placeholder="如0.9或90或90%"
                       :disabled="!selectedToolNo || loadingForm" />
            </view>
            <view class="form-cell">
                <label class="form-label">刀具名称:</label>
                <input class="input" v-model="toolName" placeholder="刀具带出" disabled />
                <input class="input small-font" v-model="toolName" placeholder="刀具带出" disabled />
                <label class="form-label" style="margin-left: 16px;">规格型号:</label>
                <input class="input" v-model="toolModel" placeholder="刀具带出" disabled />
                <input class="input small-font" v-model="toolModel" placeholder="刀具带出" disabled />
            </view>
        </view>
@@ -107,10 +107,10 @@
        </view>
        <!-- 说明 -->
        <view class="tool-desc">
        <!--<view class="tool-desc">
            <p style="color:red;">'使用上限'以下刀时的'使用上限'为计算标准</p>
            <p style="color:red;">寿命比预警值默认为90%</p>
        </view>
        </view>-->
    </view>
</template>
@@ -138,7 +138,14 @@
                loadingForm: false,
                submitting: false,
                _searchTimer: null,
                workOrderCurrentCjNum: null // 工单当前数采
                workOrderCurrentCjNum: null, // 工单当前数采
                // 自动保存相关
                autoSaveTimer: null,
                isDirty: false, // 表单是否有未保存变更
                autoSaveIntervalMs: 5 * 60 * 1000, // 默认 5 分钟
                autoSaveEnabled: true,
                autoSaveActionName: 'handleUpTool' // 自动触发的方法名,可改为自定义保存方法
            };
        },
        computed: {
@@ -146,7 +153,56 @@
                return Math.max(1, Math.ceil(this.total / this.pageSize) || 1);
            }
        },
        watch: {
            // 标记脏数据:按需监听字段变化
            selectedToolNo() { this.isDirty = true; },
            useLimitInput() { this.isDirty = true; },
            lifeWarnInput() { this.isDirty = true; },
            toolName() { this.isDirty = true; },
            toolModel() { this.isDirty = true; }
        },
        methods: {
            // 自动保存:启动
            startAutoSave() {
                if (!this.autoSaveEnabled) return;
                this.stopAutoSave();
                this.autoSaveTimer = setInterval(() => {
                    this.autoSaveTick();
                }, this.autoSaveIntervalMs);
            },
            // 自动保存:停止
            stopAutoSave() {
                if (this.autoSaveTimer) {
                    clearInterval(this.autoSaveTimer);
                    this.autoSaveTimer = null;
                }
            },
            // 自动保存:每次定时执行时的逻辑
            async autoSaveTick() {
                if (!this.autoSaveEnabled) return;
                if (!this.isDirty) return;
                if (this.submitting || this.loadingForm) return;
                const fn = this.autoSaveActionName && typeof this[this.autoSaveActionName] === 'function'
                    ? this[this.autoSaveActionName]
                    : null;
                if (!fn) {
                    console.warn('自动保存:未找到方法', this.autoSaveActionName);
                    return;
                }
                try {
                    this.submitting = true;
                    await fn.call(this); // 调用保存方法(例如 handleUpTool)
                    // 如果保存成功,清脏标记(保存方法内部若失败没有抛出可保持此方式)
                    this.isDirty = false;
                } catch (e) {
                    console.error('自动保存失败:', e);
                } finally {
                    this.submitting = false;
                }
            },
            // 新增:寿命比预警值归一化 (返回 0~1 或 null)
            normalizeLifeWarn(v) {
                if (v == null) return null;
@@ -322,6 +378,7 @@
                } catch (err) {
                    console.error(err);
                    this.$showMessage('上刀提交失败,请检查网络');
                    throw err; // 抛出以便自动保存逻辑捕获并保留 isDirty
                } finally {
                    this.submitting = false;
                }
@@ -373,6 +430,7 @@
                } catch (err) {
                    console.error(err);
                    this.$showMessage('下刀提交失败,请检查网络');
                    throw err;
                } finally {
                    this.submitting = false;
                }
@@ -383,6 +441,7 @@
                this.toolModel = '';
                this.useLimitInput = '';
                this.lifeWarnInput = ''; // 新增:清空
                this.isDirty = false;
            },
            async fetchFormData() {
                if (!this.workOrderNo || !this.machineNo) {
@@ -560,6 +619,13 @@
            } else {
                console.warn('机台号或工单号为空,无法获取表单数据');
            }
            // 启动自动保存定时器
            this.startAutoSave();
        },
        beforeDestroy() {
            // 清理定时器,防止内存泄漏
            this.stopAutoSave();
        }
    };
</script>
@@ -598,6 +664,11 @@
        background: #f8f8f8;
    }
        /* 新增:小字体样式 */
        .input.small-font {
            font-size: 0.8vw; /* 调小字体 */
        }
    .form-select {
        width: 12vw;
        padding: 1vh;