md/src/components/CodemirrorEditor/uploadImgDialog.vue

315 lines
13 KiB
Vue
Raw Normal View History

2020-08-30 20:21:15 +08:00
<template>
2020-09-13 16:09:31 +08:00
<el-dialog title="本地上传" class="upload__dialog" :visible="value" @close="$emit('close')">
<el-tabs type="card" :value="'upload'">
<el-tab-pane class="upload-panel" label="选择上传" name="upload">
2020-09-15 21:49:30 +08:00
<el-select v-model="imgHost" @change="changeImgHost" placeholder="请选择" size="small">
<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
</el-option>
</el-select>
<el-upload drag action :headers="{'Content-Type': 'multipart/form-data'}" :show-file-list="false"
:multiple="true" accept=".jpg, .jpeg, .png, .gif" name="file" :before-upload="beforeUpload"
v-loading="uploadingImg">
<i class="el-icon-upload"></i>
<div class="el-upload__text">
2020-09-13 16:09:31 +08:00
将图片拖到此处
<em>点击上传</em>
</div>
</el-upload>
</el-tab-pane>
<el-tab-pane class="github-panel" label="GitHub 图床" name="github">
2020-09-13 21:24:27 +08:00
<el-form class="setting-form" ref="form" :model="formGitHub" label-position="right" label-width="140px">
2020-09-07 23:06:43 +08:00
<el-form-item label="GitHub 仓库" :required="true">
2020-09-15 17:42:30 +08:00
<el-input v-model.trim="formGitHub.repo" placeholder="如github.com/yanglbme/resource"></el-input>
</el-form-item>
<el-form-item label="分支">
<el-input v-model.trim="formGitHub.branch" placeholder="如release可不填默认 master"></el-input>
</el-form-item>
<el-form-item label="Token" :required="true">
2020-09-15 17:42:30 +08:00
<el-input v-model.trim="formGitHub.accessToken" show-password
placeholder="如cc1d0c1426d0fd0902bd2d7184b14da61b8abc46"></el-input>
<el-link type="primary"
href="https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token"
target="_blank">如何获取 GitHub Token</el-link>
</el-form-item>
<el-form-item>
2020-09-13 21:24:27 +08:00
<el-button type="primary" @click="saveGitHubConfiguration">保存配置</el-button>
</el-form-item>
</el-form>
</el-tab-pane>
<el-tab-pane class="github-panel" label="阿里云 OSS" name="aliOSS">
<el-form class="setting-form" ref="form" :model="formAliOSS" label-position="right" label-width="140px">
<el-form-item label="AccessKey ID" :required="true">
2020-09-15 17:42:30 +08:00
<el-input v-model.trim="formAliOSS.accessKeyId" placeholder="如LTAI4GdoocsmdoxUf13ylbaNHk"></el-input>
2020-09-13 21:24:27 +08:00
</el-form-item>
<el-form-item label="AccessKey Secret" :required="true">
2020-09-15 17:42:30 +08:00
<el-input v-model.trim="formAliOSS.accessKeySecret" show-password
2020-09-13 21:24:27 +08:00
placeholder="如cc1d0c142doocs0902bd2d7md4b14da6ylbabc46"></el-input>
</el-form-item>
<el-form-item label="Bucket" :required="true">
2020-09-15 17:42:30 +08:00
<el-input v-model.trim="formAliOSS.bucket"
2020-09-13 21:24:27 +08:00
placeholder="如doocs"></el-input>
</el-form-item>
<el-form-item label="Bucket 所在区域" :required="true">
2020-09-15 17:42:30 +08:00
<el-input v-model.trim="formAliOSS.region"
2020-09-13 21:24:27 +08:00
placeholder="如oss-cn-shenzhen"></el-input>
</el-form-item>
<el-form-item label="存储路径">
2020-09-15 17:42:30 +08:00
<el-input v-model.trim="formAliOSS.path"
placeholder="如img可不填默认为根目录"></el-input>
2020-09-13 21:24:27 +08:00
<el-link type="primary"
href="https://help.aliyun.com/document_detail/31883.html"
target="_blank">如何使用阿里云 OSS</el-link>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="saveAliOSSConfiguration">保存配置</el-button>
</el-form-item>
</el-form>
</el-tab-pane>
2020-09-15 21:49:30 +08:00
<el-tab-pane class="github-panel" label="腾讯云 COS" name="txCOS">
<el-form class="setting-form" ref="form" :model="formTxCOS" label-position="right" label-width="140px">
<el-form-item label="SecretId" :required="true">
<el-input v-model.trim="formTxCOS.secretId" placeholder="如AKIDnQp1w3DOOCSs8F5MDp9tdoocsmdUPonW3"></el-input>
</el-form-item>
<el-form-item label="SecretKey" :required="true">
<el-input v-model.trim="formTxCOS.secretKey" show-password
placeholder="如ukLmdtEJ9271f3DOocsMDsCXdS3YlbW0"></el-input>
</el-form-item>
<el-form-item label="Bucket" :required="true">
<el-input v-model.trim="formTxCOS.bucket"
placeholder="如doocs-3212520134"></el-input>
</el-form-item>
<el-form-item label="Bucket 所在区域" :required="true">
<el-input v-model.trim="formTxCOS.region"
placeholder="如ap-guangzhou"></el-input>
</el-form-item>
<el-form-item label="存储路径">
<el-input v-model.trim="formTxCOS.path"
placeholder="如img可不填默认根目录"></el-input>
2020-09-15 21:49:30 +08:00
<el-link type="primary"
href="https://cloud.tencent.com/document/product/436/38484"
target="_blank">如何使用腾讯云 COS</el-link>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="saveTxCOSConfiguration">保存配置</el-button>
</el-form-item>
</el-form>
</el-tab-pane>
</el-tabs>
</el-dialog>
2020-08-30 20:21:15 +08:00
</template>
<script>
import {
uploadImgFile
} from "../../assets/scripts/uploadImageFile";
2020-08-30 20:21:15 +08:00
export default {
props: {
value: {
type: Boolean,
default: false,
2020-08-30 20:21:15 +08:00
},
},
data() {
return {
formGitHub: {
repo: "",
branch: "",
accessToken: "",
},
2020-09-13 21:24:27 +08:00
formAliOSS: {
accessKeyId: "",
accessKeySecret: "",
bucket: "",
region: "",
path: ""
},
2020-09-15 21:49:30 +08:00
formTxCOS: {
secretId: "",
secretKey: "",
bucket: "",
region: "",
path: ""
},
options: [{
value: "default",
label: "默认图床",
},
{
value: "github",
label: "GitHub",
},
2020-09-13 21:24:27 +08:00
{
value: "aliOSS",
label: "阿里云"
2020-09-15 21:49:30 +08:00
},
{
value: "txCOS",
label: "腾讯云"
2020-09-13 21:24:27 +08:00
}
],
imgHost: "default",
uploadingImg: false,
};
2020-08-30 20:21:15 +08:00
},
created() {
2020-09-07 23:06:43 +08:00
if (localStorage.getItem("githubConfig")) {
this.formGitHub = JSON.parse(localStorage.getItem("githubConfig"));
}
2020-09-13 21:24:27 +08:00
if (localStorage.getItem("aliOSSConfig")) {
this.formAliOSS = JSON.parse(localStorage.getItem("aliOSSConfig"));
}
2020-09-15 21:49:30 +08:00
if (localStorage.getItem("txCOSConfig")) {
2020-09-15 21:52:08 +08:00
this.formTxCOS = JSON.parse(localStorage.getItem("txCOSConfig"));
2020-09-15 21:49:30 +08:00
}
2020-09-13 21:24:27 +08:00
if (localStorage.getItem("imgHost")) {
this.imgHost = localStorage.getItem("imgHost");
}
2020-08-30 20:21:15 +08:00
},
methods: {
2020-09-13 21:36:51 +08:00
changeImgHost() {
2020-09-13 21:24:27 +08:00
localStorage.setItem("imgHost", this.imgHost);
2020-09-13 21:36:51 +08:00
this.$message({
showClose: true,
message: '已成功切换图床',
type: "success",
});
},
2020-09-13 21:24:27 +08:00
saveGitHubConfiguration() {
if (!(this.formGitHub.repo && this.formGitHub.accessToken)) {
const blankElement = this.formGitHub.repo ? "token" : "GitHub 仓库"
this.$message({
showClose: true,
message: `参数「​${blankElement}」不能为空`,
type: "error",
});
return;
}
2020-09-07 23:06:43 +08:00
localStorage.setItem("githubConfig", JSON.stringify(this.formGitHub));
this.$message({
message: "保存成功",
type: "success",
});
},
2020-09-13 21:24:27 +08:00
saveAliOSSConfiguration() {
if (!(this.formAliOSS.accessKeyId && this.formAliOSS.accessKeySecret && this.formAliOSS.bucket && this.formAliOSS.region)) {
this.$message({
showClose: true,
message: `阿里云 OSS 参数配置不全`,
type: "error",
});
return;
}
localStorage.setItem("aliOSSConfig", JSON.stringify(this.formAliOSS));
this.$message({
message: "保存成功",
type: "success",
});
},
2020-09-15 21:49:30 +08:00
saveTxCOSConfiguration() {
if (!(this.formTxCOS.secretId && this.formTxCOS.secretKey && this.formTxCOS.bucket && this.formTxCOS.region)) {
this.$message({
showClose: true,
message: `腾讯云 COS 参数配置不全`,
type: "error",
});
return;
}
localStorage.setItem("txCOSConfig", JSON.stringify(this.formTxCOS));
this.$message({
message: "保存成功",
type: "success",
});
},
2020-09-13 21:24:27 +08:00
// 图片上传前的处理
beforeUpload(file) {
if (!this.validateConfig()) {
return;
}
this.uploadingImg = true;
uploadImgFile(file)
2020-09-13 21:24:27 +08:00
.then(res => {
this.$emit("uploaded", res);
this.uploadingImg = false;
})
2020-09-13 21:24:27 +08:00
.catch(err => {
this.uploadingImg = false;
this.$message({
showClose: true,
message: err,
type: "error",
});
});
2020-08-30 20:21:15 +08:00
return false;
},
validateConfig() {
2020-10-13 00:07:14 +08:00
let checkRes = true, errMessage = '';
2020-09-13 21:36:51 +08:00
switch (localStorage.getItem('imgHost')) {
case "github":
2020-10-13 00:07:14 +08:00
checkRes = this.formGitHub.repo && this.formGitHub.accessToken;
errMessage = !checkRes ? '请先配置 GitHub 图床参数' : '';
2020-09-13 21:36:51 +08:00
break;
2020-09-13 21:24:27 +08:00
case 'aliOSS':
2020-10-13 00:07:14 +08:00
checkRes = this.formAliOSS.accessKeyId && this.formAliOSS.accessKeySecret && this.formAliOSS.bucket && this.formAliOSS.region;
errMessage = !checkRes ? '请先配置阿里云 OSS 参数' : '';
2020-09-15 21:49:30 +08:00
break;
case 'txCOS':
2020-10-13 00:07:14 +08:00
checkRes = this.formTxCOS.secretId && this.formTxCOS.secretKey && this.formTxCOS.bucket && this.formTxCOS.region;
errMessage = !checkRes ? '请先配置腾讯云 COS 参数' : '';
2020-09-15 21:49:30 +08:00
break;
default:
}
2020-10-13 00:07:14 +08:00
errMessage && this.$message.error(errMessage);
return checkRes;
},
2020-08-30 20:21:15 +08:00
},
};
</script>
<style lang="less" scoped>
2020-09-07 23:06:43 +08:00
/deep/ .el-dialog {
width: 40%;
}
/deep/ .el-upload-dragger {
width: 335px;
}
/deep/ .el-dialog__body {
padding-bottom: 50px;
}
2020-08-30 20:21:15 +08:00
.upload-panel {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
.el-select {
align-self: flex-end;
margin: 0 67.75px 20px;
width: 100px;
}
2020-08-30 20:21:15 +08:00
}
2020-08-30 20:21:15 +08:00
.github-panel {
display: flex;
justify-content: center;
2020-08-30 20:21:15 +08:00
}
2020-08-30 20:21:15 +08:00
.setting-form {
2020-09-07 23:06:43 +08:00
width: 100%;
.el-form-item {
margin: 15px;
}
.el-form-item:last-child {
text-align: right;
}
2020-08-30 20:21:15 +08:00
}
2020-09-07 23:06:43 +08:00
</style>