diff --git a/packages/client/src/data/wiki.tsx b/packages/client/src/data/wiki.tsx index ff2a09d..ebb9bca 100644 --- a/packages/client/src/data/wiki.tsx +++ b/packages/client/src/data/wiki.tsx @@ -156,29 +156,6 @@ export const useAllPublicWikis = () => { }; }; -/** - * 获取知识库首页文档 - * @returns - */ -export const getWikiHomeDocument = (wikiId, cookie = null): Promise => { - return HttpClient.request({ - method: WikiApiDefinition.getHomeDocumentById.method, - url: WikiApiDefinition.getHomeDocumentById.client(wikiId), - cookie, - }); -}; - -/** - * 获取知识库首页文档 - * @returns - */ -export const useWikiHomeDocument = (wikiId) => { - const { data, error, isLoading } = useQuery(WikiApiDefinition.getHomeDocumentById.client(wikiId), () => - getWikiHomeDocument(wikiId) - ); - return { data, error, loading: isLoading }; -}; - /** * 获取知识库详情 * @param wikiId diff --git a/packages/client/src/pages/wiki/[wikiId]/index.tsx b/packages/client/src/pages/wiki/[wikiId]/index.tsx index 3d73e0c..61de53e 100644 --- a/packages/client/src/pages/wiki/[wikiId]/index.tsx +++ b/packages/client/src/pages/wiki/[wikiId]/index.tsx @@ -1,8 +1,9 @@ +import { Spin } from '@douyinfe/semi-ui'; import { IWiki, WikiApiDefinition } from '@think/domains'; import { DataRender } from 'components/data-render'; import { DocumentReader } from 'components/document/reader'; import { WikiTocs } from 'components/wiki/tocs'; -import { getWikiDetail, getWikiHomeDocument, getWikiTocs, useWikiHomeDocument } from 'data/wiki'; +import { getWikiDetail, getWikiTocs, useWikiDetail } from 'data/wiki'; import { DoubleColumnLayout } from 'layouts/double-column'; import { NextPage } from 'next'; import React from 'react'; @@ -13,7 +14,7 @@ interface IProps { } const Page: NextPage = ({ wikiId }) => { - const { data: doc, loading, error } = useWikiHomeDocument(wikiId); + const { data: wiki, loading, error } = useWikiDetail(wikiId); return ( = ({ wikiId }) => { rightNode={ + + + } error={error} - normalContent={() => } + normalContent={() => } /> } - > + /> ); }; Page.getInitialProps = async (ctx) => { const { wikiId } = ctx.query; const res = await serverPrefetcher(ctx, [ - { - url: WikiApiDefinition.getHomeDocumentById.client(wikiId as IWiki['id']), - action: (cookie) => getWikiHomeDocument(cookie), - }, { url: WikiApiDefinition.getDetailById.client(wikiId as IWiki['id']), action: (cookie) => getWikiDetail(wikiId, cookie), diff --git a/packages/domains/lib/models/wiki.d.ts b/packages/domains/lib/models/wiki.d.ts index f9a1c35..47ac6e6 100644 --- a/packages/domains/lib/models/wiki.d.ts +++ b/packages/domains/lib/models/wiki.d.ts @@ -1,4 +1,5 @@ import { IUser } from './user'; +import { IDocument } from './document'; /** * 知识库状态枚举 */ @@ -32,6 +33,7 @@ export interface IWiki { createUserId: IUser['id']; createUser: IUser; status: WikiStatus; + homeDocumentId: IDocument['id']; createdAt: Date; updatedAt: Date; } diff --git a/packages/domains/src/models/wiki.ts b/packages/domains/src/models/wiki.ts index 13819cf..b198743 100644 --- a/packages/domains/src/models/wiki.ts +++ b/packages/domains/src/models/wiki.ts @@ -1,4 +1,5 @@ import { IUser } from './user'; +import { IDocument } from './document'; /** * 知识库状态枚举 @@ -36,6 +37,7 @@ export interface IWiki { createUserId: IUser['id']; createUser: IUser; status: WikiStatus; + homeDocumentId: IDocument['id']; createdAt: Date; updatedAt: Date; } diff --git a/packages/server/src/entities/wiki.entity.ts b/packages/server/src/entities/wiki.entity.ts index 02842b3..c3861cb 100644 --- a/packages/server/src/entities/wiki.entity.ts +++ b/packages/server/src/entities/wiki.entity.ts @@ -23,6 +23,9 @@ export class WikiEntity { @Column({ type: 'varchar', comment: '创建用户 Id' }) public createUserId: string; + @Column({ type: 'varchar', comment: '知识库首页文档Id', default: '' }) + public homeDocumentId: string; + @Column({ type: 'enum', enum: WikiStatus, diff --git a/packages/server/src/services/wiki.service.ts b/packages/server/src/services/wiki.service.ts index 44480c5..e23ffd0 100644 --- a/packages/server/src/services/wiki.service.ts +++ b/packages/server/src/services/wiki.service.ts @@ -13,7 +13,7 @@ import { MessageService } from '@services/message.service'; import { UserService } from '@services/user.service'; import { OutUser } from '@services/user.service'; import { ViewService } from '@services/view.service'; -import { DocumentStatus, IPagination, WikiStatus, WikiUserRole } from '@think/domains'; +import { CollectType, DocumentStatus, IPagination, WikiStatus, WikiUserRole } from '@think/domains'; import { instanceToPlain } from 'class-transformer'; import * as lodash from 'lodash'; import { Repository } from 'typeorm'; @@ -41,7 +41,26 @@ export class WikiService { @Inject(forwardRef(() => ViewService)) private readonly viewService: ViewService - ) {} + ) { + this.fixWikiData(); + } + + // 修正脚本 + async fixWikiData() { + const wikis = await this.wikiRepo.find(); + const needFixWikis = wikis.filter((wiki) => !wiki.homeDocumentId); + + await Promise.all( + needFixWikis.map(async (wiki) => { + const doc = await this.documentService.findWikiHomeDocument(wiki.id); + const homeDocumentId = doc.id; + const withHomeDocumentIdWiki = await this.wikiRepo.merge(wiki, { homeDocumentId }); + await this.wikiRepo.save(withHomeDocumentIdWiki); + }) + ); + + console.log('修正完成'); + } /** * 按 id 查取知识库 @@ -310,16 +329,23 @@ export class WikiService { targetUserRole: WikiUserRole.admin, }); // 知识库首页文档 - await this.documentService.createDocument( - user, - { - wikiId: wiki.id, - parentDocumentId: null, - title: wiki.name, - }, - true - ); - return wiki; + const [doc] = await Promise.all([ + await this.documentService.createDocument( + user, + { + wikiId: wiki.id, + parentDocumentId: null, + title: wiki.name, + }, + true + ), + await this.collectorService.toggleStar(user, { type: CollectType.wiki, targetId: wiki.id }), + ]); + const homeDocumentId = doc.id; + const withHomeDocumentIdWiki = await this.wikiRepo.merge(wiki, { homeDocumentId }); + await this.wikiRepo.save(withHomeDocumentIdWiki); + + return withHomeDocumentIdWiki; } /**