|
1 | 1 | <template> |
2 | | - <el-dialog :title="title" :visible.sync="visible" width="500px" @close="handleClose"> |
| 2 | + <el-dialog :title="title" :visible.sync="visible" width="500px" @close="handleClose" @open="handleOpen"> |
3 | 3 | <el-form ref="form" :model="form" :rules="rules" label-width="100px"> |
4 | 4 | <el-form-item label="固件名称" prop="firmwareName"> |
5 | 5 | <el-input v-model="form.firmwareName" placeholder="请输入固件名称(板子+版本号)"></el-input> |
|
13 | 13 | <el-input v-model="form.version" placeholder="请输入版本号(x.x.x格式)"></el-input> |
14 | 14 | </el-form-item> |
15 | 15 | <el-form-item label="固件文件" prop="firmwarePath"> |
16 | | - <el-upload class="upload-demo" action="#" :http-request="handleUpload" :before-upload="beforeUpload" |
17 | | - :accept="'.bin,.apk'" :limit="1" :multiple="false" :auto-upload="true"> |
| 16 | + <el-upload ref="upload" class="upload-demo" action="#" :http-request="handleUpload" |
| 17 | + :before-upload="beforeUpload" :accept="'.bin,.apk'" :limit="1" :multiple="false" :auto-upload="true" |
| 18 | + :on-remove="handleRemove"> |
18 | 19 | <el-button size="small" type="primary">点击上传</el-button> |
19 | 20 | <div slot="tip" class="el-upload__tip">只能上传固件文件(.bin/.apk),且不超过100MB</div> |
20 | 21 | </el-upload> |
| 22 | + <el-progress v-if="isUploading || uploadStatus === 'success'" :percentage="uploadProgress" |
| 23 | + :status="uploadStatus"></el-progress> |
21 | 24 | </el-form-item> |
22 | 25 | <el-form-item label="备注" prop="remark"> |
23 | 26 | <el-input type="textarea" v-model="form.remark" placeholder="请输入备注信息"></el-input> |
@@ -53,6 +56,9 @@ export default { |
53 | 56 | data() { |
54 | 57 | return { |
55 | 58 | firmwareTypes: FIRMWARE_TYPES, |
| 59 | + uploadProgress: 0, |
| 60 | + uploadStatus: '', |
| 61 | + isUploading: false, |
56 | 62 | rules: { |
57 | 63 | firmwareName: [ |
58 | 64 | { required: true, message: '请输入固件名称(板子+版本号)', trigger: 'blur' } |
@@ -108,16 +114,72 @@ export default { |
108 | 114 | }, |
109 | 115 | handleUpload(options) { |
110 | 116 | const { file } = options |
| 117 | + this.uploadProgress = 0 |
| 118 | + this.uploadStatus = '' |
| 119 | + this.isUploading = true |
| 120 | +
|
| 121 | + // 使用setTimeout实现简单的0-50%过渡 |
| 122 | + const timer = setTimeout(() => { |
| 123 | + if (this.uploadProgress < 50) { // 只有当进度小于50%时才设置 |
| 124 | + this.uploadProgress = 50 |
| 125 | + } |
| 126 | + }, 1000) |
| 127 | +
|
111 | 128 | Api.ota.uploadFirmware(file, (res) => { |
| 129 | + clearTimeout(timer) // 清除定时器 |
112 | 130 | res = res.data |
113 | 131 | if (res.code === 0) { |
114 | 132 | this.form.firmwarePath = res.data |
115 | 133 | this.form.size = file.size |
| 134 | + this.uploadProgress = 100 |
| 135 | + this.uploadStatus = 'success' |
116 | 136 | this.$message.success('固件文件上传成功') |
| 137 | + // 延迟2秒后隐藏进度条 |
| 138 | + setTimeout(() => { |
| 139 | + this.isUploading = false |
| 140 | + }, 2000) |
117 | 141 | } else { |
| 142 | + this.uploadStatus = 'exception' |
118 | 143 | this.$message.error(res.msg || '文件上传失败') |
| 144 | + this.isUploading = false |
| 145 | + } |
| 146 | + }, (progressEvent) => { |
| 147 | + if (progressEvent.total) { |
| 148 | + const progress = Math.round((progressEvent.loaded * 100) / progressEvent.total) |
| 149 | + // 只有当进度大于50%时才更新 |
| 150 | + if (progress > 50) { |
| 151 | + this.uploadProgress = progress |
| 152 | + } |
| 153 | + // 如果上传完成但还没收到成功响应,保持进度条显示 |
| 154 | + if (progress === 100) { |
| 155 | + this.uploadStatus = '' |
| 156 | + } |
119 | 157 | } |
120 | 158 | }) |
| 159 | + }, |
| 160 | + handleRemove() { |
| 161 | + this.form.firmwarePath = '' |
| 162 | + this.form.size = 0 |
| 163 | + this.uploadProgress = 0 |
| 164 | + this.uploadStatus = '' |
| 165 | + this.isUploading = false |
| 166 | + }, |
| 167 | + handleOpen() { |
| 168 | + // 重置上传相关状态 |
| 169 | + this.uploadProgress = 0 |
| 170 | + this.uploadStatus = '' |
| 171 | + this.isUploading = false |
| 172 | + // 重置表单中的文件相关字段 |
| 173 | + if (!this.form.id) { // 只在新增时重置 |
| 174 | + this.form.firmwarePath = '' |
| 175 | + this.form.size = 0 |
| 176 | + // 重置上传组件 |
| 177 | + this.$nextTick(() => { |
| 178 | + if (this.$refs.upload) { |
| 179 | + this.$refs.upload.clearFiles() |
| 180 | + } |
| 181 | + }) |
| 182 | + } |
121 | 183 | } |
122 | 184 | } |
123 | 185 | } |
|
0 commit comments