diff --git a/src/assets/scripts/uploadImageFile.js b/src/assets/scripts/uploadImageFile.js new file mode 100644 index 0000000..c03e767 --- /dev/null +++ b/src/assets/scripts/uploadImageFile.js @@ -0,0 +1,30 @@ +import fileApi from '../../api/file'; + +export function uploadImgFile(file) { + return new Promise((resolve, reject)=> { + const checkImageResult = isImageIllegal(file); + + if (checkImageResult) { + reject(checkImageResult); + } else { + let fd = new FormData(); + + fd.append('file', file); + fileApi.fileUpload(fd).then(res => { + resolve(res); + }).catch(err => { + console.log(err.message) + }) + } + }); +} + +export function isImageIllegal(file) { + if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(file.name)) { + return '请上传 JPG/PNG/GIF 格式的图片'; + } + if (file.size > 5 * 1024 * 1024) { + return '由于公众号限制,图片大小不能超过 5.0M'; + } + return false; +} \ No newline at end of file diff --git a/src/assets/scripts/util.js b/src/assets/scripts/util.js index 5520ecc..6d417e4 100644 --- a/src/assets/scripts/util.js +++ b/src/assets/scripts/util.js @@ -187,15 +187,6 @@ export function saveEditorContent(editor, name) { } } -export function isImageIllegal(file) { - if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(file.name)) { - return '请上传 JPG/PNG/GIF 格式的图片'; - } - if (file.size > 5 * 1024 * 1024) { - return '由于公众号限制,图片大小不能超过 5.0M'; - } - return false; -} export function formatDoc(content) { const doc = prettier.format(content, { diff --git a/src/components/CodemirrorEditor/header.vue b/src/components/CodemirrorEditor/header.vue index 4edd7f2..219c87c 100644 --- a/src/components/CodemirrorEditor/header.vue +++ b/src/components/CodemirrorEditor/header.vue @@ -73,11 +73,10 @@ import { downLoadMD, setFontSize, - isImageIllegal, fixCodeWhiteSpace, setColorWithCustomTemplate } from '../../assets/scripts/util' -import fileApi from '../../api/file'; +import {uploadImgFile} from '../../assets/scripts/uploadImageFile'; import { solveWeChatImage, solveHtml @@ -151,24 +150,15 @@ export default { }, // 图片上传前的处理 beforeUpload(file) { - const checkImageResult = isImageIllegal(file); - - if (checkImageResult) { + uploadImgFile(file).then(res=> { + this.$emit('uploaded', res) + }).catch(err=> { this.$message({ showClose: true, - message: checkImageResult, + message: err, type: 'error' }); - return false; - } - let fd = new FormData(); - - fd.append('file', file); - fileApi.fileUpload(fd).then(res => { - this.$emit('uploaded', res) - }).catch(err => { - console.log(err.message) - }) + }); return false; }, // 复制到微信公众号 diff --git a/src/components/CodemirrorEditor/rightClickMenu.vue b/src/components/CodemirrorEditor/rightClickMenu.vue index b8e822d..cd3ec0a 100644 --- a/src/components/CodemirrorEditor/rightClickMenu.vue +++ b/src/components/CodemirrorEditor/rightClickMenu.vue @@ -13,9 +13,8 @@