Skip to content

Commit 72f5f13

Browse files
committed
fix framework
1 parent f078c86 commit 72f5f13

File tree

13 files changed

+301
-1
lines changed

13 files changed

+301
-1
lines changed

antd-pro/config/config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export default defineConfig({
1818
// 默认是 browser
1919
history: { type: 'hash' },
2020

21+
// dva 的配置
22+
dva: {},
23+
2124
/**
2225
* @name 兼容性设置
2326
* @description 设置 ie11 不一定完美兼容,需要检查自己使用的所有依赖

antd-pro/config/routes.ts

+39
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,45 @@ export default [
9393
},
9494
],
9595
},
96+
{
97+
name: 'framework',
98+
icon: 'appstore',
99+
path: '/framework',
100+
access: 'hasAuthentication',
101+
routes: [
102+
{
103+
name: 'dva',
104+
path: '/framework/dva',
105+
access: 'hasAuthentication',
106+
routes: [
107+
{
108+
name: 'test1',
109+
path: '/framework/dva/test1',
110+
component: './framework/dva/test1',
111+
access: 'hasAuthentication',
112+
},
113+
{
114+
name: 'test2',
115+
path: '/framework/dva/test2',
116+
component: './framework/dva/test2',
117+
access: 'hasAuthentication',
118+
},
119+
]
120+
},
121+
{
122+
name: 'bean',
123+
path: '/framework/bean',
124+
access: 'hasAuthentication',
125+
component: './framework/bean',
126+
},
127+
{
128+
name: 'event',
129+
path: '/framework/event',
130+
access: 'hasAuthentication',
131+
component: './framework/event',
132+
}
133+
]
134+
},
96135
{
97136
path: '/',
98137
redirect: '/welcome',

antd-pro/src/app.tsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,18 @@ import React from 'react';
1010
import { AvatarDropdown, AvatarName } from './components/RightContent/AvatarDropdown';
1111
import { loadLayoutMenus, loadLoayoutMenuAuthentications } from './components/Menu';
1212
import { menus } from '@/services/api/account'
13-
const loginPath = '/user/login';
13+
14+
const loginPath = '/login';
15+
import * as beans from '@/bean';
16+
17+
// 初始化bean
18+
function initBean() {
19+
console.log('register beans');
20+
beans.registerBeans();
21+
}
22+
23+
24+
initBean();
1425

1526
/**
1627
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state
@@ -119,3 +130,5 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
119130
export const request = {
120131
...errorConfig,
121132
};
133+
134+

antd-pro/src/bean/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defaultTestModel } from '@/bean/test';
2+
import { beanFactory } from 'coding-components'
3+
4+
5+
export const registerBeans = () => {
6+
beanFactory.registerBean(defaultTestModel);
7+
}
8+

antd-pro/src/bean/test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { TestModel } from "@/domain/test";
2+
3+
4+
//todo 这个地方最好可以实现全局任何地方都可以注册bean
5+
export const defaultTestModel = new class extends TestModel{
6+
beanName: string = "TestModel";
7+
loadData(): string {
8+
return "default loadData";
9+
}
10+
}
11+
12+

antd-pro/src/domain/test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Bean } from "coding-components";
2+
3+
export class Test {
4+
count = 0;
5+
6+
public add(index: number) {
7+
this.count += index;
8+
}
9+
10+
public clear() {
11+
this.count = 0;
12+
}
13+
14+
constructor(count: number) {
15+
this.count = count;
16+
}
17+
18+
public toStates() {
19+
return {
20+
count: this.count,
21+
};
22+
}
23+
24+
public hasCount() {
25+
return this.count > 0;
26+
}
27+
}
28+
29+
export abstract class TestModel implements Bean {
30+
beanName: string = "TestModel";
31+
32+
public abstract loadData(): string;
33+
}
34+
35+

antd-pro/src/locales/en-US/menu.ts

+6
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,10 @@ export default {
5555
'menu.components.parameter': 'Parameter',
5656
'menu.components.oss': 'Files',
5757
'menu.components.oss.test': 'File Test',
58+
'menu.framework': 'Framework',
59+
'menu.framework.dva': 'dva',
60+
'menu.framework.dva.test1': 'Test1',
61+
'menu.framework.dva.test2': 'Test2',
62+
'menu.framework.bean': 'Bean',
63+
'menu.framework.event': 'Event',
5864
};

antd-pro/src/locales/zh-CN/menu.ts

+6
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,10 @@ export default {
5555
'menu.components.parameter': '参数管理',
5656
'menu.components.oss': '文件管理',
5757
'menu.components.oss.test': '文件测试',
58+
'menu.framework': '框架',
59+
'menu.framework.dva': 'dva',
60+
'menu.framework.dva.test1': '测试1',
61+
'menu.framework.dva.test2': '测试2',
62+
'menu.framework.bean': 'Bean',
63+
'menu.framework.event': '事件',
5864
};

antd-pro/src/models/test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {Test} from '@/domain/test';
2+
3+
export default {
4+
namespace: 'test',
5+
state: {
6+
count: 0,
7+
},
8+
9+
effects: {
10+
*test({ payload, state }: any, { call, put }: any) {
11+
yield put({ type: 'testSuccess', data: 1 });
12+
},
13+
},
14+
reducers: {
15+
testSuccess(state: any, { data }) {
16+
const test = new Test(state.count);
17+
test.add(data);
18+
return test.toStates();
19+
},
20+
21+
clear(state: any, action: any) {
22+
const test = new Test(state.count);
23+
test.clear();
24+
return test.toStates();
25+
},
26+
},
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from "react";
2+
import { Button, Space } from "antd";
3+
4+
import { TestModel } from '@/domain/test';
5+
import { defaultTestModel } from "@/bean/test";
6+
import { beanFactory } from "coding-components";
7+
8+
9+
const TestPage: React.FC = () => {
10+
11+
const [data, setData] = React.useState<string>();
12+
13+
14+
// 获取bean对象,调用loadData方法
15+
const handleClick = () => {
16+
const model = beanFactory.getBean(TestModel);
17+
setData(model.loadData());
18+
}
19+
20+
const handleDefault = () => {
21+
// 重置bean对象
22+
beanFactory.registerBean(defaultTestModel);
23+
}
24+
25+
const handleRegister = () => {
26+
// 注册bean对象
27+
beanFactory.registerBean(new class extends TestModel {
28+
loadData() {
29+
return "new load data";
30+
}
31+
});
32+
}
33+
34+
35+
return (
36+
<>
37+
<h1>{data}</h1>
38+
<Space>
39+
<Button type="primary" onClick={handleClick}>getBean</Button>
40+
<Button onClick={handleDefault}>resetBean</Button>
41+
<Button onClick={handleRegister}>registerBean</Button>
42+
</Space>
43+
</>
44+
)
45+
46+
}
47+
48+
export default TestPage;
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { connect, useSelector } from "@umijs/max";
2+
import { Button, Space } from "antd";
3+
import React from "react";
4+
import { Test } from '@/domain/test';
5+
6+
7+
const TestPage: React.FC<any> = (props: any) => {
8+
9+
const handleAdd = () => {
10+
props.dispatch({ type: 'test/test', payload: { "name": "test" } })
11+
}
12+
13+
const handleClear = () => {
14+
props.dispatch({ type: 'test/clear' })
15+
}
16+
17+
const test = useSelector((state: any) => {
18+
return new Test(state.test.count);
19+
});
20+
21+
const handleCount = () => {
22+
alert(test.hasCount());
23+
}
24+
25+
26+
return (
27+
<div>
28+
<h1>Test1</h1>
29+
<h1>{test.count}</h1>
30+
<Space>
31+
<Button onClick={handleAdd}>Add</Button>
32+
<Button onClick={handleClear}>Clear</Button>
33+
<Button onClick={handleCount}>hasCount</Button>
34+
</Space>
35+
</div>
36+
)
37+
}
38+
39+
40+
41+
export default connect(({ dva }: any) => ({ dva }))(TestPage)
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { connect, useSelector } from "@umijs/max";
2+
import { Button } from "antd";
3+
import React from "react";
4+
5+
6+
7+
const TestPage: React.FC<any> = (props: any) => {
8+
9+
const handleAdd = () => {
10+
props.dispatch({ type: 'test/test', payload: { "name": "test" } })
11+
}
12+
13+
const { count } = useSelector((state: any) => {
14+
return state.test;
15+
});
16+
17+
return (
18+
<div>
19+
<h1>Test2</h1>
20+
<h1>{count}</h1>
21+
<Button onClick={handleAdd}>Add</Button>
22+
</div>
23+
)
24+
}
25+
26+
27+
28+
export default connect(({ dva }: any) => ({ dva }))(TestPage)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { events } from "coding-components"
2+
import { Button } from "antd";
3+
import React, { useEffect } from "react"
4+
5+
const TestPage: React.FC = () => {
6+
7+
const [data, setData] = React.useState<any>();
8+
9+
const eventAction = 'test';
10+
11+
const handleClick = () => {
12+
events.emit(eventAction, 'event data');
13+
}
14+
15+
useEffect(() => {
16+
events.on(eventAction, (data: any) => {
17+
setData(data)
18+
});
19+
20+
return () => {
21+
events.off(eventAction);
22+
}
23+
}, [])
24+
25+
return (
26+
<>
27+
<h1>监听:{data}</h1>
28+
<Button onClick={handleClick}>发送事件</Button>
29+
</>
30+
)
31+
32+
}
33+
34+
export default TestPage;

0 commit comments

Comments
 (0)