Skip to content

Commit 07fa01b

Browse files
committed
fix #3
1 parent 0ee9737 commit 07fa01b

File tree

4 files changed

+144
-5
lines changed

4 files changed

+144
-5
lines changed

README.md

+56
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,61 @@ const MicroComponentTest = () => {
306306

307307
export default MicroComponentTest;
308308
```
309+
310+
### 网络请求
311+
```
312+
import React from "react";
313+
import Space from "@/components/Space";
314+
//@ts-ignore
315+
import {HttpClient,Response} from "@codingapi/ui-framework";
316+
317+
const httpClient = new HttpClient(10000,{
318+
success:(msg:string)=>{
319+
console.log('success',msg);
320+
},
321+
error:(msg:string)=>{
322+
console.log('error',msg);
323+
},
324+
});
325+
326+
const HttpTest = ()=>{
327+
328+
const [url, setUrl] = React.useState('/api/products');
329+
330+
const handlerGet = ()=>{
331+
httpClient.get(url).then((res:Response)=>{
332+
const json = JSON.stringify(res);
333+
console.log(json);
334+
alert(json);
335+
})
336+
}
337+
338+
return (
339+
<>
340+
<div
341+
style={{
342+
textAlign: 'center'
343+
}}
344+
>
345+
<h1>Http Test </h1>
346+
</div>
347+
<Space>
348+
url:
349+
<input
350+
value={url}
351+
onChange={(e) => {
352+
setUrl(e.target.value);
353+
}}/>
354+
<button onClick={handlerGet}>get</button>
355+
</Space>
356+
</>
357+
)
358+
}
359+
360+
export default HttpTest;
361+
362+
```
363+
309364
更多实例参考: https://github.com/codingapi/ui-framework/tree/main/playground
310365

311366
## 主要特性
@@ -314,6 +369,7 @@ export default MicroComponentTest;
314369
- 事件总线:用于组件间通信
315370
- 访问控制:用于权限管理
316371
- 微前端动态组件:支持动态加载和卸载组件
372+
- 网络请求:封装了 HttpClient,支持 GET、POST、PUT、DELETE 等请求方式
317373

318374
## 开发
319375

playground/src/App.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ComponentBusTest from "@/components/ComponentBusTest";
44
import EventBusTest from "@/components/EventBusTest";
55
import MicroComponentTest from "@/components/MicroComponentTest";
66
import Base64Test from "@/components/Base64Test";
7+
import HttpTest from "@/components/HttpTest";
78

89
const App = () => {
910

@@ -18,6 +19,7 @@ const App = () => {
1819
<EventBusTest/>
1920
<MicroComponentTest/>
2021
<Base64Test/>
22+
<HttpTest/>
2123
</div>
2224
);
2325
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from "react";
2+
import Space from "@/components/Space";
3+
//@ts-ignore
4+
import {HttpClient,Response} from "@codingapi/ui-framework";
5+
6+
const httpClient = new HttpClient(10000,{
7+
success:(msg:string)=>{
8+
console.log('success',msg);
9+
},
10+
error:(msg:string)=>{
11+
console.log('error',msg);
12+
},
13+
});
14+
15+
const HttpTest = ()=>{
16+
17+
const [url, setUrl] = React.useState('/api/products');
18+
19+
const handlerGet = ()=>{
20+
httpClient.get(url).then((res:Response)=>{
21+
const json = JSON.stringify(res);
22+
console.log(json);
23+
alert(json);
24+
})
25+
}
26+
27+
return (
28+
<>
29+
<div
30+
style={{
31+
textAlign: 'center'
32+
}}
33+
>
34+
<h1>Http Test </h1>
35+
</div>
36+
<Space>
37+
url:
38+
<input
39+
value={url}
40+
onChange={(e) => {
41+
setUrl(e.target.value);
42+
}}/>
43+
<button onClick={handlerGet}>get</button>
44+
</Space>
45+
</>
46+
)
47+
}
48+
49+
export default HttpTest;

src/utils/http.ts

+37-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ export interface MessageBox {
77
error: (msg: string) => void;
88
}
99

10+
export type Response = {
11+
success: boolean;
12+
errCode?: string;
13+
errMessage?: string;
14+
data?: any;
15+
total?: number;
16+
}
17+
1018
export class HttpClient {
1119
private readonly api: AxiosInstance;
1220
private readonly messageBox: MessageBox;
@@ -90,23 +98,47 @@ export class HttpClient {
9098
)
9199
}
92100

93-
public get = async (url: string, params?: any) => {
101+
public get = async (url: string, params?: any): Promise<Response> => {
94102
try {
95103
const response = await this.api.get(url, {
96104
params
97105
});
98-
return response.data;
106+
return response.data as Response;
107+
} catch (e) {
108+
return {
109+
success: false,
110+
}
111+
}
112+
}
113+
114+
public put = async (url: string, data: any): Promise<Response> => {
115+
try {
116+
const response = await this.api.put(url, data);
117+
return response.data as Response;
118+
} catch (e) {
119+
return {
120+
success: false,
121+
}
122+
}
123+
}
124+
125+
public delete = async (url: string, params?: any): Promise<Response> => {
126+
try {
127+
const response = await this.api.delete(url, {
128+
params
129+
});
130+
return response.data as Response;
99131
} catch (e) {
100132
return {
101133
success: false,
102134
}
103135
}
104136
}
105137

106-
public post = async (url: string, data: any) => {
138+
public post = async (url: string, data: any): Promise<Response> => {
107139
try {
108140
const response = await this.api.post(url, data);
109-
return response.data;
141+
return response.data as Response;
110142
} catch (e) {
111143
return {
112144
success: false,
@@ -118,7 +150,7 @@ export class HttpClient {
118150
public page = async (url: string, params: any, sort: any, filter: any, match: {
119151
key: string,
120152
type: string
121-
}[]) => {
153+
}[]): Promise<Response> => {
122154
const base64Match = Base64Utils.stringToBase64(JSON.stringify(match));
123155
const base64Sort = Base64Utils.stringToBase64(JSON.stringify(sort));
124156
const base64Filter = Base64Utils.stringToBase64(JSON.stringify(filter));

0 commit comments

Comments
 (0)