import { createNamespace } from '../../utils'; import Icon from '../../icon'; import Loading from '../../loading'; import Uploader from '../../uploader'; const [createComponent, bem] = createNamespace('sku-img-uploader'); export default createComponent({ props: { value: String, uploadImg: Function, maxSize: { type: Number, default: 6 } }, data() { return { // 正在上传的图片 base64 paddingImg: '', uploadFail: false }; }, methods: { afterReadFile(file) { // 上传文件 this.paddingImg = file.content; this.uploadFail = false; this.uploadImg(file.file, file.content) .then(img => { this.$emit('input', img); this.$nextTick(() => { this.paddingImg = ''; }); }) .catch(() => { this.uploadFail = true; }); }, onOversize() { this.$toast(`最大可上传图片为${this.maxSize}MB,请尝试压缩图片尺寸`); }, renderUploader(content, disabled = false) { return (
{content}
); }, renderMask() { return (
{this.uploadFail ? ( [ ,
上传失败
重新上传
] ) : ( )}
); } }, render(h) { return (
{this.value && this.renderUploader( [ , { this.$emit('input', ''); }} /> ], true )} {this.paddingImg && this.renderUploader( [ , this.renderMask() ], !this.uploadFail )} {!this.value && !this.paddingImg && this.renderUploader(
)}
); } });