refactor: update upload component

This commit is contained in:
yanglbme 2020-12-04 00:57:46 +08:00
parent 2f3bdeadbf
commit 71704264c5
8 changed files with 46 additions and 42 deletions

View File

@ -237,9 +237,9 @@ function fileUpload(content, file) {
case "github": case "github":
return ghFileUpload(content, file.name); return ghFileUpload(content, file.name);
default: default:
// return file.size / 1024 < 1024 return file.size / 1024 < 1024
// ? giteeUpload(content, file.name) ? giteeUpload(content, file.name)
// : ghFileUpload(content, file.name); : ghFileUpload(content, file.name);
return ghFileUpload(content, file.name); return ghFileUpload(content, file.name);
} }
} }

View File

@ -2,12 +2,6 @@ import fileApi from "../../api/file";
export function uploadImgFile(file) { export function uploadImgFile(file) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const checkImageResult = isImageIllegal(file);
if (checkImageResult) {
reject(checkImageResult);
return;
}
const base64Reader = new FileReader(); const base64Reader = new FileReader();
base64Reader.readAsDataURL(file); base64Reader.readAsDataURL(file);
base64Reader.onload = function () { base64Reader.onload = function () {
@ -23,13 +17,3 @@ export function uploadImgFile(file) {
}; };
}); });
} }
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

@ -263,3 +263,10 @@ export function createTable({ data, rows, cols }) {
return table; return table;
} }
export const toBase64 = file => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result.split(",").pop());
reader.onerror = error => reject(error);
});

View File

@ -48,7 +48,7 @@
<i <i
class="el-icon-s-grid" class="el-icon-s-grid"
size="medium" size="medium"
@click="$emit('showDialogForm')" @click="$emit('show-dialog-form')"
></i> ></i>
</el-tooltip> </el-tooltip>
<el-form size="mini" class="ctrl" :inline="true"> <el-form size="mini" class="ctrl" :inline="true">

View File

@ -29,7 +29,8 @@
:multiple="true" :multiple="true"
accept=".jpg, .jpeg, .png, .gif" accept=".jpg, .jpeg, .png, .gif"
name="file" name="file"
:before-upload="beforeUpload" :before-upload="beforeImageUpload"
:http-request="uploadImage"
v-loading="uploadingImg" v-loading="uploadingImg"
> >
<i class="el-icon-upload"></i> <i class="el-icon-upload"></i>
@ -305,6 +306,8 @@
<script> <script>
import { uploadImgFile } from "../../assets/scripts/uploadImageFile"; import { uploadImgFile } from "../../assets/scripts/uploadImageFile";
import { toBase64 } from "../../assets/scripts/util";
import fileApi from "../../api/file";
export default { export default {
props: { props: {
@ -516,30 +519,42 @@ export default {
}); });
}, },
// beforeImageUpload(file) {
beforeUpload(file) { let imgHost = localStorage.getItem("imgHost");
const imgHost = localStorage.getItem("imgHost"); imgHost = !imgHost ? "default" : imgHost;
const config = localStorage.getItem(`${imgHost}Config`);
if (!config && imgHost !== "" && imgHost !== "default") {
this.$message.error(`请先配置 ${imgHost} 图床参数`);
return false;
}
const config = localStorage.getItem(`${imgHost}Config`);
const maxSize = 5;
const isValidSuffix = /\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(
file.name
);
const isLt5M = file.size / 1024 / 1024 <= maxSize;
const isValidHost = imgHost == "default" || config;
if (!isValidSuffix) {
this.$message.error("请上传 JPG/PNG/GIF 格式的图片");
}
if (!isLt5M) {
this.$message.error(
`由于公众号限制,图片大小不能超过 ${maxSize}M`
);
}
if (!isValidHost) {
this.$message.error(`请先配置 ${imgHost} 图床参数`);
}
return isValidSuffix && isLt5M && isValidHost;
},
uploadImage(params) {
this.uploadingImg = true; this.uploadingImg = true;
uploadImgFile(file) uploadImgFile(params.file)
.then((res) => { .then((res) => {
this.$emit("uploaded", res); this.$emit("uploaded", res);
this.uploadingImg = false;
}) })
.catch((err) => { .catch((err) => {
this.uploadingImg = false; this.$message.error(err);
this.$message({
showClose: true,
message: err,
type: "error",
});
}); });
return false; this.uploadingImg = false;
}, },
}, },
}; };

View File

@ -5,7 +5,6 @@ import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css"; import "element-ui/lib/theme-chalk/index.css";
import "./plugins/element"; import "./plugins/element";
import "codemirror/lib/codemirror.css"; import "codemirror/lib/codemirror.css";
import "codemirror/theme/ambiance.css";
import "codemirror/theme/xq-light.css"; import "codemirror/theme/xq-light.css";
import "codemirror/mode/css/css"; import "codemirror/mode/css/css";
import "codemirror/mode/markdown/markdown"; import "codemirror/mode/markdown/markdown";

View File

@ -152,7 +152,6 @@ const mutations = {
}, },
clearEditorToDefault(state) { clearEditorToDefault(state) {
const doc = formatDoc(DEFAULT_CONTENT); const doc = formatDoc(DEFAULT_CONTENT);
state.editor.setValue(doc); state.editor.setValue(doc);
state.cssEditor.setValue(DEFAULT_CSS_CONTENT); state.cssEditor.setValue(DEFAULT_CSS_CONTENT);
}, },

View File

@ -9,7 +9,7 @@
@download="downloadEditorContent" @download="downloadEditorContent"
@showCssEditor="showCssEditor = !showCssEditor" @showCssEditor="showCssEditor = !showCssEditor"
@show-about-dialog="aboutDialogVisible = true" @show-about-dialog="aboutDialogVisible = true"
@showDialogForm="dialogFormVisible = true" @show-dialog-form="dialogFormVisible = true"
@show-dialog-upload-img="dialogUploadImgVisible = true" @show-dialog-upload-img="dialogUploadImgVisible = true"
@startCopy="(isCoping = true), (backLight = true)" @startCopy="(isCoping = true), (backLight = true)"
@endCopy="endCopy" @endCopy="endCopy"