From 1849c36ef957e588798ca69928fadfa47f77d2b0 Mon Sep 17 00:00:00 2001 From: terwer Date: Sun, 4 Sep 2022 20:05:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:#51=20=E6=94=AF=E6=8C=81=E5=B1=95=E7=A4=BA?= =?UTF-8?q?Wordpress=E5=88=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/themes/default/defaultHeader.tsx | 5 +-- components/themes/default/defaultLayout.tsx | 8 +++-- components/themes/default/defaultNavbar.tsx | 18 +++++++++- lib/api.ts | 18 +++++++--- lib/common/categoryInfo.ts | 23 +++++++++++++ lib/common/post.ts | 4 +-- lib/constants/metaweblogMethodConstants.ts | 4 ++- lib/metaweblog/metaWeblogApi.ts | 37 +++++++++++++++++++-- lib/metaweblog/metaWeblogApiAdaptor.ts | 13 ++++++++ lib/siyuan/siYuanApiAdaptor.ts | 5 +++ pages/index.tsx | 13 +++++--- 11 files changed, 129 insertions(+), 19 deletions(-) create mode 100644 lib/common/categoryInfo.ts diff --git a/components/themes/default/defaultHeader.tsx b/components/themes/default/defaultHeader.tsx index 463d8151..3f08e33f 100644 --- a/components/themes/default/defaultHeader.tsx +++ b/components/themes/default/defaultHeader.tsx @@ -3,12 +3,13 @@ import DefaultNavbar from "./defaultNavbar"; import SiteConfig from "../../../lib/common/siteconfig"; import Image from "next/image"; import headerStyles from "./css/header.module.css" +import {CategoryInfo} from "../../../lib/common/categoryInfo"; const getTitle = (webname: string, webslogen: string) => { return webname + " - " + webslogen } -export default function DefaultHeader({props, keyword, type}: { props: SiteConfig, keyword?: string, type: string }) { +export default function DefaultHeader({props, keyword, type, cats}: { props: SiteConfig, keyword?: string, type: string, cats?: CategoryInfo[] }) { return ( <> @@ -25,7 +26,7 @@ export default function DefaultHeader({props, keyword, type}: { props: SiteConfi data-recalc-dims="1"/> - + ) diff --git a/components/themes/default/defaultLayout.tsx b/components/themes/default/defaultLayout.tsx index 9337880d..b2aa2ceb 100644 --- a/components/themes/default/defaultLayout.tsx +++ b/components/themes/default/defaultLayout.tsx @@ -4,6 +4,7 @@ import DefaultHeader from "./defaultHeader"; import DefaultFooter from "./defaultFooter"; import {Alert, Container, Row} from "react-bootstrap"; import SiteConfig from "../../../lib/common/siteconfig"; +import {CategoryInfo} from "../../../lib/common/categoryInfo"; /** * 默认布局 @@ -15,13 +16,14 @@ export default function DefaultLayout({ props, keyword, children, - type - }: { props: SiteConfig, keyword?: string, children: any, type: string }) { + type, + cats + }: { props: SiteConfig, keyword?: string, children: any, type: string, cats?: CategoryInfo[] }) { return ( <> - + diff --git a/components/themes/default/defaultNavbar.tsx b/components/themes/default/defaultNavbar.tsx index 0d43ce42..c96c2aee 100644 --- a/components/themes/default/defaultNavbar.tsx +++ b/components/themes/default/defaultNavbar.tsx @@ -8,8 +8,14 @@ import {useState} from "react"; import Image from "next/image"; import {isEmptyString} from "../../../lib/util"; import {API_TYPE_CONSTANTS} from "../../../lib/constants"; +import {CategoryInfo} from "../../../lib/common/categoryInfo"; -export default function DefaultNavbar({props, keyword, type}: { props: SiteConfig, keyword?: string, type: string }) { +export default function DefaultNavbar({ + props, + keyword, + type, + cats + }: { props: SiteConfig, keyword?: string, type: string, cats?: CategoryInfo[] }) { let [value, setValue] = useState("") @@ -24,6 +30,16 @@ export default function DefaultNavbar({props, keyword, type}: { props: SiteConfi {props.webname}/ + + + { false &&
diff --git a/lib/api.ts b/lib/api.ts index 706e75f5..869df0d7 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -6,6 +6,7 @@ import {CnblogsApiAdaptor} from "./metaweblog/cnblogsApiAdaptor"; import {Post} from "./common/post"; import {UserBlog} from "./common/userBlog"; import {WordpressApiAdaptor} from "./metaweblog/WordpressApiAdaptor"; +import {CategoryInfo} from "./common/categoryInfo"; export interface IApi { /** @@ -27,6 +28,11 @@ export interface IApi { * @param useSlug 是否使用的是别名(可选,部分平台不支持) */ getPost(postid: string, useSlug?: boolean): Promise + + /** + * 获取分类列表 + */ + getCategories(): Promise } export class API implements IApi { @@ -57,15 +63,19 @@ export class API implements IApi { } async getRecentPosts(numOfPosts: number, page?: number, keyword?: string): Promise> { - return this.apiAdaptor.getRecentPosts(numOfPosts, page, keyword); + return await this.apiAdaptor.getRecentPosts(numOfPosts, page, keyword); } async getUsersBlogs(): Promise> { - return this.apiAdaptor.getUsersBlogs(); + return await this.apiAdaptor.getUsersBlogs(); + } + + async getPost(postid: string, useSlug?: boolean): Promise { + return await this.apiAdaptor.getPost(postid, useSlug); } - getPost(postid: string, useSlug?: boolean): Promise { - return this.apiAdaptor.getPost(postid, useSlug); + async getCategories(): Promise { + return await this.apiAdaptor.getCategories() } } diff --git a/lib/common/categoryInfo.ts b/lib/common/categoryInfo.ts new file mode 100644 index 00000000..42fec4c9 --- /dev/null +++ b/lib/common/categoryInfo.ts @@ -0,0 +1,23 @@ +/** + * 通用分类模型定义 + */ +export class CategoryInfo { + categoryId: string + parentId: string + description: string + categoryDescription: string + categoryName: string + htmlUrl: string + rssUrl: string + + + constructor() { + this.categoryId = "" + this.parentId = "0" + this.description = "" + this.categoryDescription = "" + this.categoryName = "" + this.htmlUrl = "" + this.rssUrl = "" + } +} \ No newline at end of file diff --git a/lib/common/post.ts b/lib/common/post.ts index 0e0e0bb6..71dd89ad 100644 --- a/lib/common/post.ts +++ b/lib/common/post.ts @@ -1,8 +1,8 @@ +import {POST_STATUS_CONSTANTS} from "../constants/postStatusConstants"; + /** * 通用文章模型定义 */ -import {POST_STATUS_CONSTANTS} from "../constants/postStatusConstants"; - export class Post { postid: string title: string diff --git a/lib/constants/metaweblogMethodConstants.ts b/lib/constants/metaweblogMethodConstants.ts index 72bb6e21..2b10e76c 100644 --- a/lib/constants/metaweblogMethodConstants.ts +++ b/lib/constants/metaweblogMethodConstants.ts @@ -4,6 +4,7 @@ const EDIT_POST = "metaWeblog.editPost" const DELETE_POST = "blogger.deletePost" const GET_RECENT_POSTS = "metaWeblog.getRecentPosts" const GET_POST = "metaWeblog.getPost" +const GET_CATEGORIES = "metaWeblog.getCategories" export const METAWEBLOG_METHOD_CONSTANTS = { GET_USERS_BLOGS, @@ -11,5 +12,6 @@ export const METAWEBLOG_METHOD_CONSTANTS = { EDIT_POST, DELETE_POST, GET_RECENT_POSTS, - GET_POST + GET_POST, + GET_CATEGORIES } \ No newline at end of file diff --git a/lib/metaweblog/metaWeblogApi.ts b/lib/metaweblog/metaWeblogApi.ts index cdb9d90d..0734aa88 100644 --- a/lib/metaweblog/metaWeblogApi.ts +++ b/lib/metaweblog/metaWeblogApi.ts @@ -5,7 +5,7 @@ import logUtil from "../logUtil"; import {METAWEBLOG_METHOD_CONSTANTS} from "../constants/metaweblogMethodConstants"; import {POST_STATUS_CONSTANTS} from "../constants/postStatusConstants"; import {inBrowser, isEmptyString} from "../util"; -import {render} from "../markdownUtil"; +import {CategoryInfo} from "../common/categoryInfo"; export class MetaWeblogApi { private readonly apiType: string @@ -121,7 +121,7 @@ export class MetaWeblogApi { result.push(post) } } - + logUtil.logInfo("result=>", result) return result } @@ -284,4 +284,37 @@ export class MetaWeblogApi { // wp_password: post.wp_password || '' // } } + + public async getCategories(blogid: string, username: string, password: string,): Promise { + let result = >[] + + let ret = await this.xmlrpcClient.methodCallEntry(METAWEBLOG_METHOD_CONSTANTS.GET_CATEGORIES, + [this.apiType, username, password]) + logUtil.logInfo("getCategories ret=>", ret) + + // 错误处理 + this.doFault(ret) + + let catArray = ret.params.param.value.array.data.value || [] + catArray.forEach((item: any) => { + const cat = this.parseCat(item) + result.push(cat) + }) + + return result + } + + private parseCat(catStruct: any): CategoryInfo { + const cat = new CategoryInfo() + const catObj = catStruct.struct.member + cat.categoryId = this.parseFieldValue(catObj, "categoryId") + cat.parentId = this.parseFieldValue(catObj, "parentId") + cat.description = this.parseFieldValue(catObj, "description") + cat.categoryDescription = this.parseFieldValue(catObj, "categoryDescription") + cat.categoryName = this.parseFieldValue(catObj, "categoryName") + cat.htmlUrl = this.parseFieldValue(catObj, "htmlUrl") + cat.rssUrl = this.parseFieldValue(catObj, "rssUrl") + + return cat + } } \ No newline at end of file diff --git a/lib/metaweblog/metaWeblogApiAdaptor.ts b/lib/metaweblog/metaWeblogApiAdaptor.ts index 69503c65..8dd9b415 100644 --- a/lib/metaweblog/metaWeblogApiAdaptor.ts +++ b/lib/metaweblog/metaWeblogApiAdaptor.ts @@ -5,6 +5,7 @@ import {mdToHtml, mdToPlainText, parseHtml, removeTitleNumber} from "../htmlUtil import {CONSTANTS} from "../constants"; import logUtil from "../logUtil"; import {isEmptyString} from "../util"; +import {CategoryInfo} from "../common/categoryInfo"; /** * 博客园的API适配器 @@ -116,4 +117,16 @@ export class MetaWeblogApiAdaptor implements IApi { return commonPost; } + + /** + * getCategories + * https://codex.wordpress.org/XML-RPC_MetaWeblog_API#metaWeblog.getCategories + * + * @returns {Promise} + */ + public async getCategories(): Promise { + const cats = await this.metaWeblog.getCategories(this.appkey, this.username, this.password) + logUtil.logInfo("获取分类列表=>", cats) + return cats + } } \ No newline at end of file diff --git a/lib/siyuan/siYuanApiAdaptor.ts b/lib/siyuan/siYuanApiAdaptor.ts index 2852ff7e..51223680 100644 --- a/lib/siyuan/siYuanApiAdaptor.ts +++ b/lib/siyuan/siYuanApiAdaptor.ts @@ -6,6 +6,7 @@ import {API_TYPE_CONSTANTS} from "../constants"; import logUtil from "../logUtil"; import {render} from "../markdownUtil"; import {mdToHtml, removeTitleNumber, removeWidgetTag} from "../htmlUtil"; +import {CategoryInfo} from "../common/categoryInfo"; /** * 思源笔记API适配器 @@ -117,4 +118,8 @@ export class SiYuanApiAdaptor implements IApi { return commonPost } + + public async getCategories(): Promise { + return Promise.resolve([]) + } } \ No newline at end of file diff --git a/pages/index.tsx b/pages/index.tsx index 9b55a137..ec2932ca 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -6,16 +6,18 @@ import DefaultLayout from "../components/themes/default/defaultLayout"; import SiteConfig from "../lib/common/siteconfig"; import DefaultHomePostList from "../components/themes/default/defaultHomePostList"; import {assginPreviewUrlForPosts, getHomelink, isEmptyString} from "../lib/util"; +import {CategoryInfo} from "../lib/common/categoryInfo"; type Props = { type: string, layoutCfg: SiteConfig, - posts: Post[] + posts: Post[], + cats: CategoryInfo[] } const Home: NextPage = (props, context) => { return ( - + ) @@ -72,14 +74,17 @@ export const getServerSideProps: GetServerSideProps = async (context) => } else { result = await api.getRecentPosts(10) } - assginPreviewUrlForPosts(type, result) + // 分类 + const cats = await api.getCategories() + return { props: { type: type, layoutCfg: JSON.parse(JSON.stringify(cfg)), - posts: JSON.parse(JSON.stringify(result)) + posts: JSON.parse(JSON.stringify(result)), + cats: JSON.parse(JSON.stringify(cats)) } } }