Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:#18 实现metaweblog-api客户端-通用设置模板 #52

Merged
merged 5 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ dist-ssr
.vercel
*.private.env.json
__snapshots__
test/data/
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@types/express": "^4.17.13",
"@types/node": "^18.6.2",
"@vitejs/plugin-vue": "^3.0.0",
"node-localstorage": "^2.2.1",
"typescript": "^4.6.4",
"vite": "^3.0.0",
"vite-plugin-require-transform": "^1.0.3",
Expand Down
177 changes: 176 additions & 1 deletion src/components/tab/setting/CommonBlogSetting.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,186 @@
<template>
<el-form label-width="120px">
<el-form-item :label="$t('setting.blog.url')">
<el-alert class="top-version-tip" :title="apiTypeInfo + blogName" type="info"
:closable="false"/>
<el-form-item :label="$t('setting.common.home')">
<el-input v-model="home"/>
</el-form-item>

<el-form-item :label="$t('setting.common.username')" v-if="props.usernameEnabled">
<el-input v-model="username"/>
</el-form-item>

<el-form-item :label="$t('setting.common.password')" v-if="props.passwordEnabled">
<el-input type="password" v-model="password" show-password/>
<a :href="tokenSettingUrl" target="_blank">{{ $t('setting.common.username.gen') }}:{{ tokenSettingUrl }}</a>
</el-form-item>

<el-form-item :label="$t('setting.common.token')" v-if="props.tokenEnabled">
<el-input type="password" v-model="token" show-password/>
<a :href="tokenSettingUrl" target="_blank">{{ $t('setting.common.token.gen') }}:{{ tokenSettingUrl }}</a>
</el-form-item>

<el-form-item :label="$t('setting.common.apiurl')">
<el-input v-model="apiUrl"/>
</el-form-item>

<el-form-item>
<el-button type="primary" @click="valiConf" :loading="isLoading">
{{ isLoading ? $t('setting.blog.vali.ing') : $t('setting.blog.vali') }}
</el-button>
<el-alert :title="$t('setting.blog.vali.tip.metaweblog')" type="warning" :closable="false" v-if="!apiStatus"/>
<el-alert :title="$t('setting.blog.vali.ok.metaweblog')" type="success" :closable="false" v-if="apiStatus"/>
</el-form-item>

<el-form-item>
<el-button type="primary" @click="saveConf">{{ $t('setting.blog.save') }}</el-button>
<el-button>{{ $t('setting.blog.cancel') }}</el-button>
</el-form-item>
</el-form>
</template>

<script lang="ts" setup>

import {CommonblogCfg, ICommonblogCfg} from "../../../lib/platform/commonblog/commonblogCfg";
import {onMounted, ref} from "vue";
import {useI18n} from "vue-i18n";
import logUtil from "../../../lib/logUtil";
import {getJSONConf, setJSONConf} from "../../../lib/config";
import {ElMessage} from "element-plus";
import {isEmptyObject} from "../../../lib/util";
import {API} from "../../../lib/api";
import {UserBlog} from "../../../lib/common/userBlog";

const {t} = useI18n()

const props = defineProps({
isReload: {
type: Boolean,
default: false
},
apiType: {
type: String,
default: ""
},
cfg: {
type: CommonblogCfg,
default: null
},
usernameEnabled: {
type: Boolean,
default: false
},
passwordEnabled: {
type: Boolean,
default: false
},
tokenEnabled: {
type: Boolean,
default: false
}
})

const home = ref("")
const apiUrl = ref("")
const username = ref("")
const password = ref("")
const token = ref("")

const isLoading = ref(false)
const apiStatus = ref(false)
const blogName = ref("")
const tokenSettingUrl = ref("")

const apiTypeInfo = ref(t('setting.blog.platform.support.common') + props.apiType + " ")

