From f8bfb3f9277250bcf39bd8db48538e1ef5c8cd76 Mon Sep 17 00:00:00 2001 From: visiky <736929286@qq.com> Date: Mon, 14 Mar 2022 09:02:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=A6=96=E6=AC=A1?= =?UTF-8?q?=E8=BF=9B=E5=85=A5=EF=BC=8C=E6=B2=A1=E6=9C=89=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E7=AE=80=E5=8E=86=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/index.tsx | 52 ++++++++++++++++++++--------------- src/helpers/fetch-resume.ts | 22 +++++++++++++++ src/helpers/store-to-local.ts | 27 ++++++------------ 3 files changed, 60 insertions(+), 41 deletions(-) create mode 100644 src/helpers/fetch-resume.ts diff --git a/src/components/index.tsx b/src/components/index.tsx index 438ee6e..06966df 100644 --- a/src/components/index.tsx +++ b/src/components/index.tsx @@ -11,6 +11,7 @@ import { copyToClipboard } from '@/helpers/copy-to-board'; import { getDevice } from '@/helpers/detect-device'; import { exportDataToLocal } from '@/helpers/export-to-local'; import { getConfig, saveToLocalStorage } from '@/helpers/store-to-local'; +import { fetchResume } from '@/helpers/fetch-resume'; import { Drawer } from './Drawer'; import { Resume } from './Resume'; import { ResumeConfig, ThemeConfig } from './types'; @@ -49,31 +50,38 @@ export const Page: React.FC = () => { const user = (query.user || '') as string; const branch = (query.branch || 'master') as string; const mode = query.mode; + + function store(data) { + originalConfig.current = data; + changeConfig( + _.omit(customAssign({}, data, _.get(data, ['locales', lang])), [ + 'locales', + ]) + ); + updateLoading(false); + } + if (!mode) { const link = `https://github.com/${user}/${user}/tree/${branch}`; - Modal.info({ - title: i18n.get('获取简历信息失败'), - content: ( -
- 请检查用户名 {user} 是否正确或者简历信息是否在 - {`${link}/resume.json`}下 -
- ), - okText: i18n.get('进入在线编辑'), - onOk: () => { - changeMode('edit'); - }, - }); + fetchResume(lang, branch, user) + .then(data => store(data)) + .catch(() => { + Modal.info({ + title: i18n.get('获取简历信息失败'), + content: ( +
+ 请检查用户名 {user} 是否正确或者简历信息是否在 + {`${link}/resume.json`}下 +
+ ), + okText: i18n.get('进入在线编辑'), + onOk: () => { + changeMode('edit'); + }, + }); + }); } else { - getConfig(lang, branch, user).then(data => { - originalConfig.current = data; - changeConfig( - _.omit(customAssign({}, data, _.get(data, ['locales', lang])), [ - 'locales', - ]) - ); - updateLoading(false); - }); + getConfig(lang, branch, user).then(data => store(data)); } }, [lang, query.user, query.branch]); diff --git a/src/helpers/fetch-resume.ts b/src/helpers/fetch-resume.ts new file mode 100644 index 0000000..0b5eb61 --- /dev/null +++ b/src/helpers/fetch-resume.ts @@ -0,0 +1,22 @@ +import fetch from 'cross-fetch'; +import _ from "lodash"; +import { ResumeConfig } from "@/components/types"; +import { customAssign } from "./customAssign"; + +export function fetchResume(lang: string, branch: string, user: string): Promise { + + return fetch( + `https://raw.githubusercontent.com/${user}/${user}/${branch}/resume.json` + ) + .then(data => { + if (data.status !== 200) { + return Promise.reject(new Error()); + } + return data.json(); + }) + .then(data => { + return _.omit(customAssign({}, data, _.get(data, ['locales', lang])), [ + 'locales', + ]) + }); +} \ No newline at end of file diff --git a/src/helpers/store-to-local.ts b/src/helpers/store-to-local.ts index 6d85e04..6d4944e 100644 --- a/src/helpers/store-to-local.ts +++ b/src/helpers/store-to-local.ts @@ -1,10 +1,10 @@ -import fetch from 'cross-fetch'; import { message } from 'antd'; import { ResumeConfig } from "@/components/types"; import { customAssign } from "@/helpers/customAssign"; import _ from "lodash"; import { getLocale } from '@/locale'; import { RESUME_INFO } from '@/datas/resume'; +import { fetchResume } from './fetch-resume'; export const LOCAL_KEY = (user) => `${user ?? ''}resume-config`; @@ -22,24 +22,13 @@ export async function getConfig(lang: string, branch: string, user: string): Pro } } - return fetch( - `https://raw.githubusercontent.com/${user}/${user}/${branch}/resume.json` - ) - .then(data => { - if (data.status !== 200) { - message.warn(i18n.get('从模板中获取'), 1); - return _.omit( - customAssign({}, RESUME_INFO, _.get(RESUME_INFO, ['locales', lang])), - ['locales'] - ); - } - return data.json(); - }) - .then(data => { - return _.omit(customAssign({}, data, _.get(data, ['locales', lang])), [ - 'locales', - ]) - }); + return fetchResume(lang, branch, user).catch(() => { + message.warn(i18n.get('从模板中获取'), 1); + return _.omit( + customAssign({}, RESUME_INFO, _.get(RESUME_INFO, ['locales', lang])), + ['locales'] + ); + }) } export const saveToLocalStorage = _.throttle((user: string, config: ResumeConfig) => {