feat: update configuration

This commit is contained in:
yanglbme 2020-08-31 20:48:22 +08:00
parent f7e845be12
commit e80b769e12
3 changed files with 49 additions and 40 deletions

View File

@ -1,5 +1,7 @@
import fetch from './fetch'; import fetch from './fetch';
import { v4 as uuidv4 } from 'uuid'; import {
v4 as uuidv4
} from 'uuid';
const fileUploadConfig = { const fileUploadConfig = {
username: 'filess', username: 'filess',
repo: 'images', repo: 'images',
@ -15,32 +17,41 @@ const fileUploadConfig = {
] ]
} }
function getConfiguration() {
const imgHost = localStorage.getItem("ImgHost") || 'default'
// default
let token = fileUploadConfig.accessToken[Math.floor(Math.random() * fileUploadConfig.accessToken.length)].replace('doocsmd', '')
let username = fileUploadConfig.username
let repo = fileUploadConfig.repo
// GitHub
if (imgHost === 'github' && localStorage.getItem("GitHubConfig")) {
const githubConfg = JSON.parse(localStorage.getItem("GitHubConfig"))
const repoUrl = githubConfg.repo.replace("https://github.com/", "").replace("http://github.com/", "").replace("github.com/", "").split("/")
token = githubConfg.accessToken
username = repoUrl[0]
repo = repoUrl[1]
}
return {
username,
repo,
token
}
}
function fileUpload(content, filename) { function fileUpload(content, filename) {
const date = new Date(); const date = new Date();
const dir = date.getFullYear() + '/' + (date.getMonth() + 1).toString().padStart(2, '0') + '/' + date.getDate().toString().padStart(2, '0'); const dir = date.getFullYear() + '/' + (date.getMonth() + 1).toString().padStart(2, '0') + '/' + date.getDate().toString().padStart(2, '0');
const uuid = uuidv4(); const uuid = uuidv4();
const dateFilename = new Date().getTime() + '-' + uuid + '.' + filename.split('.')[1]; const dateFilename = new Date().getTime() + '-' + uuid + '.' + filename.split('.')[1];
const imgHost = localStorage.getItem("ImgHost") || 'default' const {
username,
let token = '' repo,
let username = '' token
let repo = '' } = getConfiguration()
const url = `https://api.github.com/repos/${username}/${repo}/contents/${dir}/${dateFilename}`
if (imgHost === 'default') {
token = fileUploadConfig.accessToken[Math.floor(Math.random() * fileUploadConfig.accessToken.length)].replace('doocsmd', '');
username = fileUploadConfig.username
repo = fileUploadConfig.repo
}
if (imgHost === 'github' && localStorage.getItem("GitHubConfig")) {
const githubConfg = JSON.parse(localStorage.getItem("GitHubConfig"));
token = githubConfg.accessToken
username = githubConfg.username
repo = githubConfg.repo
}
const url = `https://api.github.com/repos/${username}/${repo}/contents/${dir}/${dateFilename}`;
return fetch({ return fetch({
url, url,
@ -58,4 +69,4 @@ function fileUpload(content, filename) {
export default { export default {
fileUpload fileUpload
}; };

View File

@ -179,7 +179,6 @@ export function css2json(css) {
*/ */
export function saveEditorContent(editor, name) { export function saveEditorContent(editor, name) {
const content = editor.getValue(0) const content = editor.getValue(0)
if (content) { if (content) {
localStorage.setItem(name, content) localStorage.setItem(name, content)
} else { } else {
@ -198,7 +197,6 @@ export function formatDoc(content) {
export function fixCodeWhiteSpace(value = 'pre') { export function fixCodeWhiteSpace(value = 'pre') {
const preDomList = document.getElementsByClassName('code__pre'); const preDomList = document.getElementsByClassName('code__pre');
if (preDomList.length > 0) { if (preDomList.length > 0) {
preDomList.forEach(pre => { preDomList.forEach(pre => {
pre.style.whiteSpace = value; pre.style.whiteSpace = value;
@ -217,4 +215,4 @@ export function downLoadMD(doc) {
document.body.appendChild(downLink); document.body.appendChild(downLink);
downLink.click(); downLink.click();
document.body.removeChild(downLink); document.body.removeChild(downLink);
} }

View File

@ -26,7 +26,6 @@
将文件拖到此处 将文件拖到此处
<em>点击上传</em> <em>点击上传</em>
</div> </div>
<!-- <div class="el-upload__tip" slot="tip">只能上传jpg/png文件且不超过500kb</div> -->
</el-upload> </el-upload>
</el-tab-pane> </el-tab-pane>
<el-tab-pane class="github-panel" label="GitHub 图床" name="github"> <el-tab-pane class="github-panel" label="GitHub 图床" name="github">
@ -35,21 +34,15 @@
ref="form" ref="form"
:model="formGitHub" :model="formGitHub"
label-position="right" label-position="right"
label-width="80px" label-width="100px"
> >
<el-form-item label="用户名"> <el-form-item label="GitHub 仓库">
<el-input v-model="formGitHub.username" placeholder="如yanglbme"></el-input> <el-input v-model="formGitHub.repo" placeholder="如github.com/yanglbme/resource"></el-input>
</el-form-item>
<el-form-item label="仓库名">
<el-input v-model="formGitHub.repo" placeholder="如resource"></el-input>
</el-form-item> </el-form-item>
<el-form-item label="token"> <el-form-item label="token">
<el-input v-model="formGitHub.accessToken" placeholder="如cc1d0c1426d0fd0902bd2d7184b14da61b8abc46"></el-input> <el-input v-model="formGitHub.accessToken" 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-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>
<!-- <el-form-item label="jsDelivr CDN:">
<el-checkbox v-model="formGitHub.useCDN"></el-checkbox>
</el-form-item>-->
<el-form-item> <el-form-item>
<el-button type="primary" @click="onSubmit">保存配置</el-button> <el-button type="primary" @click="onSubmit">保存配置</el-button>
</el-form-item> </el-form-item>
@ -72,10 +65,8 @@ export default {
data() { data() {
return { return {
formGitHub: { formGitHub: {
username: "",
repo: "", repo: "",
accessToken: "", accessToken: "",
// useCDN: true,
}, },
options: [ options: [
{ {
@ -105,8 +96,17 @@ export default {
localStorage.setItem("ImgHost", this.imgHost); localStorage.setItem("ImgHost", this.imgHost);
}, },
onSubmit() { onSubmit() {
console.log("submit github params:", this.formGitHub); if (!(this.formGitHub.repo && this.formGitHub.accessToken)) {
const blankElement = this.formGitHub.repo ? "token" : "GitHub 仓库"
this.$message({
showClose: true,
message: `参数「​${blankElement}」不能为空`,
type: "error",
});
return;
}
localStorage.setItem("GitHubConfig", JSON.stringify(this.formGitHub)); localStorage.setItem("GitHubConfig", JSON.stringify(this.formGitHub));
console.log("submit github params:", this.formGitHub);
this.$message({ this.$message({
message: "保存成功", message: "保存成功",
type: "success", type: "success",
@ -137,8 +137,8 @@ export default {
validateConfig() { validateConfig() {
switch (this.imgHost) { switch (this.imgHost) {
case "github": case "github":
const { username, repo, accessToken } = this.formGitHub; const { repo, accessToken } = this.formGitHub;
if (!username || !repo || !accessToken) { if (!repo || !accessToken) {
this.$message.error("未配置 GitHub 参数"); this.$message.error("未配置 GitHub 参数");
return false; return false;
} }