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
| <script> import { message, Upload, Button, Icon, Modal } from "ant-design-vue";
function getBase64(file) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => resolve(reader.result); reader.onerror = (error) => reject(error); }); }
/** * 基于 a-upload 二次封装的文件上传组件。 * @displayName SeekerUpload */ export default { inheritAttrs: false, name: "SeekerUpload", components: { "a-upload": Upload, "a-button": Button, "a-icon": Icon, "a-modal": Modal, }, props: { /** * 上传文件类型 * @values file image */ uploadType: { type: String, default: "file", validator: (x) => ["file", "image"].includes(x), }, /** 上传文件数量限制 */ number: { type: Number, default: 1, }, /** 文件上传按钮说明文字 */ btnText: { type: String, default: "选择文件", }, /** 图片上传说明文字 */ tipText: { type: String, default: "选择图片", }, alwaysShowBtn: { type: Boolean, default: false, }, }, computed: { listType() { if (this.uploadType === "image") { return "picture-card"; } return "text"; }, }, data() { return { previewImage: "", previewVisible: false, }; }, methods: { beforeUpload() { // 使用自定义上传方法 if (typeof this.$attrs.customRequest === "function") { return true; } return false; }, handleCancel() { this.previewVisible = false; }, async handlePreview(file) { if (this.uploadType === "file") { return; } if (!file.url && !file.preview) { file.preview = await getBase64(file.originFileObj); } this.previewImage = file.url || file.preview; this.previewVisible = true; }, handleChange({ fileList }) { const files = fileList.slice(-this.number); // 对于上传图片 限制图片大小在 800KB 以内 if (this.uploadType === "image" && files.length) { const limit = 800 * 1024; // 判断最后添加的图片 const latestAdd = files[files.length - 1]; if (latestAdd.size > limit) { message.error("图片大小超过限制,最大不得超过800KB!"); files.pop(); } } this.$emit("update:fileList", files); this.$emit("listChange", files); }, }, render() { const { listType, uploadType, $attrs, $listeners, handleCancel, previewVisible, previewImage, handlePreview, handleChange, tipText, btnText, beforeUpload, number, alwaysShowBtn, } = this; const bind = { props: { accept: uploadType === "image" ? "image/*" : undefined, ...$attrs, }, on: { preview: handlePreview, change: handleChange, ...$listeners, }, };
const uploadControl = uploadType === "image" ? ( <div> <a-icon type="plus" /> <div class="ant-upload-text">{tipText}</div> </div> ) : ( <a-button disabled={$attrs.disabled}> <a-icon type="upload" /> {btnText} </a-button> );
return ( <div> <a-upload list-type={listType} beforeUpload={beforeUpload} {...bind}> {($attrs.fileList && $attrs.fileList.length < number) || alwaysShowBtn ? uploadControl : null} </a-upload> <a-modal visible={previewVisible} footer={null} width={600} {...{ on: { cancel: handleCancel } }} > <img alt="example" style="width: 100%" src={previewImage} /> </a-modal> </div> ); }, }; </script>
|