Skip to content

Commit

Permalink
chore: add bug-breadcrumb-link folder
Browse files Browse the repository at this point in the history
  • Loading branch information
lzm0x219 committed May 23, 2023
1 parent d3fec65 commit e25642f
Show file tree
Hide file tree
Showing 133 changed files with 8,719 additions and 1,076 deletions.
226 changes: 127 additions & 99 deletions examples/ant-design-pro/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,109 +1,137 @@
import Footer from '@/components/Footer';
import RightContent from '@/components/RightContent';
import { BookOutlined, LinkOutlined } from '@ant-design/icons';
import type { Settings as LayoutSettings } from '@ant-design/pro-components';
import { SettingDrawer } from '@ant-design/pro-components';
import type { RunTimeLayoutConfig } from 'umi';
import { history, Link } from 'umi';
import { currentUser as queryCurrentUser } from './services/ant-design-pro/api';
// @ts-ignore
import type { RequestConfig } from '@@/plugin-request';
import { message } from 'antd';
import { notification } from 'antd/es';

const isDev = process.env.NODE_ENV === 'development';
const loginPath = '/user/login';

// TODO: 不知道这是啥?
/** 获取用户信息比较慢的时候会展示一个 loading */
// export const initialStateConfig = {
// loading: <PageLoading />,
// };
async function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