const valiConf = async () => {
isLoading.value = true;

try {
// 先保存
saveConf(true)

const cfg = getJSONConf<ICommonblogCfg>(props.apiType)
const api = new API(props.apiType)
const usersBlogs: Array<UserBlog> = await api.getUsersBlogs()
if (usersBlogs && usersBlogs.length > 0) {
const userBlog = usersBlogs[0]

cfg.apiStatus = true
apiStatus.value = true

cfg.blogName = userBlog.blogName
blogName.value = userBlog.blogName

// 验证通过刷新状态
setJSONConf(props.apiType, cfg)
} else {
cfg.apiStatus = false
apiStatus.value = false

// 验证失败刷新状态
setJSONConf(props.apiType, cfg)
}
} catch (e) {
console.error(e)
}

if (!apiStatus.value) {
ElMessage.error(t('setting.blog.vali.error'))
} else {
ElMessage.success(t('main.opt.success'))
}

isLoading.value = false

logUtil.logInfo("通用Setting验证完毕")
}

const saveConf = (hideTip?: boolean) => {
logUtil.logInfo("Metaweblog通用Setting保存配置")

const cfg = props.cfg
cfg.home = home.value
cfg.username = username.value
cfg.password = password.value
cfg.token = token.value
cfg.apiUrl = apiUrl.value
cfg.apiStatus = apiStatus.value
cfg.blogName = blogName.value

setJSONConf(props.apiType, cfg)

if (hideTip != true) {
ElMessage.success(t('main.opt.success'))
}
}

const initConf = () => {
logUtil.logInfo("通用Setting配置初始化")
let conf = getJSONConf<ICommonblogCfg>(props.apiType)
// 如果没有配置。读取默认配置
if (isEmptyObject(conf)) {
conf = props.cfg
}
if (conf) {
logUtil.logInfo("get setting conf=>", conf)

home.value = conf.home || ""
apiUrl.value = conf.apiUrl || ""
username.value = conf.username || ""
password.value = conf.password || ""
apiStatus.value = conf.apiStatus || false
blogName.value = conf.blogName || ""
tokenSettingUrl.value = conf.tokenSettingUrl || ""
}
}

onMounted(async () => {
// 初始化
initConf()
})
</script>

