diff --git a/src/adaptors/api/base/metaweblog/metaweblogBlogApi.ts b/src/adaptors/api/base/metaweblog/metaweblogBlogApi.ts new file mode 100644 index 0000000..1a36f23 --- /dev/null +++ b/src/adaptors/api/base/metaweblog/metaweblogBlogApi.ts @@ -0,0 +1,277 @@ +/* + * Copyright (c) 2023, Terwer . All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Terwer designates this + * particular file as subject to the "Classpath" exception as provided + * by Terwer in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com + * or visit www.terwer.space if you need additional information or have any + * questions. + */ + +import { BlogApi, CategoryInfo, Post, PostStatusEnum, UserBlog } from "zhi-blog-api" +import { AppInstance } from "~/src/appInstance.ts" +import { CnblogsConfig } from "~/src/adaptors/api/cnblogs/config/cnblogsConfig.ts" +import { createAppLogger } from "~/src/utils/appLogger.ts" +import { CommonXmlrpcClient } from "zhi-xmlrpc-middleware" +import { MetaweblogConfig } from "~/src/adaptors/api/base/metaweblog/config/MetaweblogConfig.ts" +import { MetaweblogConstants } from "~/src/adaptors/api/base/metaweblog/metaweblogConstants.ts" +import { StrUtil } from "zhi-common" +import { BrowserUtil } from "zhi-device" + +/** + * MetaweblogBlogApi 类继承自 BlogApi 类,并为 Metaweblog API 提供了额外的功能 + * + * @author terwer + * @version 0.9.0 + * @since 0.9.0 + */ +class MetaweblogBlogApi extends BlogApi { + protected readonly cfg: MetaweblogConfig + protected logger + private readonly commonXmlrpcClient + + /** + * 初始化 metaweblog API 适配器 + * + * @param appInstance 应用实例 + * @param cfg 配置项 + */ + constructor(appInstance: AppInstance, cfg: CnblogsConfig) { + super() + + this.cfg = cfg + this.cfg.blogid = "metaweblog" + this.logger = createAppLogger("metaweblog-api-adaptor") + this.commonXmlrpcClient = new CommonXmlrpcClient(appInstance, cfg.apiUrl) + } + + public override async getUsersBlogs(): Promise> { + let result: UserBlog[] = [] + result = await this.metaweblogCall(MetaweblogConstants.METHOD_GET_USERS_BLOGS, [ + this.cfg.blogid, + this.cfg.username, + this.cfg.password, + ]) + this.logger.debug("getUsersBlogs=>", result) + return result + } + + /** + * Not supported + * + * @param keyword + */ + public override async getRecentPostsCount(keyword?: string): Promise { + return 0 + } + + public override async getRecentPosts(numOfPosts: number): Promise { + const result: Post[] = [] + const blogPosts = await this.metaweblogCall(MetaweblogConstants.METHOD_GET_RECENT_POSTS, [ + this.cfg.blogid, + this.cfg.username, + this.cfg.password, + numOfPosts, + ]) + + for (let i = 0; i < blogPosts.length; i++) { + const blogPost = blogPosts[i] + + // 适配公共属性 + const commonPost = new Post() + commonPost.postid = blogPost.postid + commonPost.title = blogPost.title + commonPost.mt_keywords = blogPost.mt_keywords + commonPost.permalink = blogPost.permalink + commonPost.description = blogPost.description + commonPost.wp_slug = blogPost.wp_slug + commonPost.dateCreated = blogPost.dateCreated + commonPost.categories = blogPost.categories + result.push(commonPost) + } + + return result + } + + public async getPost(postid: string): Promise { + const data = await this.metaweblogCall(MetaweblogConstants.METHOD_GET_POST, [ + postid, + this.cfg.username, + this.cfg.password, + ]) + return data + } + + /** + * 新建文章 + * @param post + * @param publish + */ + public async newPost(post: Post, publish: boolean = true): Promise { + // 草稿 + if (!publish) { + post.post_status = PostStatusEnum.PostStatusEnum_Draft + } + + const postStruct = this.createPostStruct(post) + this.logger.debug("postStruct=>", postStruct) + let ret = await this.metaweblogCall(MetaweblogConstants.METHOD_NEW_POST, [ + this.cfg.blogid, + this.cfg.username, + this.cfg.password, + postStruct, + publish, + ]) + ret = ret + "" + ret = ret.replace(/"/g, "") + this.logger.debug("ret=>", ret) + + return ret + } + + public async editPost(postid: string, post: Post, publish: boolean = true): Promise { + // 草稿 + if (!publish) { + post.post_status = PostStatusEnum.PostStatusEnum_Draft + } + + const postStruct = this.createPostStruct(post) + this.logger.debug("postStruct=>", postStruct) + const ret = await this.metaweblogCall(MetaweblogConstants.METHOD_EDIT_POST, [ + postid, + this.cfg.username, + this.cfg.password, + postStruct, + publish, + ]) + this.logger.debug("ret=>", ret) + return ret + } + + public async deletePost(postid: string): Promise { + const ret = await this.metaweblogCall(MetaweblogConstants.METHOD_DELETE_POST, [ + this.cfg.blogid, + postid, + this.cfg.username, + this.cfg.password, + ]) + this.logger.debug("ret=>", ret) + + return ret + } + + public async getCategories(): Promise { + const result = [] as CategoryInfo[] + + try { + const ret = await this.metaweblogCall(MetaweblogConstants.METHOD_GET_CATEGORIES, [ + this.cfg.blogid, + this.cfg.username, + this.cfg.password, + ]) + const dataArr = ret + this.logger.debug("获取的分类信息,dataArr=>", dataArr) + + dataArr.forEach((item: any) => { + const cat = new CategoryInfo() + cat.description = item.description + cat.categoryId = item.categoryId + result.push(cat) + }) + } catch (e) { + this.logger.error("分类获取失败", e) + } + + return result + } + + protected async metaweblogCall(method: string, params: any[]) { + return await this.commonXmlrpcClient.methodCall(method, params, this.cfg.middlewareUrl) + } + + /** + * 适配文章字段 + * @param post 原始文章 + * @private + */ + private createPostStruct(post: Post): object { + const postObj = {} + + if (!StrUtil.isEmptyString(post.title)) { + Object.assign(postObj, { + title: post.title, + }) + } + + if (!StrUtil.isEmptyString(post.mt_keywords)) { + Object.assign(postObj, { + mt_keywords: post.mt_keywords, + }) + } + + if (!StrUtil.isEmptyString(post.description)) { + Object.assign(postObj, { + description: post.description, + }) + } + + if (!StrUtil.isEmptyString(post.wp_slug)) { + Object.assign(postObj, { + wp_slug: post.wp_slug, + }) + } + + // 浏览器端的date转换有问题 + if (!BrowserUtil.isInBrowser) { + Object.assign(postObj, { + // 这里要注意时间格式 + // http://www.ab-weblog.com/en/create-new-posts-with-publishing-date-in-wordpress-using-xml-rpc-and-php/ + // dateCreated: post.dateCreated.toISOString() || new Date().toISOString() + dateCreated: post.dateCreated || new Date(), + }) + } + + Object.assign(postObj, { + categories: post.categories || [], + }) + + Object.assign(postObj, { + post_status: post.post_status ?? PostStatusEnum.PostStatusEnum_Publish, + }) + + if (!StrUtil.isEmptyString(post.wp_password)) { + Object.assign(postObj, { + wp_password: post.wp_password, + }) + } + + return postObj + // return { + // title: post.title || '', + // mt_keywords: post.mt_keywords || '', + // description: post.description || '', + // wp_slug: post.wp_slug || '', + // dateCreated: post.dateCreated.toISOString() || new Date().toISOString(), + // categories: post.categories || [], + // post_status: post.post_status || POST_STATUS_CONSTANTS.POST_STATUS_PUBLISH, + // wp_password: post.wp_password || '' + // } + } +} + +export { MetaweblogBlogApi } diff --git a/src/adaptors/api/base/metaweblog/metaweblogConstants.ts b/src/adaptors/api/base/metaweblog/metaweblogConstants.ts new file mode 100644 index 0000000..de10ff3 --- /dev/null +++ b/src/adaptors/api/base/metaweblog/metaweblogConstants.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023, Terwer . All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Terwer designates this + * particular file as subject to the "Classpath" exception as provided + * by Terwer in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com + * or visit www.terwer.space if you need additional information or have any + * questions. + */ + +/** + * 预定义 Metaweblog 变量 + * + * @author terwer + * @version 0.9.0 + * @since 0.9.0 + */ +class MetaweblogConstants { + public static METHOD_GET_USERS_BLOGS = "metaWeblog.getUsersBlogs" + public static METHOD_NEW_POST = "metaWeblog.newPost" + public static METHOD_EDIT_POST = "metaWeblog.editPost" + public static METHOD_DELETE_POST = "blogger.deletePost" + public static METHOD_GET_RECENT_POSTS = "metaWeblog.getRecentPosts" + public static METHOD_GET_POST = "metaWeblog.getPost" + public static METHOD_GET_CATEGORIES = "metaWeblog.getCategories" +} + +export { MetaweblogConstants } diff --git a/src/adaptors/api/cnblogs/adaptor/cnblogsApiAdaptor.ts b/src/adaptors/api/cnblogs/adaptor/cnblogsApiAdaptor.ts index cf24f55..8d35ac9 100644 --- a/src/adaptors/api/cnblogs/adaptor/cnblogsApiAdaptor.ts +++ b/src/adaptors/api/cnblogs/adaptor/cnblogsApiAdaptor.ts @@ -23,12 +23,12 @@ * questions. */ -import { BlogApi, UserBlog } from "zhi-blog-api" +import { UserBlog } from "zhi-blog-api" import { CnblogsConfig } from "~/src/adaptors/api/cnblogs/config/cnblogsConfig.ts" -import { CommonXmlrpcClient } from "zhi-xmlrpc-middleware" import { AppInstance } from "~/src/appInstance.ts" import { createAppLogger } from "~/src/utils/appLogger.ts" import { CnblogsConstants } from "~/src/adaptors/api/cnblogs/cnblogsConstants.ts" +import { MetaweblogBlogApi } from "~/src/adaptors/api/base/metaweblog/metaweblogBlogApi.ts" /** * 博客园 API 适配器 @@ -39,11 +39,7 @@ import { CnblogsConstants } from "~/src/adaptors/api/cnblogs/cnblogsConstants.ts * @version 0.9.0 * @since 0.9.0 */ -class CnblogsApiAdaptor extends BlogApi { - private readonly logger - private readonly cfg: CnblogsConfig - private readonly commonXmlrpcClient - +class CnblogsApiAdaptor extends MetaweblogBlogApi { /** * 初始化博客园 API 适配器 * @@ -51,24 +47,37 @@ class CnblogsApiAdaptor extends BlogApi { * @param cfg 配置项 */ constructor(appInstance: AppInstance, cfg: CnblogsConfig) { - super() - - this.cfg = cfg + super(appInstance, cfg) this.logger = createAppLogger("cnblogs-api-adaptor") - this.commonXmlrpcClient = new CommonXmlrpcClient(appInstance, cfg.apiUrl) + this.cfg.blogid = "cnblogs" } public override async getUsersBlogs(): Promise> { let result: UserBlog[] = [] - result = await this.cnblogsCall(CnblogsConstants.METHOD_GET_USERS_BLOGS, []) + result = await this.metaweblogCall(CnblogsConstants.METHOD_GET_USERS_BLOGS, [ + this.cfg.blogid, + this.cfg.username, + this.cfg.password, + ]) this.logger.debug("getUsersBlogs=>", result) return result } - private async cnblogsCall(method: string, params: string[]) { - const parameters = ["cnblogs", this.cfg.username, this.cfg.password] - params.forEach((param) => parameters.push(param)) - return await this.commonXmlrpcClient.methodCall(method, parameters, this.cfg.middlewareUrl) + public override async getPreviewUrl(postid: string): Promise { + return this.cfg.previewUrl.replace(/\[postid\]/g, postid) + } + + public override async deletePost(postid: string): Promise { + const ret = await this.metaweblogCall(CnblogsConstants.METHOD_DELETE_POST, [ + this.cfg.blogid, + postid, + this.cfg.username, + this.cfg.password, + false, + ]) + this.logger.debug("ret=>", ret) + + return ret } } export { CnblogsApiAdaptor } diff --git a/src/adaptors/api/cnblogs/cnblogsConstants.ts b/src/adaptors/api/cnblogs/cnblogsConstants.ts index b35f39b..37cdcf9 100644 --- a/src/adaptors/api/cnblogs/cnblogsConstants.ts +++ b/src/adaptors/api/cnblogs/cnblogsConstants.ts @@ -31,9 +31,10 @@ * @since 0.9.0 */ class CnblogsConstants { - // 博客园不支持 metaWeblog.getUsersBlogs - // 博客园只支持 blogger.getUsersBlogs + // 博客园不支持 metaWeblog.getUsersBlogs 只支持 blogger.getUsersBlogs public static METHOD_GET_USERS_BLOGS = "blogger.getUsersBlogs" + // 博客园不支持 metaWeblog.deletePost 只支持 blogger.deletePost + public static METHOD_DELETE_POST = "blogger.deletePost" } export { CnblogsConstants } diff --git a/src/components/set/PublishSetting.vue b/src/components/set/PublishSetting.vue index 66fd57d..08d35ce 100644 --- a/src/components/set/PublishSetting.vue +++ b/src/components/set/PublishSetting.vue @@ -71,6 +71,7 @@ const formData = reactive({ dynamicConfigArray: [] as DynamicConfig[], + webAuthLoadingMap: {} as any, isUpgradeLoading: false, showLogMessage: false, logMessage: "", @@ -255,7 +256,7 @@ const _handleValidateOpenBrowserAuth = (cfg: DynamicConfig) => { const cookieCb = async (dynCfg: DynamicConfig, cookies: ElectronCookie[]) => { // ElMessage.info("验证中,请关注状态,没有授权表示不可用,已授权表示该平台可正常使用...") logger.debug("get cookie result=>", cookies) - dynCfg.isWebAuthLoading = true + formData.webAuthLoadingMap[dynCfg.platformKey] = true try { const appInstance = new AppInstance() @@ -301,14 +302,14 @@ const _handleValidateOpenBrowserAuth = (cfg: DynamicConfig) => { formData.setting[DYNAMIC_CONFIG_KEY] = dynJsonCfg // 更新状态 await updateSetting(formData.setting) - dynCfg.isWebAuthLoading = false + formData.webAuthLoadingMap[dynCfg.platformKey] = false } openBrowserWindow(cfg.authUrl, cfg, cookieCb) } const _handleValidateChromeExtensionAuth = async (cfg: DynamicConfig) => { - cfg.isWebAuthLoading = true + formData.webAuthLoadingMap[cfg.platformKey] = true try { const appInstance = new AppInstance() @@ -342,7 +343,7 @@ const _handleValidateChromeExtensionAuth = async (cfg: DynamicConfig) => { formData.setting[DYNAMIC_CONFIG_KEY] = dynJsonCfg // 更新状态 await updateSetting(formData.setting) - cfg.isWebAuthLoading = false + formData.webAuthLoadingMap[cfg.platformKey] = false } const _handleValidateCookieAuth = async (cfg: DynamicConfig) => { @@ -543,7 +544,7 @@ onMounted(async () => { class="action-btn action-web-auth" @click="handleValidateWebAuth(platform)" :size="'small'" - :loading="platform.isWebAuthLoading" + :loading="formData.webAuthLoadingMap[platform.platformKey] === true" > diff --git a/src/components/set/publish/platform/dynamicConfig.ts b/src/components/set/publish/platform/dynamicConfig.ts index 18392be..9d664ef 100644 --- a/src/components/set/publish/platform/dynamicConfig.ts +++ b/src/components/set/publish/platform/dynamicConfig.ts @@ -67,11 +67,6 @@ export class DynamicConfig { */ isAuth: boolean - /** - * 授权加载中 - */ - isWebAuthLoading?: boolean - /** * 授权模式 */ @@ -162,13 +157,13 @@ export enum SubPlatformType { Common_Yuque = "Yuque", // Github 子平台 - Github_Hugo = "Hugo", Github_Hexo = "Hexo", - Github_Jekyll = "Jekyll", - Github_Vuepress = "Vuepress", - Github_Vitepress = "Vitepress", - Github_Nuxt = "Nuxt", - Github_Next = "Next", + // Github_Hugo = "Hugo", + // Github_Jekyll = "Jekyll", + // Github_Vuepress = "Vuepress", + // Github_Vitepress = "Vitepress", + // Github_Nuxt = "Nuxt", + // Github_Next = "Next", // Metaweblog Metaweblog_Cnblogs = "Cnblogs", @@ -179,10 +174,10 @@ export enum SubPlatformType { // Custom Custom_Zhihu = "Zhihu", - Custom_CSDN = "Csdn", + // Custom_CSDN = "Csdn", Custom_Jianshu = "Jianshu", Custom_Juejin = "Juejin", - Custom_Wechat = "Wechat", + // Custom_Wechat = "Wechat", NONE = "none", } @@ -211,12 +206,12 @@ export function getSubtypeList(ptype: PlatformType): SubPlatformType[] { break case PlatformType.Github: subtypeList.push(SubPlatformType.Github_Hexo) - subtypeList.push(SubPlatformType.Github_Hugo) - subtypeList.push(SubPlatformType.Github_Jekyll) - subtypeList.push(SubPlatformType.Github_Vuepress) - subtypeList.push(SubPlatformType.Github_Vitepress) - subtypeList.push(SubPlatformType.Github_Nuxt) - subtypeList.push(SubPlatformType.Github_Next) + // subtypeList.push(SubPlatformType.Github_Hugo) + // subtypeList.push(SubPlatformType.Github_Jekyll) + // subtypeList.push(SubPlatformType.Github_Vuepress) + // subtypeList.push(SubPlatformType.Github_Vitepress) + // subtypeList.push(SubPlatformType.Github_Nuxt) + // subtypeList.push(SubPlatformType.Github_Next) break case PlatformType.Metaweblog: subtypeList.push(SubPlatformType.Metaweblog_Cnblogs) diff --git a/src/components/test/CnblogsTest.vue b/src/components/test/CnblogsTest.vue index 767c236..5bb363b 100644 --- a/src/components/test/CnblogsTest.vue +++ b/src/components/test/CnblogsTest.vue @@ -29,9 +29,10 @@ import { Utils } from "~/src/utils/utils.ts" import { reactive, ref } from "vue" import { fileToBuffer } from "~/src/utils/polyfillUtils.ts" import { SimpleXmlRpcClient } from "simple-xmlrpc" -import { MediaObject } from "zhi-blog-api" +import {MediaObject, Post} from "zhi-blog-api" import { createAppLogger } from "~/src/utils/appLogger.ts" import Adaptors from "~/src/adaptors" +import {SiYuanApiAdaptor, SiyuanConfig} from "zhi-siyuan-api"; const logger = createAppLogger("cnblogs-test") @@ -127,13 +128,13 @@ const onMethodChange = (val: string) => { } case METHOD_GET_POST: { params.value = JSON.stringify({ - postid: "20230526221603-3mgotyw", + postid: "17593464", }) break } case METHOD_EDIT_POST: { params.value = JSON.stringify({ - postid: "20230527202519-k09a4gx", + postid: "17593464", post: { title: "自动发布的测试标题2", description: "自动发布的测试内容2", @@ -146,7 +147,7 @@ const onMethodChange = (val: string) => { } case METHOD_DELETE_POST: { params.value = JSON.stringify({ - postid: "20230528192554-mlcxbe8", + postid: "17593463", }) break } @@ -156,7 +157,7 @@ const onMethodChange = (val: string) => { } case METHOD_GET_PREVIEW_URL: { params.value = JSON.stringify({ - postid: "20230528192554-mlcxbe8", + postid: "17243968", }) break } @@ -200,27 +201,99 @@ const cnblogsHandleApi = async () => { break } case METHOD_GET_RECENT_POSTS_COUNT: { + const key = "metaweblog_Cnblogs" + const cnblogsApiAdaptor = await Adaptors.getAdaptor(key) + const cnblogsApi = Utils.blogApi(appInstance, cnblogsApiAdaptor) + logger.info("cnblogsApi=>", cnblogsApi) + const result = await cnblogsApi.getRecentPostsCount() + logMessage.value = JSON.stringify(result) + logger.info("cnblogs getRecentPostsCount=>", result) break } case METHOD_GET_RECENT_POSTS: { + const key = "metaweblog_Cnblogs" + const cnblogsApiAdaptor = await Adaptors.getAdaptor(key) + const cnblogsApi = Utils.blogApi(appInstance, cnblogsApiAdaptor) + logger.info("cnblogsApi=>", cnblogsApi) + const result = await cnblogsApi.getRecentPosts(10) + logMessage.value = JSON.stringify(result) + logger.info("cnblogs getRecentPosts=>", result) break } case METHOD_NEW_POST: { + const paramsValue = JSON.parse(params.value) + let post = new Post() + post = { + ...post, + ...paramsValue, + } + const key = "metaweblog_Cnblogs" + const cnblogsApiAdaptor = await Adaptors.getAdaptor(key) + const cnblogsApi = Utils.blogApi(appInstance, cnblogsApiAdaptor) + const result = await cnblogsApi.newPost(post) + logMessage.value = JSON.stringify(result) + logger.info("cnblogs newPost=>", result) break } case METHOD_GET_POST: { + const key = "metaweblog_Cnblogs" + const cnblogsApiAdaptor = await Adaptors.getAdaptor(key) + const cnblogsApi = Utils.blogApi(appInstance, cnblogsApiAdaptor) + logger.info("cnblogsApi=>", cnblogsApi) + const paramsValue = JSON.parse(params.value) + const postid = paramsValue.postid + const result = await cnblogsApi.getPost(postid) + logMessage.value = JSON.stringify(result) + logger.info("cnblogs getPost=>", result) break } case METHOD_EDIT_POST: { + const key = "metaweblog_Cnblogs" + const cnblogsApiAdaptor = await Adaptors.getAdaptor(key) + const cnblogsApi = Utils.blogApi(appInstance, cnblogsApiAdaptor) + logger.info("cnblogsApi=>", cnblogsApi) + const paramsValue = JSON.parse(params.value) + const postid = paramsValue.postid + let post = new Post() + post = { + ...post, + ...paramsValue.post, + } + const result = await cnblogsApi.editPost(postid, post) + logMessage.value = JSON.stringify(result) break } case METHOD_DELETE_POST: { + const key = "metaweblog_Cnblogs" + const cnblogsApiAdaptor = await Adaptors.getAdaptor(key) + const cnblogsApi = Utils.blogApi(appInstance, cnblogsApiAdaptor) + logger.info("cnblogsApi=>", cnblogsApi) + const paramsValue = JSON.parse(params.value) + const postid = paramsValue.postid + const result = await cnblogsApi.deletePost(postid) + logMessage.value = JSON.stringify(result) break } case METHOD_GET_CATEGORIES: { + const key = "metaweblog_Cnblogs" + const cnblogsApiAdaptor = await Adaptors.getAdaptor(key) + const cnblogsApi = Utils.blogApi(appInstance, cnblogsApiAdaptor) + logger.info("cnblogsApi=>", cnblogsApi) + const result = await cnblogsApi.getCategories() + logMessage.value = JSON.stringify(result) + logger.info("cnblogs getCategories=>", result) break } case METHOD_GET_PREVIEW_URL: { + const key = "metaweblog_Cnblogs" + const cnblogsApiAdaptor = await Adaptors.getAdaptor(key) + const cnblogsApi = Utils.blogApi(appInstance, cnblogsApiAdaptor) + logger.info("cnblogsApi=>", cnblogsApi) + const paramsValue = JSON.parse(params.value) + const postid = paramsValue.postid + const result = await cnblogsApi.getPreviewUrl(postid) + logMessage.value = JSON.stringify(result) + logger.info("cnblogs getPreviewUrl=>", result) break } case METHOD_NEW_MEDIA_OBJECT: { diff --git a/src/utils/import/pre.ts b/src/utils/import/pre.ts index a4e6f59..b003bc5 100644 --- a/src/utils/import/pre.ts +++ b/src/utils/import/pre.ts @@ -99,7 +99,7 @@ export const pre = { domain: "zhihu.com", isEnabled: false, }, - // CSDN 目前有CA验证 + // // CSDN 目前有CA验证 // { // platformType: PlatformType.Custom, // subPlatformType: SubPlatformType.Custom_CSDN, diff --git a/src/workers/quickPublish.vue b/src/workers/quickPublish.vue index 43c1d9b..74045c9 100644 --- a/src/workers/quickPublish.vue +++ b/src/workers/quickPublish.vue @@ -28,12 +28,22 @@ import { onMounted, reactive } from "vue" import { useRoute } from "vue-router" import { useVueI18n } from "~/src/composables/useVueI18n.ts" import { createAppLogger } from "~/src/utils/appLogger.ts" +import Adaptors from "~/src/adaptors" +import { Utils } from "~/src/utils/utils.ts" +import { AppInstance } from "~/src/appInstance.ts" +import { Post } from "zhi-blog-api" +import { useSettingStore } from "~/src/stores/useSettingStore.ts" +import { JsonUtil, StrUtil } from "zhi-common" +import { SypConfig } from "~/syp.config.ts" +import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts" const logger = createAppLogger("quick-publish-worker") // uses const { t } = useVueI18n() const route = useRoute() +const { getSetting, updateSetting } = useSettingStore() +const { kernelApi, blogApi } = useSiyuanApi() // datas const params = reactive(route.params) @@ -43,14 +53,71 @@ const formData = reactive({ isPublishLoading: false, publishStatus: false, errMsg: "", + + setting: {} as typeof SypConfig, + cfg: {} as any, + postid: "", + previewUrl: "", }) const doPublish = async () => { try { + // 加载配置 + formData.setting = await getSetting() + formData.cfg = JsonUtil.safeParse(formData.setting[key], {} as any) + + // 思源笔记原始文章数据 + const doc = await blogApi.getPost(id) + + // 初始化API + const appInstance = new AppInstance() + const apiAdaptor = await Adaptors.getAdaptor(key) + const api = Utils.blogApi(appInstance, apiAdaptor) + logger.info("api=>", api) + + // 检测是否发布 + const posidKey = formData.cfg.posidKey + if (StrUtil.isEmptyString(posidKey)) { + throw new Error("配置错误,posidKey不能为空,请检查配置") + } + const postMeta = formData.setting[id] ?? {} + formData.postid = postMeta[posidKey] ?? "" + + if (StrUtil.isEmptyString(formData.postid)) { + logger.info("文章未发布,准备发布") + const post = new Post() + post.title = doc.title + post.description = doc.description + // result 正常情况下就是 postid + const result = await api.newPost(post) + + // 写入postid到配置 + formData.postid = result + postMeta[posidKey] = formData.postid + formData.setting[id] = postMeta + await updateSetting(formData.setting) + logger.info("new post=>", result) + } else { + logger.info("文章已发布,准备更新") + const post = new Post() + post.title = doc.title + post.description = doc.description + // result 正常情况下就是 postid + const result = await api.editPost(formData.postid, post) + logger.info("edit post=>", result) + } + const previewUrl = await api.getPreviewUrl(formData.postid) + formData.previewUrl = `${formData.cfg.home}${previewUrl}` + formData.publishStatus = true } catch (e) { formData.errMsg = t("main.opt.failure") + "=>" + e logger.error(e) + // ElMessage.error(formData.errMsg) + await kernelApi.pushErrMsg({ + msg: formData.errMsg, + timeout: 7000, + }) formData.publishStatus = false } } @@ -68,13 +135,22 @@ onMounted(async () => {
- + + 发布中,请稍后...:
- 发布到 [博客园] 成功, 查看文章 + {{ StrUtil.isEmptyString(formData.postid) ? "发布到" : "更新文章到" }} [博客园] 成功, + 查看文章 +
+
+ {{ StrUtil.isEmptyString(formData.postid) ? "发布到" : "更新文章到" }} [博客园] 失败!
-
发布到 [博客园] 失败,异常如下:{{ formData.errMsg }}