2025-04-22 23:36:23 +08:00
|
|
|
|
<template>
|
2025-06-12 09:03:48 +08:00
|
|
|
|
<el-dialog :title="title" :visible.sync="dialogVisible" :close-on-click-modal="false" @close="handleClose" @open="handleOpen">
|
2025-04-22 23:36:23 +08:00
|
|
|
|
<el-form ref="form" :model="form" :rules="rules" label-width="100px">
|
|
|
|
|
|
<el-form-item label="固件名称" prop="firmwareName">
|
|
|
|
|
|
<el-input v-model="form.firmwareName" placeholder="请输入固件名称(板子+版本号)"></el-input>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
<el-form-item label="固件类型" prop="type">
|
2025-04-25 09:48:25 +08:00
|
|
|
|
<el-select v-model="form.type" placeholder="请选择固件类型" style="width: 100%;" filterable :disabled="isTypeDisabled">
|
2025-04-22 23:36:23 +08:00
|
|
|
|
<el-option v-for="item in firmwareTypes" :key="item.key" :label="item.name" :value="item.key"></el-option>
|
|
|
|
|
|
</el-select>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
<el-form-item label="版本号" prop="version">
|
|
|
|
|
|
<el-input v-model="form.version" placeholder="请输入版本号(x.x.x格式)"></el-input>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
<el-form-item label="固件文件" prop="firmwarePath">
|
2025-04-23 23:23:25 +08:00
|
|
|
|
<el-upload ref="upload" class="upload-demo" action="#" :http-request="handleUpload"
|
|
|
|
|
|
:before-upload="beforeUpload" :accept="'.bin,.apk'" :limit="1" :multiple="false" :auto-upload="true"
|
|
|
|
|
|
:on-remove="handleRemove">
|
2025-04-22 23:36:23 +08:00
|
|
|
|
<el-button size="small" type="primary">点击上传</el-button>
|
|
|
|
|
|
<div slot="tip" class="el-upload__tip">只能上传固件文件(.bin/.apk),且不超过100MB</div>
|
|
|
|
|
|
</el-upload>
|
2025-04-23 23:23:25 +08:00
|
|
|
|
<el-progress v-if="isUploading || uploadStatus === 'success'" :percentage="uploadProgress"
|
|
|
|
|
|
:status="uploadStatus"></el-progress>
|
2025-04-25 09:48:25 +08:00
|
|
|
|
<div class="hint-text">
|
2025-05-11 19:04:39 +08:00
|
|
|
|
<span>温馨提示:请上传合并前的xiaozhi.bin文件,而不是合并后的merged-binary.bin文件</span>
|
2025-04-25 09:48:25 +08:00
|
|
|
|
</div>
|
2025-04-22 23:36:23 +08:00
|
|
|
|
</el-form-item>
|
|
|
|
|
|
<el-form-item label="备注" prop="remark">
|
|
|
|
|
|
<el-input type="textarea" v-model="form.remark" placeholder="请输入备注信息"></el-input>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
</el-form>
|
|
|
|
|
|
<div slot="footer" class="dialog-footer">
|
|
|
|
|
|
<el-button @click="handleCancel">取 消</el-button>
|
|
|
|
|
|
<el-button type="primary" @click="handleSubmit">确 定</el-button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</el-dialog>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
import Api from '@/apis/api';
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
name: 'FirmwareDialog',
|
|
|
|
|
|
props: {
|
|
|
|
|
|
visible: {
|
|
|
|
|
|
type: Boolean,
|
|
|
|
|
|
default: false
|
|
|
|
|
|
},
|
|
|
|
|
|
title: {
|
|
|
|
|
|
type: String,
|
|
|
|
|
|
default: ''
|
|
|
|
|
|
},
|
|
|
|
|
|
form: {
|
|
|
|
|
|
type: Object,
|
|
|
|
|
|
default: () => ({})
|
2025-05-01 16:57:58 +08:00
|
|
|
|
},
|
|
|
|
|
|
firmwareTypes: {
|
|
|
|
|
|
type: Array,
|
|
|
|
|
|
default: () => []
|
2025-04-22 23:36:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-05-08 17:57:11 +08:00
|
|
|
|
|
2025-04-22 23:36:23 +08:00
|
|
|
|
data() {
|
|
|
|
|
|
return {
|
2025-04-23 23:23:25 +08:00
|
|
|
|
uploadProgress: 0,
|
|
|
|
|
|
uploadStatus: '',
|
|
|
|
|
|
isUploading: false,
|
2025-05-08 17:57:11 +08:00
|
|
|
|
dialogVisible: this.visible,
|
2025-04-22 23:36:23 +08:00
|
|
|
|
rules: {
|
|
|
|
|
|
firmwareName: [
|
|
|
|
|
|
{ required: true, message: '请输入固件名称(板子+版本号)', trigger: 'blur' }
|
|
|
|
|
|
],
|
|
|
|
|
|
type: [
|
|
|
|
|
|
{ required: true, message: '请选择固件类型', trigger: 'change' }
|
|
|
|
|
|
],
|
|
|
|
|
|
version: [
|
|
|
|
|
|
{ required: true, message: '请输入版本号', trigger: 'blur' },
|
|
|
|
|
|
{ pattern: /^\d+\.\d+\.\d+$/, message: '版本号格式不正确,请输入x.x.x格式', trigger: 'blur' }
|
|
|
|
|
|
],
|
|
|
|
|
|
firmwarePath: [
|
|
|
|
|
|
{ required: false, message: '请上传固件文件', trigger: 'change' }
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-04-25 09:48:25 +08:00
|
|
|
|
computed: {
|
|
|
|
|
|
isTypeDisabled() {
|
|
|
|
|
|
// 如果有id,说明是编辑模式,禁用类型选择
|
|
|
|
|
|
return !!this.form.id
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-05-01 16:57:58 +08:00
|
|
|
|
created() {
|
|
|
|
|
|
// 移除 getDictDataByType 调用
|
|
|
|
|
|
},
|
2025-05-08 17:57:11 +08:00
|
|
|
|
watch: {
|
|
|
|
|
|
visible(val) {
|
|
|
|
|
|
this.dialogVisible = val;
|
|
|
|
|
|
},
|
|
|
|
|
|
dialogVisible(val) {
|
|
|
|
|
|
this.$emit('update:visible', val);
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2025-04-22 23:36:23 +08:00
|
|
|
|
methods: {
|
2025-05-01 16:57:58 +08:00
|
|
|
|
// 移除 getFirmwareTypes 方法
|
2025-04-22 23:36:23 +08:00
|
|
|
|
handleClose() {
|
2025-05-08 17:57:11 +08:00
|
|
|
|
this.dialogVisible = false;
|
2025-04-22 23:36:23 +08:00
|
|
|
|
this.$emit('cancel');
|
|
|
|
|
|
},
|
|
|
|
|
|
handleCancel() {
|
|
|
|
|
|
this.$refs.form.clearValidate();
|
|
|
|
|
|
this.$emit('cancel');
|
|
|
|
|
|
},
|
|
|
|
|
|
handleSubmit() {
|
|
|
|
|
|
this.$refs.form.validate(valid => {
|
|
|
|
|
|
if (valid) {
|
|
|
|
|
|
// 如果是新增模式且没有上传文件,则提示错误
|
|
|
|
|
|
if (!this.form.id && !this.form.firmwarePath) {
|
|
|
|
|
|
this.$message.error('请上传固件文件')
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
// 提交成功后将关闭对话框的逻辑交给父组件处理
|
|
|
|
|
|
this.$emit('submit', this.form)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
},
|
|
|
|
|
|
beforeUpload(file) {
|
|
|
|
|
|
const isValidSize = file.size / 1024 / 1024 < 100
|
|
|
|
|
|
const isValidType = ['.bin', '.apk'].some(ext => file.name.toLowerCase().endsWith(ext))
|
|
|
|
|
|
|
|
|
|
|
|
if (!isValidType) {
|
|
|
|
|
|
this.$message.error('只能上传.bin/.apk格式的固件文件!')
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!isValidSize) {
|
|
|
|
|
|
this.$message.error('固件文件大小不能超过100MB!')
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
return true
|
|
|
|
|
|
},
|
|
|
|
|
|
handleUpload(options) {
|
|
|
|
|
|
const { file } = options
|
2025-04-23 23:23:25 +08:00
|
|
|
|
this.uploadProgress = 0
|
|
|
|
|
|
this.uploadStatus = ''
|
|
|
|
|
|
this.isUploading = true
|
|
|
|
|
|
|
|
|
|
|
|
// 使用setTimeout实现简单的0-50%过渡
|
|
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
|
|
if (this.uploadProgress < 50) { // 只有当进度小于50%时才设置
|
|
|
|
|
|
this.uploadProgress = 50
|
|
|
|
|
|
}
|
|
|
|
|
|
}, 1000)
|
|
|
|
|
|
|
2025-04-22 23:36:23 +08:00
|
|
|
|
Api.ota.uploadFirmware(file, (res) => {
|
2025-04-23 23:23:25 +08:00
|
|
|
|
clearTimeout(timer) // 清除定时器
|
2025-04-22 23:36:23 +08:00
|
|
|
|
res = res.data
|
|
|
|
|
|
if (res.code === 0) {
|
|
|
|
|
|
this.form.firmwarePath = res.data
|
|
|
|
|
|
this.form.size = file.size
|
2025-04-23 23:23:25 +08:00
|
|
|
|
this.uploadProgress = 100
|
|
|
|
|
|
this.uploadStatus = 'success'
|
2025-04-22 23:36:23 +08:00
|
|
|
|
this.$message.success('固件文件上传成功')
|
2025-04-23 23:23:25 +08:00
|
|
|
|
// 延迟2秒后隐藏进度条
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
this.isUploading = false
|
|
|
|
|
|
}, 2000)
|
2025-04-22 23:36:23 +08:00
|
|
|
|
} else {
|
2025-04-23 23:23:25 +08:00
|
|
|
|
this.uploadStatus = 'exception'
|
2025-04-22 23:36:23 +08:00
|
|
|
|
this.$message.error(res.msg || '文件上传失败')
|
2025-04-23 23:23:25 +08:00
|
|
|
|
this.isUploading = false
|
|
|
|
|
|
}
|
|
|
|
|
|
}, (progressEvent) => {
|
|
|
|
|
|
if (progressEvent.total) {
|
|
|
|
|
|
const progress = Math.round((progressEvent.loaded * 100) / progressEvent.total)
|
|
|
|
|
|
// 只有当进度大于50%时才更新
|
|
|
|
|
|
if (progress > 50) {
|
|
|
|
|
|
this.uploadProgress = progress
|
|
|
|
|
|
}
|
|
|
|
|
|
// 如果上传完成但还没收到成功响应,保持进度条显示
|
|
|
|
|
|
if (progress === 100) {
|
|
|
|
|
|
this.uploadStatus = ''
|
|
|
|
|
|
}
|
2025-04-22 23:36:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
2025-04-23 23:23:25 +08:00
|
|
|
|
},
|
|
|
|
|
|
handleRemove() {
|
|
|
|
|
|
this.form.firmwarePath = ''
|
|
|
|
|
|
this.form.size = 0
|
|
|
|
|
|
this.uploadProgress = 0
|
|
|
|
|
|
this.uploadStatus = ''
|
|
|
|
|
|
this.isUploading = false
|
|
|
|
|
|
},
|
|
|
|
|
|
handleOpen() {
|
|
|
|
|
|
// 重置上传相关状态
|
|
|
|
|
|
this.uploadProgress = 0
|
|
|
|
|
|
this.uploadStatus = ''
|
|
|
|
|
|
this.isUploading = false
|
|
|
|
|
|
// 重置表单中的文件相关字段
|
|
|
|
|
|
if (!this.form.id) { // 只在新增时重置
|
|
|
|
|
|
this.form.firmwarePath = ''
|
|
|
|
|
|
this.form.size = 0
|
|
|
|
|
|
}
|
2025-05-14 22:28:36 +08:00
|
|
|
|
// 无论是否编辑模式,都重置上传组件
|
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
|
|
if (this.$refs.upload) {
|
|
|
|
|
|
this.$refs.upload.clearFiles()
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2025-04-22 23:36:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2025-05-08 17:57:11 +08:00
|
|
|
|
::v-deep .el-dialog {
|
|
|
|
|
|
border-radius: 20px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-22 23:36:23 +08:00
|
|
|
|
.upload-demo {
|
|
|
|
|
|
text-align: left;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.el-upload__tip {
|
|
|
|
|
|
line-height: 1.2;
|
2025-05-08 17:57:11 +08:00
|
|
|
|
padding-top: 2%;
|
2025-04-22 23:36:23 +08:00
|
|
|
|
color: #909399;
|
|
|
|
|
|
}
|
2025-04-25 09:48:25 +08:00
|
|
|
|
|
|
|
|
|
|
.hint-text {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
gap: 8px;
|
2025-05-11 19:04:39 +08:00
|
|
|
|
font-size: 14px;
|
2025-04-25 09:48:25 +08:00
|
|
|
|
}
|
2025-04-22 23:36:23 +08:00
|
|
|
|
</style>
|