<script lang="ts">
export default {
name: "CommonBlogSetting"
Expand Down
6 changes: 3 additions & 3 deletions src/components/tab/setting/MetaweblogSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ const valiConf = async () => {

isLoading.value = false

logUtil.logInfo("通用Setting验证完毕")
logUtil.logInfo("Metaweblog通用Setting验证完毕")
}

const saveConf = (hideTip?: boolean) => {
logUtil.logInfo("通用Setting保存配置")
logUtil.logInfo("Metaweblog通用Setting保存配置")

const cfg = props.cfg
cfg.home = home.value
Expand All @@ -136,7 +136,7 @@ const saveConf = (hideTip?: boolean) => {
}

const initConf = () => {
logUtil.logInfo("通用Setting配置初始化")
logUtil.logInfo("Metaweblog通用Setting配置初始化")
let conf = getJSONConf<IMetaweblogCfg>(props.apiType)
// 如果没有配置。读取默认配置
if (isEmptyObject(conf)) {
Expand Down
13 changes: 12 additions & 1 deletion src/components/tab/setting/commonsettingadaptor/KmsSetting.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<template>
<common-blog-setting/>
<common-blog-setting :api-type="apiType" :username-enabled="true" :password-enabled="true" :cfg="cfg"/>
</template>

<script lang="ts" setup>
import {ref} from "vue";
import {API_TYPE_CONSTANTS} from "../../../../lib/constants/apiTypeConstants";
import {KmsCfg} from "../../../../lib/platform/commonblog/kms/kmsCfg";

const apiType = ref(API_TYPE_CONSTANTS.API_TYPE_KMS)
const kmsCfg = new KmsCfg()
const cfg = ref(kmsCfg)

</script>

<script lang="ts">
import CommonBlogSetting from "../CommonBlogSetting.vue";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<template>
<common-blog-setting/>
<common-blog-setting :api-type="apiType" :token-enabled="true" :cfg="cfg"/>
</template>

<script lang="ts" setup>
import {ref} from "vue";
import {API_TYPE_CONSTANTS} from "../../../../lib/constants/apiTypeConstants";
import {LiandiCfg} from "../../../../lib/platform/commonblog/liandi/liandiCfg";

const apiType = ref(API_TYPE_CONSTANTS.API_TYPE_LIANDI)
const liandiCfg = new LiandiCfg()
const cfg = ref(liandiCfg)

</script>

<script lang="ts">
import CommonBlogSetting from "../CommonBlogSetting.vue";

Expand Down
13 changes: 12 additions & 1 deletion src/components/tab/setting/commonsettingadaptor/YuqueSetting.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<template>
<common-blog-setting/>
<common-blog-setting :api-type="apiType" :token-enabled="true" :cfg="cfg"/>
</template>

<script lang="ts" setup>
import {ref} from "vue";
import {API_TYPE_CONSTANTS} from "../../../../lib/constants/apiTypeConstants";
import {YuqueCfg} from "../../../../lib/platform/commonblog/yuque/yuqueCfg";

const apiType = ref(API_TYPE_CONSTANTS.API_TYPE_YUQUE)
const yuqueCfg = new YuqueCfg()
const cfg = ref(yuqueCfg)

</script>

<script lang="ts">
import CommonBlogSetting from "../CommonBlogSetting.vue";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {ref} from "vue";
import {CnblogsCfg} from "../../../../lib/platform/metaweblog/config/cnblogsCfg";

const apiType = ref(API_TYPE_CONSTANTS.API_TYPE_CNBLOGS)
const cfg = ref(new CnblogsCfg())
const cnblogsCfg = new CnblogsCfg()
const cfg = ref(cnblogsCfg)

</script>

Expand Down
32 changes: 20 additions & 12 deletions src/lib/platform/commonblog/commonblogApiAdaptor.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
import {IApi} from "../../api";
import {Post} from "../../common/post";
import {UserBlog} from "../../common/userBlog";
import {getJSONConf} from "../../config";
import {ICommonblogCfg} from "./commonblogCfg";

/**
* 通用平台接口适配器
*/
export class CommonblogApiAdaptor implements IApi {
deletePost(postid: string): Promise<boolean> {
return Promise.resolve(false);
protected cfg: ICommonblogCfg

constructor(apiType: string) {
this.cfg = getJSONConf<ICommonblogCfg>(apiType)
}

public async deletePost(postid: string): Promise<boolean> {
throw new Error("该功能未实现,请在子类重写改方法")
}

editPost(postid: string, post: Post, publish?: boolean): Promise<boolean> {
return Promise.resolve(false);
public async editPost(postid: string, post: Post, publish?: boolean): Promise<boolean> {
throw new Error("该功能未实现,请在子类重写改方法")
}

getPost(postid: string, useSlug?: boolean): Promise<Post> {
return Promise.resolve(new Post());
public async getPost(postid: string, useSlug?: boolean): Promise<Post> {
throw new Error("该功能未实现,请在子类重写改方法")
}

getRecentPosts(numOfPosts: number, page?: number, keyword?: string): Promise<Array<Post>> {
return Promise.resolve([]);
public async getRecentPosts(numOfPosts: number, page?: number, keyword?: string): Promise<Array<Post>> {
throw new Error("该功能未实现,请在子类重写改方法")
}

getUsersBlogs(): Promise<Array<UserBlog>> {
return Promise.resolve([]);
public async getUsersBlogs(): Promise<Array<UserBlog>> {
throw new Error("该功能未实现,请在子类重写改方法")
}

newPost(post: Post, publish?: boolean): Promise<string> {
return Promise.resolve("");
public async newPost(post: Post, publish?: boolean): Promise<string> {
throw new Error("该功能未实现,请在子类重写改方法")
}
}
Loading