myUpload.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <template>
  2. <div class="upload_wrap">
  3. <block v-if="waitFileList.length > 0">
  4. <div class="el-upload-list el-upload-list--picture-card" v-if="listType == 'picture-card' || listType == 'picture'">
  5. <div class="el-upload-list__item is-success" v-for="( item, index ) in waitFileList " :key="index">
  6. <el-image style="width:148px;height:148px;" :src="item.url" fit="fill" :lazy="true"></el-image>
  7. <div v-if="!props.isDisableUpload" class="el-upload-list__item-actions">
  8. <el-icon class="cursor-pointer el-upload-list__item-delete" @click="removeFile(item)">
  9. <DeleteFilled />
  10. </el-icon>
  11. </div>
  12. <span v-if="isDownLoad" style="paddingleft: 5px">
  13. <el-icon @click="handleDownLoad(item)">
  14. <Download />
  15. </el-icon>
  16. </span>
  17. </div>
  18. </div>
  19. <div class="template_list" v-if="listType == 'text'">
  20. <div class="template" v-for="(item, index) in waitFileList" :key="index">
  21. <span>
  22. <el-icon>
  23. <Link />
  24. </el-icon>
  25. </span>
  26. <span class="documentName">{{ item.name }}</span>
  27. <span v-if="!props.isDisableUpload">
  28. <el-icon color="#000000a6" size="16" @click="removeFile(item)">
  29. <Close />
  30. </el-icon>
  31. </span>
  32. <span v-if="isDownLoad" style="paddingleft: 5px">
  33. <el-icon @click="handleDownLoad(item)">
  34. <Download />
  35. </el-icon>
  36. </span>
  37. </div>
  38. </div>
  39. </block>
  40. <el-upload v-else class="w-auto upload" ref="uploadRef" :file-list="waitFileList" :multiple="props.isMultiple"
  41. :limit="props.limitNum" :list-type="listType" :accept="props.acceptType" :auto-upload="false"
  42. :show-file-list="false" :disabled="props.isDisableUpload" :on-change="handleChange" :on-remove="handleRemove">
  43. <block>
  44. <div class="el-upload__text" v-if="listType == 'text'">
  45. <el-icon>
  46. <Upload />
  47. </el-icon>
  48. <span>上传文件</span>
  49. </div>
  50. <div v-else>
  51. <el-icon>
  52. <Plus />
  53. </el-icon>
  54. </div>
  55. </block>
  56. <template #tip v-if="!props.isDisableUpload && isShowTips">
  57. <div class="el-upload__tip">
  58. <span>支持{{ acceptTypeDesc }};</span> <span v-if="isLimitSize">文件大小不能超过{{
  59. props.maxFileSize }}M</span>
  60. </div>
  61. </template>
  62. </el-upload>
  63. <el-dialog v-model="dialogVisible">
  64. <img w-full :src="dialogImageUrl" alt="Preview Image" />
  65. </el-dialog>
  66. </div>
  67. </template>
  68. <script lang="ts" setup>
  69. import { ref, watch } from "vue";
  70. import { ElLoading, ElMessage } from "element-plus";
  71. import http from '@/api/http';
  72. import { Close, Upload, Download, Plus, Link, DeleteFilled } from "@element-plus/icons-vue";
  73. import type { UploadProps, UploadUserFile } from 'element-plus'
  74. const emits = defineEmits(["fileSuccess", "fileRemove"]);
  75. interface Props {
  76. acceptType?: string; // 上传文件类型
  77. acceptTypeDesc?: string; // 描述 - 上传文件类型
  78. isMultiple?: boolean; // 是否可批量上传
  79. isCheckName?: boolean; // 是否检查文件名
  80. limitNum?: number; // 允许上传文件的最大数量
  81. isDisableUpload?: boolean; // 是否禁用上传
  82. maxFileSize?: number; // 文件大小
  83. isLimitSize?: boolean;//是否限制大小
  84. isShowTips?: boolean;
  85. action?: string;
  86. formType?: string;//上传的文件字段名称
  87. fileList?: any; // 回显的文件
  88. isDownLoad?: boolean; // 是否可以下载
  89. listType?: string;//文件上传样式类型
  90. }
  91. // 接收父组件传递过来的参数
  92. const props = withDefaults(defineProps<Props>(), {
  93. acceptType: ".jpeg,.png",
  94. acceptTypeDesc: ".png/.jpeg",
  95. formType: "photo",
  96. isMultiple: false,
  97. isCheckName: false,
  98. limitNum: 10,
  99. isDisableUpload: false,
  100. maxFileSize: 0.3,
  101. isShowTips: true,
  102. isLimitSize: true,
  103. action: "/qiniu/upload/image",
  104. fileList: [],
  105. isDownLoad: false,
  106. listType: 'picture-card'
  107. });
  108. let waitFileList = ref<any[]>([]);
  109. waitFileList.value = props.fileList;
  110. // waitFileList.value?.forEach((item: any) => {
  111. // item.name = item.original;
  112. // });
  113. const dialogImageUrl = ref('')
  114. const dialogVisible = ref(false)
  115. const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
  116. dialogImageUrl.value = uploadFile.url!
  117. dialogVisible.value = true
  118. }
  119. const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  120. console.log(uploadFile, uploadFiles)
  121. }
  122. watch(
  123. () => props.fileList,
  124. () => {
  125. console.log("props.fileList====>", props.fileList);
  126. waitFileList.value = props.fileList;
  127. // waitFileList.value?.forEach((item: any) => {
  128. // item.name = item.original;
  129. // });
  130. }
  131. );
  132. // 文件变化Handle 这里监听上传文件的变化是一个一个接收到变化的,所以文件也是一个一个上传到服务器上面的
  133. const handleChange = async (file: any, fileList: any[]) => {
  134. console.log(fileList, 'fileListfileList');
  135. console.log(file, 'filefilefile', /^[0-9_][0-9_]*$/.test(file.name));
  136. if (!/^[0-9]+_/.test(file.name) && props.isCheckName) {
  137. ElMessage.error(`文件上传格式错误`);
  138. return false
  139. }
  140. // 防止多次执行change
  141. const rawFile = file.raw;
  142. const list = props.acceptTypeDesc.split("/");
  143. let acceptTypeList = list.map((its: string) => {
  144. return getType(its)
  145. })
  146. // 如果要检索的字符串值没有出现,则该方法返回 -1
  147. const ars = acceptTypeList.filter((q: string) => {
  148. return rawFile.type.indexOf(q) > -1
  149. })
  150. // 用于校验是否符合上传条件
  151. const type = props.acceptTypeDesc.replace("/", ", ");
  152. if (ars.length < 1) {
  153. ElMessage.error(`仅支持格式为${type}的图片`);
  154. return false;
  155. } else if (rawFile.size / 1024 / 1024 > props.maxFileSize && props.isLimitSize) {
  156. ElMessage.error(`文件大小不能超过${props.maxFileSize}MB!`);
  157. const arr = [...waitFileList.value];
  158. waitFileList.value = arr.filter((item: any) => {
  159. return item.uid != rawFile.uid;
  160. });
  161. return false;
  162. } else {
  163. let formData = new FormData();
  164. formData.append(props.formType, rawFile);
  165. const loadingInstance = ElLoading.service({
  166. text: "正在上传",
  167. background: "rgba(0,0,0,.2)",
  168. });
  169. // 上传到服务器上面
  170. const requestURL: string = props.action;
  171. http.post(requestURL, formData)
  172. .then(async (res: any) => {
  173. if (res.code == 10000) {
  174. loadingInstance.close();
  175. waitFileList.value.push({ uid: file.uid, url: res.data })
  176. emits("fileSuccess", res.data);
  177. } else {
  178. loadingInstance.close();
  179. ElMessage.warning(`文件上传失败`);
  180. }
  181. })
  182. .catch(() => {
  183. loadingInstance.close();
  184. // ElMessage.warning(`文件上传失败`);
  185. });
  186. }
  187. return true;
  188. };
  189. // 校验上传文件格式
  190. const getType = (acceptType: string) => {
  191. let val = "";
  192. switch (acceptType) {
  193. case "xls":
  194. val = "excel";
  195. break;
  196. case "doc":
  197. val = "word";
  198. break;
  199. case "pdf":
  200. val = "pdf";
  201. break;
  202. case "zip":
  203. val = "zip";
  204. break;
  205. case "xlsx":
  206. val = "sheet";
  207. break;
  208. case "pptx":
  209. val = "presentation";
  210. break;
  211. case "docx":
  212. val = "document";
  213. break;
  214. case "text":
  215. val = "text";
  216. break;
  217. case "jpeg":
  218. val = "jpeg";
  219. break;
  220. case "png":
  221. val = "png";
  222. break;
  223. case "gif":
  224. val = "gif";
  225. break;
  226. case "mp4":
  227. val = "mp4";
  228. break;
  229. case "mp3":
  230. val = "mp3";
  231. break;
  232. }
  233. return val
  234. };
  235. // 移除文件
  236. const removeFile = (file: any) => {
  237. const arr: any[] = [...waitFileList.value];
  238. waitFileList.value = arr.filter((its: any) => {
  239. console.log(its, file);
  240. return its.uid != file.uid;
  241. });
  242. console.log(file, 'file', arr, waitFileList.value);
  243. emits("fileRemove", file);
  244. };
  245. const handleDownLoad = (row: { ossFile: string }) => {
  246. const str = window.location.href.split("#")[0];
  247. const herf = str.slice(0, str.length - 1);
  248. window.location.href = herf + row.ossFile;
  249. };
  250. </script>
  251. <style lang="scss" scoped>
  252. .upload_wrap {
  253. .upload {
  254. width: auto;
  255. padding-bottom: 10px;
  256. }
  257. .tips {
  258. display: block;
  259. font-size: 14px;
  260. font-family: PingFangSC-Regular, PingFang SC;
  261. font-weight: 400;
  262. color: rgba(0, 0, 0, 0.65);
  263. }
  264. }
  265. :deep().el-upload__text {
  266. width: 106px;
  267. height: 32px;
  268. display: flex;
  269. align-items: center;
  270. justify-content: center;
  271. background: #ffffff;
  272. border: 1px solid rgba(0, 0, 0, 0.15);
  273. img {
  274. display: block;
  275. width: 14px;
  276. height: 14px;
  277. }
  278. span {
  279. font-size: 14px;
  280. padding-left: 6px;
  281. font-family: PingFangSC-Regular, PingFang SC;
  282. font-weight: 400;
  283. color: rgba(0, 0, 0, 0.65);
  284. }
  285. }
  286. .template_list {
  287. padding-bottom: 4px;
  288. }
  289. .template {
  290. display: flex;
  291. align-items: center;
  292. padding: 5px 0;
  293. span {
  294. line-height: 16px;
  295. }
  296. img {
  297. margin-right: 8px;
  298. width: 16px;
  299. height: 16px;
  300. }
  301. .documentName {
  302. margin-right: 12px;
  303. font-size: 14px;
  304. color: rgba(0, 0, 0, 0.65);
  305. }
  306. }
  307. </style>