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
| <template> <el-form-item label="驾驶证照片" prop="licenseImg"> <el-tooltip class="item" effect="dark" content="最多选择一个文件" placement="top-start" > <upload-img ref="licenseImg" refName="licenseImg" :fileList="drivingLicenseUploadList" id="license" :limit="1" @handleChange="handleChange" @handleRemove="handleRemove" ></upload-img> </el-tooltip> <span v-if="drivingLicenseUploadList.length > 0" v-show="false"></span> <img v-else v-for="(img, idx) in basicFormData.licenseImgUrls" :key="idx" :src="img.url" @click="handleImgPreview(img.url)" alt="" width="50%" /> {{ basicFormData.licenseImg }} </el-form-item> </template> <script> import uploadImg from '@/components/common/uploadImg'; export default { name: 'edit', data() { return { basicFormData: { ... licenseImg: '',
licenseImgUrls: [ { filePath: '', url: '' } ], }, drivingLicenseUploadList: [],
dialogImageUrl: '', dialogVisible: false }; }, methods: { /** * @description 上传的图片改变 * @param file 当前文件 * @param fileList 所有文件列表 * @param type 删除的哪个字段的图片 */ async handleChange(file, fileList, type) { const obj = { license: { list: this.drivingLicenseUploadList, target: "licenseImg", }, }; const isLt5M = file.size / 1024 / 1024 < 5; if (!isLt5M) { this.$message.warning("上传图片大小不能超过5MB!"); this.$refs[obj[type].target].$refs[obj[type].target].clearFiles(); return; } const data = await this.uploadImgFile(file); if (data.filePath) { this.basicFormData[obj[type].target] = data.filePath; obj[type].list.splice(0, obj[type].list.length); for (const x of fileList) { const fileName = x.name + x.size; if (x.raw) { obj[type].list.push({ ...x, fileName, fileUrl: x.uid === file.uid ? data.url : x.fileUrl, filePath: x.uid === file.uid ? data.filePath : x.filePath, }); } } } }, /** * @description 删除操作 * @param file 当前文件 * @param type 删除的哪个字段的图片 */ async handleRemove(file, type) { const obj = { license: { list: this.drivingLicenseUploadList, target: "licenseImg", }, }; const data = await this.deleteImgFile(file.filePath); if (data) { const targetIdx = obj[type].list.findIndex( (item) => item.uid !== file.uid ); obj[type].list.splice(targetIdx, 1); this.basicFormData[obj[type].target] = this.basicFormData.identificationImgUrl.length > 0 ? this.basicFormData.identificationImgUrl[0].filePath : ""; } }, /** * @description 预览图片 * @param url 图片url */ handleImgPreview(url) { this.dialogImageUrl = url; this.dialogVisible = true; }, /** * @description 上传图片文件 * @param file File原始文件 */ uploadImgFile(file) { const uploadForm = new FormData(); uploadForm.append("file", file.raw); uploadForm.append("moduleName", "driver");
return new Promise((resolve, reject) => { const url = `${HXConfig.Instance().mdaHttpUrl}/commonImg/uploadFile`; httpAsync(url, "post", uploadForm, { "Content-Type": "multipart/form-data", PROJECTCODE: "TMS", SID: localStorage.getItem("hafSID"), }) .then((res) => { if (res.code === 0) { resolve(res.data); } }) .catch((e) => { console.log(e); reject(e); }); }); }, /** * @description 删除图片 * @param filePath 图片tms路径 * @returns {Promise<unknown>} */ deleteImgFile(filePath) { return new Promise((resolve, reject) => { const url = `${ HXConfig.Instance().mdaHttpUrl }/commonImg/deleteListFilePath`; httpAsync( url, "post", [ { filePath, }, ], { PROJECTCODE: "TMS", SID: localStorage.getItem("hafSID"), } ) .then((res) => { if (res.code === 0) { resolve(true); } }) .catch((e) => { console.log(e); reject(e); }); }); }, } }; </script>
|