peng
2026-03-18 b89df58e3b783f3d718bd85a3e4c6a3bd9ee353c
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
<template>
  <a-modal centered width="35%" v-model="visibleShow" title="新增版本" @cancel="cancelClick" @ok="onsubmit">
    <a-form-model :model="queryForm" ref="ruleForm" :rules="rules">
      <a-form-model-item
        ref="version"
        prop="version"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
        label="版本号"
      >
        <a-input :maxLength="10" v-model="queryForm.version" placeholder="请输入版本号" allowClear/>
      </a-form-model-item>
      <a-form-model-item
        prop="path"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
        label="文件地址"
      >
        <a-upload
          name="file"
          :file-list="uploadedFileList"
          :showUploadList="true"
          :multiple="false"
          :customRequest="uploadFiles"
          :remove="deleteFileItem"
          :beforeUpload="beforeFileUpload"
        >
          <div class="upload">
            <a-button>
              <a-icon type="upload"/>
              选择文件
            </a-button>
            <div>文件不能超过200M</div>
          </div>
        </a-upload>
      </a-form-model-item>
      <a-form-model-item
        prop="description"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
        label="版本描述"
      >
        <a-textarea placeholder="请输入版本描述" v-model="queryForm.description" allow-clear/>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
 
<script>
import {uploadAction} from '@tievd/cube-block/lib/api/manage'
 
const formItemLayout = {
  labelCol: {span: 8},
  wrapperCol: {span: 12},
}
const formTailLayout = {
  labelCol: {span: 4},
  wrapperCol: {span: 8, offset: 4},
}
export default {
  created() {
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      formItemLayout,
      formTailLayout,
      visibleShow: false,
      queryForm: {
        version: '',
        description: '',
        fileSize: '',
        path: '',
      },
      uploadedFileList: [],
      rules: {
        version: [{required: true, message: '请输入版本号', trigger: 'blur'}],
        description: [{required: true, message: '请输入版本描述', trigger: 'blur'}],
        path: [{required: true, message: '请上传文件', trigger: 'blur'}],
      },
    }
  },
  watch: {
    visible(val) {
      this.visibleShow = val
    },
  },
  methods: {
    // 点击关闭或者是遮罩发射关闭信号给父组件,控制关闭
    cancelClick() {
      this.$emit('modalClose')
      this.$refs.ruleForm.resetFields()
      this.uploadedFileList = []
    },
    // 上传文件
    uploadFiles(info) {
      //初始化文件信息
      const fileInfo = {
        uid: info.file.uid,
        name: info.file.name,
        status: 'uploading',
        response: '',
        url: '',
      }
      //放入上传列表中,以便于显示上传进度
      this.uploadedFileList.push(fileInfo)
      let formData = new FormData()
      formData.append('file', info.file)
      uploadAction('/jyz/upgrade/upload', formData)
        .then((res) => {
          // console.log(res)
          //根据服务端返回的结果判断成功与否,设置文件条目的状态
          if (res.code == 200) {
            this.queryForm.path = res.result.path
            this.queryForm.fileSize = res.result.fileSize
            fileInfo.status = 'done'
            fileInfo.response = res.message
            fileInfo.url = res.result.path
            this.$message.success('上传成功!')
            console.log(this.queryForm)
          } else {
            fileInfo.status = 'error'
            fileInfo.response = res.message
            this.$message.error('上传失败!')
          }
        })
        .catch((err) => {
          fileInfo.status = 'error'
          fileInfo.response = err.message
        })
    },
    // 删除文件
    deleteFileItem(file) {
      console.log(file)
      //找到当前文件所在列表的索引
      let index = this.uploadedFileList.indexOf(file)
      //从列表中移除该文件
      this.uploadedFileList.splice(index, 1)
      return true
    },
    //上传之前的操作
    beforeFileUpload(file, fileList) {
      var this_ = this
      return new Promise((resolve, reject) => {
        const isSize = file.size > 200 * 1024 * 1024
        if (this.uploadedFileList.length == 1) {
          //可以限制只上传一个文件
          this_.$message.error('只能上传一个文件')
          reject(false)
          return
        }
        if (isSize) {
          this_.$message.error('上传文件不能超过200M!')
          reject(false)
          return
        }
        const name = file.name
        if (name.indexOf(".") == -1 || name.indexOf("V") == -1) {
          this_.$message.error('升级包名称不符合规范!')
          reject(false)
          return
        }
        const version = name.substring(name.indexOf("V") - 1, name.lastIndexOf("."));
        if (version.length === 0) {
          this_.$message.error('升级包名称不符合规范!')
          reject(false)
          return
        } else {
          this.queryForm.version = version
          this.queryForm.description = version
          resolve(true)
        }
      }).finally(() => {
 
      })
    },
    // 提交表单
    onsubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let formObj = JSON.parse(JSON.stringify(this.queryForm))
          this.$emit('modalClose', formObj)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
.upload {
  display: flex;
  align-items: center;
 
  div {
    margin-left: 10px;
    font-size: 12px;
    color: #fff;
  }
}
</style>