From 1626e1764a40582bcf1ddf8525884a9b83cfcdc4 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Wed, 6 Jul 2022 09:28:00 +0800 Subject: [PATCH] fix #117 --- .../src/components/data-render/constant.tsx | 41 ++++++++++++++++ .../src/components/data-render/index.tsx | 48 ++----------------- .../src/components/data-render/loading.tsx | 14 ++---- .../src/components/document/editor/index.tsx | 26 +++++++--- .../src/services/collaboration.service.ts | 7 +++ 5 files changed, 75 insertions(+), 61 deletions(-) create mode 100644 packages/client/src/components/data-render/constant.tsx diff --git a/packages/client/src/components/data-render/constant.tsx b/packages/client/src/components/data-render/constant.tsx new file mode 100644 index 00000000..37847854 --- /dev/null +++ b/packages/client/src/components/data-render/constant.tsx @@ -0,0 +1,41 @@ +import { Spin, Typography } from '@douyinfe/semi-ui'; +import { Empty } from 'illustrations/empty'; +import React, { useMemo } from 'react'; + +const { Text } = Typography; + +export const defaultLoading = ; + +export const defaultRenderError = (error) => { + return {(error && error.message) || '未知错误'}; +}; + +export const defaultEmpty = ( +
+
+ +
+ 暂无数据 +
+); + +export const Render: React.FC<{ fn: ((arg: unknown) => React.ReactNode) | React.ReactNode; args?: unknown[] }> = ({ + fn, + args = [], +}) => { + // eslint-disable-next-line react-hooks/exhaustive-deps + const content = useMemo(() => (typeof fn === 'function' ? fn.apply(null, ...args) : fn), [args]); + + return <>{content}; +}; diff --git a/packages/client/src/components/data-render/index.tsx b/packages/client/src/components/data-render/index.tsx index 4b3c5e3f..db8627e5 100644 --- a/packages/client/src/components/data-render/index.tsx +++ b/packages/client/src/components/data-render/index.tsx @@ -1,7 +1,6 @@ -import { Spin, Typography } from '@douyinfe/semi-ui'; -import { Empty } from 'illustrations/empty'; import React from 'react'; +import { defaultEmpty, defaultLoading, defaultRenderError, Render } from './constant'; import { LoadingWrap } from './loading'; type RenderProps = React.ReactNode | (() => React.ReactNode); @@ -16,40 +15,6 @@ interface IProps { normalContent: RenderProps; } -const { Text } = Typography; - -const defaultLoading = () => { - return ; -}; - -const defaultRenderError = (error) => { - return {(error && error.message) || '未知错误'}; -}; - -const defaultEmpty = () => { - return ( -
-
- -
- 暂无数据 -
- ); -}; - -const runRender = (fn, ...args) => (typeof fn === 'function' ? fn(...args) : fn); - export const DataRender: React.FC = ({ loading, error, @@ -60,19 +25,14 @@ export const DataRender: React.FC = ({ normalContent, }) => { if (error) { - return runRender(errorContent, error); + return ; } if (empty) { - return runRender(emptyContent); + return ; } return ( - + ); }; diff --git a/packages/client/src/components/data-render/loading.tsx b/packages/client/src/components/data-render/loading.tsx index 8940de68..d071d2d3 100644 --- a/packages/client/src/components/data-render/loading.tsx +++ b/packages/client/src/components/data-render/loading.tsx @@ -1,15 +1,9 @@ import { useToggle } from 'hooks/use-toggle'; import React, { useEffect, useRef } from 'react'; -// interface IProps { -// loading: boolean; -// delay?: number; -// runRender -// loadingContent: React.ReactElement; -// normalContent: React.ReactElement; -// } +import { Render } from './constant'; -export const LoadingWrap = ({ loading, delay = 200, runRender, loadingContent, normalContent }) => { +export const LoadingWrap = ({ loading, delay = 200, loadingContent, normalContent }) => { const timer = useRef>(null); const [showLoading, toggleShowLoading] = useToggle(false); @@ -32,8 +26,8 @@ export const LoadingWrap = ({ loading, delay = 200, runRender, loadingContent, n }, [delay, loading, toggleShowLoading]); if (loading) { - return showLoading ? runRender(loadingContent) : null; + return showLoading ? : null; } - return runRender(normalContent); + return ; }; diff --git a/packages/client/src/components/document/editor/index.tsx b/packages/client/src/components/document/editor/index.tsx index f2af666f..3a00816f 100644 --- a/packages/client/src/components/document/editor/index.tsx +++ b/packages/client/src/components/document/editor/index.tsx @@ -122,13 +122,25 @@ export const DocumentEditor: React.FC = ({ documentId }) => { {isMobile &&
{actions}
}
- {docAuthError && ( -
- -
- )} - {document && } - + { + return ( +
+ +
+ ); + }} + normalContent={() => { + return ( + <> + {document && } + {user && } + + ); + }} + />
); diff --git a/packages/server/src/services/collaboration.service.ts b/packages/server/src/services/collaboration.service.ts index 8c89a9b1..f04887cd 100644 --- a/packages/server/src/services/collaboration.service.ts +++ b/packages/server/src/services/collaboration.service.ts @@ -90,6 +90,8 @@ export class CollaborationService { async onAuthenticate({ connection, token, requestParameters }: onAuthenticatePayload) { const targetId = requestParameters.get('targetId'); const docType = requestParameters.get('docType'); + const userId = requestParameters.get('userId'); + const user = token ? await this.userService.decodeToken(token) : null; switch (docType) { @@ -99,8 +101,13 @@ export class CollaborationService { if (!document || document.status !== DocumentStatus.public) { throw new HttpException('您无权查看此文档', HttpStatus.FORBIDDEN); } + connection.readOnly = true; return { user: { name: '匿名用户' } }; } else { + if (user.id !== userId) { + throw new HttpException('用户信息不匹配', HttpStatus.FORBIDDEN); + } + const authority = await this.documentService.getDocumentUserAuth(user.id, targetId); if (!authority.readable) {