/**
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state
* */
export async function getInitialState(): Promise<{
settings?: Partial<LayoutSettings>;
currentUser?: API.CurrentUser;
loading?: boolean;
fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
}> {
const fetchUserInfo = async () => {
try {
const msg = await queryCurrentUser();
return msg.data;
} catch (error) {
history.push(loginPath);
}
return undefined;
};
// 如果是登录页面,不执行
if (history.location.pathname !== loginPath) {
const currentUser = await fetchUserInfo();
return {
fetchUserInfo,
currentUser,
settings: { layout: 'mix' },
};
}
export async function getInitialState() {
await delay(500);
return {
fetchUserInfo,
settings: { layout: 'mix' },
name: 'Big Fish',
size: 'big',
color: 'blue',
mood: 'happy',
food: 'fish',
location: 'sea',
};
}

// ProLayout 支持的api https://procomponents.ant.design/components/layout
export const layout: RunTimeLayoutConfig = ({
initialState,
setInitialState,
}) => {
return {
rightContentRender: () => <RightContent />,
disableContentMargin: false,
waterMarkProps: {
content: initialState?.currentUser?.name,
export const locale = {
textComponent: 'h1',
onError: () => {
console.log('error handler...');
},
// locale: string
// formats: CustomFormats
// messages: Record<string, string> | Record<string, MessageFormatElement[]>
// defaultLocale: string
// defaultFormats: CustomFormats
// timeZone?: string
// textComponent?: React.ComponentType | keyof React.ReactHTML
// wrapRichTextChunksInFragment?: boolean
// defaultRichTextElements?: Record<string, FormatXMLElementFn<React.ReactNode>>
// onError(err: string): void
};

export const layout = {
logout() {
alert('logout');
},
};

export function onRouteChange(opts: any) {
console.log('route changed', opts.location.pathname);
}

enum ErrorShowType {
SILENT = 0,
WARN_MESSAGE = 1,
ERROR_MESSAGE = 2,
NOTIFICATION = 3,
REDIRECT = 9,
}

interface ResponseStructure {
success: boolean;
data: any;
errorCode?: number;
errorMessage?: string;
showType?: number;
}

export const request: RequestConfig = {
requestInterceptors: [
(config) => {
console.log('Interceptor:', config);
return config;
},
footerRender: () => <Footer />,
onPageChange: () => {
const { location } = history;
// 如果没有登录,重定向到 login
// if (!initialState?.currentUser && location.pathname !== loginPath) {
// history.push(loginPath);
// }
(url, options) => {
console.log(url, options);
return { url, options };
},
links: isDev
? [
<Link to="/umi/plugin/openapi" target="_blank">
<LinkOutlined />
<span>OpenAPI 文档</span>
</Link>,
<Link to="/~docs">
<BookOutlined />
<span>业务组件文档</span>
</Link>,
]
: [],
menuHeaderRender: undefined,
// 自定义 403 页面
// unAccessible: <div>unAccessible</div>,
// 增加一个 loading 的状态
childrenRender: (children, props) => {
// if (initialState?.loading) return <PageLoading />;
return (
<>
{children}
{!props.location?.pathname?.includes('/login') && (
<SettingDrawer
enableDarkTheme
settings={initialState?.settings}
onSettingChange={(settings) => {
setInitialState((preInitialState) => ({
...preInitialState,
settings,
}));
}}
/>
)}
</>
);
],
responseInterceptors: [
(res) => {
console.log('responseInterceptor', res);
return res;
},
...initialState?.settings,
};
],
errorConfig: {
errorHandler: (error: any, opts: any) => {
if (opts?.skipErrorHandler) throw error;
// 我们的 errorThrow 抛出的错误。
if (error.name === 'BizError') {
const errorInfo: ResponseStructure | undefined = error.info;
if (errorInfo) {
const { errorMessage, errorCode } = errorInfo;
switch (errorInfo.showType) {
case ErrorShowType.SILENT:
// do nothong
break;
case ErrorShowType.WARN_MESSAGE:
message.warn(errorMessage);
break;
case ErrorShowType.ERROR_MESSAGE:
message.error(errorMessage);
break;
case ErrorShowType.NOTIFICATION:
notification.open({
description: errorMessage,
message: errorCode,
});
break;
case ErrorShowType.REDIRECT:
// TODO: redirect
break;
default:
message.error(errorMessage);
}
}
} else if (error.response) {
// Axios 的错误
// 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
message.error('Response status:', error.response.status);
} else if (error.request) {
// 请求已经成功发起,但没有收到响应
// \`error.request\` 在浏览器中是 XMLHttpRequest 的实例,
// 而在node.js中是 http.ClientRequest 的实例
message.error('None response! Please retry.');
} else {
// 发送请求时出了点问题
message.error('Request error, please retry.');
}
},
errorThrower: (res: ResponseStructure) => {
const { success, data, errorCode, errorMessage, showType } = res;
if (!success) {
const error: any = new Error(errorMessage);
error.name = 'BizError';
error.info = { errorCode, errorMessage, showType, data };
throw error;
}
},
},
};
3 changes: 3 additions & 0 deletions examples/bug-breadcrumb-link-max/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: require.resolve('@umijs/max/eslint'),
};
13 changes: 13 additions & 0 deletions examples/bug-breadcrumb-link-max/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/node_modules
/.env.local
/.umirc.local.ts
/config/config.local.ts
/src/.umi
/src/.umi-production
/src/.umi-test
/.umi
/.umi-production
/.umi-test
/dist
/.mfsu
.swc
17 changes: 17 additions & 0 deletions examples/bug-breadcrumb-link-max/.lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"*.{md,json}": [
"prettier --cache --write"
],
"*.{js,jsx}": [
"max lint --fix --eslint-only",
"prettier --cache --write"
],
"*.{css,less}": [
"max lint --fix --stylelint-only",
"prettier --cache --write"
],
"*.ts?(x)": [
"max lint --fix --eslint-only",
"prettier --cache --parser=typescript --write"
]
}
3 changes: 3 additions & 0 deletions examples/bug-breadcrumb-link-max/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.umi
.umi-production
8 changes: 8 additions & 0 deletions examples/bug-breadcrumb-link-max/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"printWidth": 80,
"singleQuote": true,
"trailingComma": "all",
"proseWrap": "never",
"overrides": [{ "files": ".prettierrc", "options": { "parser": "json" } }],
"plugins": ["prettier-plugin-organize-imports", "prettier-plugin-packagejson"]
}
3 changes: 3 additions & 0 deletions examples/bug-breadcrumb-link-max/.stylelintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: require.resolve('@umijs/max/stylelint'),
};
31 changes: 31 additions & 0 deletions examples/bug-breadcrumb-link-max/.umirc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { defineConfig } from '@umijs/max';

export default defineConfig({
antd: {},
access: {},
model: {},
initialState: {},
request: {},
layout: {
title: '@umijs/max',
},
routes: [
{
name: '首页',
path: '/home',
routes: [
{
name: 'c',
path: '/home/c',
component: './Home/c',
},
{
name: 'd',
path: '/home/d',
component: './Home/d',
},
],
},
],
npmClient: 'pnpm',
});
3 changes: 3 additions & 0 deletions examples/bug-breadcrumb-link-max/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# README

`@umijs/max` 模板项目,更多功能参考 [Umi Max 简介](https://umijs.org/docs/max/introduce)
20 changes: 20 additions & 0 deletions examples/bug-breadcrumb-link-max/mock/userAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const users = [
{ id: 0, name: 'Umi', nickName: 'U', gender: 'MALE' },
{ id: 1, name: 'Fish', nickName: 'B', gender: 'FEMALE' },
];

export default {
'GET /api/v1/queryUserList': (req: any, res: any) => {
res.json({
success: true,
data: { list: users },
errorCode: 0,
});
},
'PUT /api/v1/user/': (req: any, res: any) => {
res.json({
success: true,
errorCode: 0,
});
},
};
27 changes: 27 additions & 0 deletions examples/bug-breadcrumb-link-max/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"private": true,
"author": "0x219 <lzm0x219@gmail.com>",
"scripts": {
"build": "max build",
"dev": "max dev",
"format": "prettier --cache --write .",
"postinstall": "max setup",
"setup": "max setup",
"start": "npm run dev"
},
"dependencies": {
"@ant-design/icons": "^5.1.0",
"@ant-design/pro-components": "^2.4.16",
"@umijs/max": "workspace:^",
"antd": "^5.5.1"
},
"devDependencies": {
"@types/react": "^18.2.6",
"@types/react-dom": "^18.2.4",
"lint-staged": "^13.2.2",
"prettier": "^2.8.8",
"prettier-plugin-organize-imports": "^3.2.2",
"prettier-plugin-packagejson": "^2.4.3",
"typescript": "^5.0.4"
}
}
10 changes: 10 additions & 0 deletions examples/bug-breadcrumb-link-max/src/access.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default (initialState: API.UserInfo) => {
// 在这里按照初始化数据定义项目中的权限,统一管理
// 参考文档 https://umijs.org/docs/max/access
const canSeeAdmin = !!(
initialState && initialState.name !== 'dontHaveAccess'
);
return {
canSeeAdmin,
};
};
Loading

0 comments on commit e25642f

Please sign in to comment.