update: ImgUpload update

This commit is contained in:
JimQing 2020-07-13 20:58:38 +08:00
parent 3940a9e816
commit 4de82b263e
5 changed files with 59 additions and 59 deletions

View File

@ -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;
}

View File

@ -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) { export function formatDoc(content) {
const doc = prettier.format(content, { const doc = prettier.format(content, {

View File

@ -73,11 +73,10 @@
import { import {
downLoadMD, downLoadMD,
setFontSize, setFontSize,
isImageIllegal,
fixCodeWhiteSpace, fixCodeWhiteSpace,
setColorWithCustomTemplate setColorWithCustomTemplate
} from '../../assets/scripts/util' } from '../../assets/scripts/util'
import fileApi from '../../api/file'; import {uploadImgFile} from '../../assets/scripts/uploadImageFile';
import { import {
solveWeChatImage, solveWeChatImage,
solveHtml solveHtml
@ -151,24 +150,15 @@ export default {
}, },
// //
beforeUpload(file) { beforeUpload(file) {
const checkImageResult = isImageIllegal(file); uploadImgFile(file).then(res=> {
if (checkImageResult) {
this.$message({
showClose: true,
message: checkImageResult,
type: 'error'
});
return false;
}
let fd = new FormData();
fd.append('file', file);
fileApi.fileUpload(fd).then(res => {
this.$emit('uploaded', res) this.$emit('uploaded', res)
}).catch(err=> { }).catch(err=> {
console.log(err.message) this.$message({
}) showClose: true,
message: err,
type: 'error'
});
});
return false; return false;
}, },
// //

View File

@ -13,9 +13,8 @@
<script> <script>
import { import {
isImageIllegal, uploadImgFile,
} from '../../assets/scripts/util'; } from '../../assets/scripts/uploadImageFile';
import fileApi from '../../api/file';
export default { export default {
props: { props: {
value: { value: {
@ -60,24 +59,15 @@ export default {
// el-upload // el-upload
// //
beforeUpload(file) { beforeUpload(file) {
const checkImageResult = isImageIllegal(file); uploadImgFile(file).then(res=> {
if (checkImageResult) {
this.$message({
showClose: true,
message: checkImageResult,
type: 'error'
});
return false;
}
let fd = new FormData();
fd.append('file', file);
fileApi.fileUpload(fd).then(res => {
this.$emit('menuTick', 'insertPic', res) this.$emit('menuTick', 'insertPic', res)
}).catch(err=> { }).catch(err=> {
console.log(err.message) this.$message({
}) showClose: true,
message: err,
type: 'error'
});
});
return false; return false;
}, },
}, },

View File

@ -61,7 +61,6 @@
</div> </div>
</template> </template>
<script> <script>
import fileApi from '../api/file';
import editorHeader from '../components/CodemirrorEditor/header'; import editorHeader from '../components/CodemirrorEditor/header';
import aboutDialog from '../components/CodemirrorEditor/aboutDialog'; import aboutDialog from '../components/CodemirrorEditor/aboutDialog';
import insertFormDialog from '../components/CodemirrorEditor/insertForm'; import insertFormDialog from '../components/CodemirrorEditor/insertForm';
@ -70,10 +69,10 @@ import {
css2json, css2json,
downLoadMD, downLoadMD,
setFontSize, setFontSize,
isImageIllegal,
saveEditorContent, saveEditorContent,
customCssWithTemplate customCssWithTemplate
} from '../assets/scripts/util' } from '../assets/scripts/util'
import {uploadImgFile} from '../assets/scripts/uploadImageFile';
require('codemirror/mode/javascript/javascript') require('codemirror/mode/javascript/javascript')
import {mapState, mapMutations} from 'vuex'; import {mapState, mapMutations} from 'vuex';
@ -85,6 +84,7 @@ export default {
dialogFormVisible: false, dialogFormVisible: false,
rightClickMenuVisible: false, rightClickMenuVisible: false,
isCoping: false, isCoping: false,
isImgLoading: false,
backLight: false, backLight: false,
timeout: null, timeout: null,
changeTimer: null, changeTimer: null,
@ -131,31 +131,30 @@ export default {
// //
this.editor.on('paste', (cm, e) => { this.editor.on('paste', (cm, e) => {
if (!(e.clipboardData && e.clipboardData.items)) { if (!(e.clipboardData && e.clipboardData.items) || this.isImgLoading) {
return return;
} }
for (let i = 0, len = e.clipboardData.items.length; i < len; ++i) { for (let i = 0, len = e.clipboardData.items.length; i < len; ++i) {
let item = e.clipboardData.items[i] let item = e.clipboardData.items[i]
if (item.kind === 'file') { if (item.kind === 'file') {
const pasteFile = item.getAsFile() this.isImgLoading = true;
const checkImageResult = isImageIllegal(pasteFile);
if (checkImageResult) {
this.$message({ this.$message({
showClose: true, showClose: true,
message: checkImageResult, message: '正在加载资源'
});
const pasteFile = item.getAsFile()
uploadImgFile(pasteFile).then(res=> {
this.uploaded(res)
this.isImgLoading = false;
}).catch(err=> {
this.$message({
showClose: true,
message: err,
type: 'error' type: 'error'
}); });
return; this.isImgLoading = false;
} });
let data = new FormData()
data.append('file', pasteFile)
fileApi.fileUpload(data).then(res => {
this.uploaded(res)
}).catch(err => {
console.log(err.message)
})
} }
} }
}); });