refactor: update uploadImg component

This commit is contained in:
yanglbme 2020-12-04 20:47:28 +08:00
parent 71704264c5
commit 186026afa1
5 changed files with 94 additions and 146 deletions

View File

@ -1,19 +0,0 @@
import fileApi from "../../api/file";
export function uploadImgFile(file) {
return new Promise((resolve, reject) => {
const base64Reader = new FileReader();
base64Reader.readAsDataURL(file);
base64Reader.onload = function () {
const base64Content = this.result.split(",").pop();
fileApi
.fileUpload(base64Content, file)
.then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
};
});
}

View File

@ -19,7 +19,6 @@
</template> </template>
<script> <script>
import { uploadImgFile } from "../../assets/scripts/uploadImageFile";
export default { export default {
props: { props: {
value: { value: {

View File

@ -31,7 +31,6 @@
name="file" name="file"
:before-upload="beforeImageUpload" :before-upload="beforeImageUpload"
:http-request="uploadImage" :http-request="uploadImage"
v-loading="uploadingImg"
> >
<i class="el-icon-upload"></i> <i class="el-icon-upload"></i>
<div class="el-upload__text"> <div class="el-upload__text">
@ -305,9 +304,6 @@
</template> </template>
<script> <script>
import { uploadImgFile } from "../../assets/scripts/uploadImageFile";
import { toBase64 } from "../../assets/scripts/util";
import fileApi from "../../api/file";
export default { export default {
props: { props: {
@ -315,9 +311,14 @@ export default {
type: Boolean, type: Boolean,
default: false, default: false,
}, },
a: {
type: Boolean,
default: false,
}
}, },
data() { data() {
return { return {
checkResult: false,
formGitHub: { formGitHub: {
repo: "", repo: "",
branch: "", branch: "",
@ -378,9 +379,13 @@ export default {
}, },
], ],
imgHost: "default", imgHost: "default",
uploadingImg: false,
}; };
}, },
watch: {
a: function(newVal, oldVal) {
this.checkResult = newVal;
}
},
created() { created() {
if (localStorage.getItem("githubConfig")) { if (localStorage.getItem("githubConfig")) {
this.formGitHub = JSON.parse(localStorage.getItem("githubConfig")); this.formGitHub = JSON.parse(localStorage.getItem("githubConfig"));
@ -401,50 +406,32 @@ export default {
methods: { methods: {
changeImgHost() { changeImgHost() {
localStorage.setItem("imgHost", this.imgHost); localStorage.setItem("imgHost", this.imgHost);
this.$message({ this.$message.success("已成功切换图床");
showClose: true,
message: "已成功切换图床",
type: "success",
});
}, },
saveGitHubConfiguration() { saveGitHubConfiguration() {
if (!(this.formGitHub.repo && this.formGitHub.accessToken)) { if (!(this.formGitHub.repo && this.formGitHub.accessToken)) {
const blankElement = this.formGitHub.repo const blankElement = this.formGitHub.repo
? "token" ? "token"
: "GitHub 仓库"; : "GitHub 仓库";
this.$message({ this.$message.error( `参数「​${blankElement}」不能为空`);
showClose: true,
message: `参数「​${blankElement}」不能为空`,
type: "error",
});
return; return;
} }
localStorage.setItem( localStorage.setItem(
"githubConfig", "githubConfig",
JSON.stringify(this.formGitHub) JSON.stringify(this.formGitHub)
); );
this.$message({ this.$message.success("保存成功");
message: "保存成功",
type: "success",
});
}, },
saveGiteeConfiguration() { saveGiteeConfiguration() {
if (!(this.formGitee.repo && this.formGitee.accessToken)) { if (!(this.formGitee.repo && this.formGitee.accessToken)) {
const blankElement = this.formGitee.repo const blankElement = this.formGitee.repo
? "私人令牌" ? "私人令牌"
: "Gitee 仓库"; : "Gitee 仓库";
this.$message({ this.$message.error(`参数「​${blankElement}」不能为空`);
showClose: true,
message: `参数「​${blankElement}」不能为空`,
type: "error",
});
return; return;
} }
localStorage.setItem("giteeConfig", JSON.stringify(this.formGitee)); localStorage.setItem("giteeConfig", JSON.stringify(this.formGitee));
this.$message({ this.$message.success("保存成功");
message: "保存成功",
type: "success",
});
}, },
saveAliOSSConfiguration() { saveAliOSSConfiguration() {
if ( if (
@ -455,21 +442,14 @@ export default {
this.formAliOSS.region this.formAliOSS.region
) )
) { ) {
this.$message({ this.$message.error(`阿里云 OSS 参数配置不全`);
showClose: true,
message: `阿里云 OSS 参数配置不全`,
type: "error",
});
return; return;
} }
localStorage.setItem( localStorage.setItem(
"aliOSSConfig", "aliOSSConfig",
JSON.stringify(this.formAliOSS) JSON.stringify(this.formAliOSS)
); );
this.$message({ this.$message.success("保存成功");
message: "保存成功",
type: "success",
});
}, },
saveTxCOSConfiguration() { saveTxCOSConfiguration() {
@ -481,18 +461,11 @@ export default {
this.formTxCOS.region this.formTxCOS.region
) )
) { ) {
this.$message({ this.$message.error(`腾讯云 COS 参数配置不全`);
showClose: true,
message: `腾讯云 COS 参数配置不全`,
type: "error",
});
return; return;
} }
localStorage.setItem("txCOSConfig", JSON.stringify(this.formTxCOS)); localStorage.setItem("txCOSConfig", JSON.stringify(this.formTxCOS));
this.$message({ this.$message.success("保存成功");
message: "保存成功",
type: "success",
});
}, },
saveQiniuConfiguration() { saveQiniuConfiguration() {
@ -505,56 +478,20 @@ export default {
this.formQiniu.region this.formQiniu.region
) )
) { ) {
this.$message({ this.$message.error(`七牛云 Kodo 参数配置不全`);
showClose: true,
message: `七牛云 Kodo 参数配置不全`,
type: "error",
});
return; return;
} }
localStorage.setItem("qiniuConfig", JSON.stringify(this.formQiniu)); localStorage.setItem("qiniuConfig", JSON.stringify(this.formQiniu));
this.$message({ this.$message.success("保存成功");
message: "保存成功",
type: "success",
});
}, },
beforeImageUpload(file) { beforeImageUpload(file) {
let imgHost = localStorage.getItem("imgHost"); this.$emit("beforeUpload", file);
imgHost = !imgHost ? "default" : imgHost; console.log(this.checkResult);
return this.checkResult;
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) { uploadImage(params) {
this.uploadingImg = true; return this.$emit("uploadImage", params.file);
uploadImgFile(params.file)
.then((res) => {
this.$emit("uploaded", res);
})
.catch((err) => {
this.$message.error(err);
});
this.uploadingImg = false;
}, },
}, },
}; };

View File

@ -2,6 +2,7 @@ import Vue from "vue";
import App from "./App.vue"; import App from "./App.vue";
import store from "./store"; import store from "./store";
import ElementUI from "element-ui"; import ElementUI from "element-ui";
import { Message} 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";
@ -16,6 +17,7 @@ import "codemirror/addon/hint/css-hint.js";
import "./assets/less/theme.less"; import "./assets/less/theme.less";
Vue.use(ElementUI); Vue.use(ElementUI);
Vue.use(Message);
Vue.config.productionTip = false; Vue.config.productionTip = false;

View File

@ -73,7 +73,10 @@
<upload-img-dialog <upload-img-dialog
v-model="dialogUploadImgVisible" v-model="dialogUploadImgVisible"
@close="dialogUploadImgVisible = false" @close="dialogUploadImgVisible = false"
@beforeUpload="beforeUpload"
@uploadImage="uploadImage"
@uploaded="uploaded" @uploaded="uploaded"
:a = "a"
/> />
<about-dialog v-model="aboutDialogVisible" /> <about-dialog v-model="aboutDialogVisible" />
<insert-form-dialog v-model="dialogFormVisible" /> <insert-form-dialog v-model="dialogFormVisible" />
@ -101,7 +104,9 @@ import {
saveEditorContent, saveEditorContent,
customCssWithTemplate, customCssWithTemplate,
} from "../assets/scripts/util"; } from "../assets/scripts/util";
import { uploadImgFile } from "../assets/scripts/uploadImageFile";
import { toBase64 } from "../assets/scripts/util";
import fileApi from "../api/file";
require("codemirror/mode/javascript/javascript"); require("codemirror/mode/javascript/javascript");
import { mapState, mapMutations } from "vuex"; import { mapState, mapMutations } from "vuex";
@ -120,6 +125,7 @@ export default {
source: "", source: "",
mouseLeft: 0, mouseLeft: 0,
mouseTop: 0, mouseTop: 0,
a: false,
}; };
}, },
components: { components: {
@ -174,37 +180,14 @@ export default {
++i ++i
) { ) {
let item = e.clipboardData.items[i]; let item = e.clipboardData.items[i];
if (item.kind === "file") { if (item.kind === "file") {
// //
const imgHost = const pasteFile = item.getAsFile();
localStorage.getItem("imgHost") || "default"; const isValid = this.beforeUpload(pasteFile);
if ( if (!isValid) {
imgHost != "default" &&
!localStorage.getItem(`${imgHost}Config`)
) {
this.$message({
showClose: true,
message: "请先配置好图床参数",
type: "error",
});
continue; continue;
} }
this.uploadImage(pasteFile);
this.isImgLoading = true;
const pasteFile = item.getAsFile();
uploadImgFile(pasteFile)
.then((res) => {
this.uploaded(res);
})
.catch((err) => {
this.$message({
showClose: true,
message: err,
type: "error",
});
});
this.isImgLoading = false;
} }
} }
}); });
@ -243,14 +226,64 @@ export default {
}); });
this.onEditorRefresh(); this.onEditorRefresh();
}, },
beforeUpload(file) {
// check filename suffix
const isValidSuffix = /\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(
file.name
);
if (!isValidSuffix) {
this.$message.error("请上传 JPG/PNG/GIF 格式的图片");
this.a = false;
return;
}
// check file size
const maxSize = 5;
const isLt5M = file.size / 1024 / 1024 <= maxSize;
if (!isLt5M) {
this.$message.error(
`由于公众号限制,图片大小不能超过 ${maxSize}M`
);
this.a = false;
return;
}
// check image host
let imgHost = localStorage.getItem("imgHost");
imgHost = imgHost ? imgHost : "default";
localStorage.setItem("imgHost", imgHost);
const config = localStorage.getItem(`${imgHost}Config`);
const isValidHost = imgHost == "default" || config;
if (!isValidHost) {
this.$message.error(`请先配置 ${imgHost} 图床参数`);
this.a = false;
return;
}
this.a = true;
},
uploadImage(file) {
this.isImgLoading = true;
toBase64(file)
.then((base64Content) => {
fileApi
.fileUpload(base64Content, file)
.then((url) => {
this.uploaded(url);
})
.catch((err) => {
this.$message.error(err.message);
});
})
.catch((err) => {
this.$message.error(err.message);
});
this.isImgLoading = false;
},
// //
uploaded(response) { uploaded(response) {
if (!response) { if (!response) {
this.$message({ this.$message.error("上传图片未知异常");
showClose: true,
message: "上传图片未知异常",
type: "error",
});
return; return;
} }
this.dialogUploadImgVisible = false; this.dialogUploadImgVisible = false;
@ -260,11 +293,7 @@ export default {
const markdownImage = `![](${imageUrl})`; const markdownImage = `![](${imageUrl})`;
// Markdown URL // Markdown URL
this.editor.replaceSelection(`\n${markdownImage}\n`, cursor); this.editor.replaceSelection(`\n${markdownImage}\n`, cursor);
this.$message({ this.$message.success("图片上传成功");
showClose: true,
message: "图片上传成功",
type: "success",
});
this.onEditorRefresh(); this.onEditorRefresh();
}, },
// //