-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from wenzhu-eternal/feat/version_0.1.1
version_0.1.1
- Loading branch information
Showing
7 changed files
with
124 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useEffect } from 'react'; | ||
import { getIP } from "@/utils"; | ||
import io from 'socket.io-client'; | ||
import cookie from 'react-cookies'; | ||
import { Modal } from 'antd'; | ||
|
||
export default function WebSocket({ dataName, callback }: { | ||
dataName: string; | ||
callback: (data: any) => any, | ||
}) { | ||
useEffect((): any => { | ||
const socket = io(getIP()); | ||
const token = cookie.load('x-auth-token'); | ||
let errTimes = 0; | ||
|
||
socket.on('connect', () => { | ||
errTimes = 0; | ||
socket.emit('addSocket', { token }); | ||
}); | ||
|
||
socket.on(dataName, data => { | ||
callback(JSON.parse(data)); | ||
}); | ||
|
||
socket.on("connect_error", () => { | ||
if (errTimes === 10) { | ||
Modal.error({ | ||
title: '连接错误', | ||
content: '网络连接异常,需要刷新页面处理!如还异常,请联系管理员。', | ||
okText: '刷新', | ||
onOk: () => history.go(), | ||
}); | ||
} | ||
errTimes++; | ||
socket.emit('delectSocket', { token }); | ||
}); | ||
|
||
return () => { | ||
socket.emit('delectSocket', { token }); | ||
socket.close() | ||
}; | ||
}, []) | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import { request } from "@/utils"; | ||
import { useEffect } from "react"; | ||
import WebSocket from "@/components/WebSocket"; | ||
|
||
export default function About() { | ||
useEffect(() => { | ||
request('GET', 'api/user/findUsers?page=1&pageSize=10') | ||
.then((res) => console.log('res :>> ', res)) | ||
.catch((err) => console.log('err :>> ', err)) | ||
request({ | ||
url: 'api/user/findUsers?page=1&pageSize=10', | ||
method: 'GET', | ||
}).then((res: any) => console.log('res :>> ', res)) | ||
.catch((err: any) => console.log('err :>> ', err)) | ||
}, []) | ||
return <h2>About</h2>; | ||
|
||
return <h2> | ||
<WebSocket dataName="wsData" callback={(data) => console.log('data :>> ', data)} /> | ||
WebSocket | ||
</h2>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import MyIcon from './Icons'; | ||
import request from './request'; | ||
import request, { getIP } from './request'; | ||
|
||
export { MyIcon, request }; | ||
export { | ||
MyIcon, | ||
request, | ||
getIP, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,46 @@ | ||
/** | ||
* @Description: http 请求 | ||
* @param {string} method 请求类型 | ||
* @param {string} url | ||
* @param {any} body | ||
* @param {any} history | ||
*/ | ||
import { message } from 'antd'; | ||
import axios from 'axios'; | ||
import { notification } from 'antd'; | ||
import cookie from 'react-cookies'; | ||
|
||
const getIP = () => { | ||
export const getIP = () => { | ||
if (process.env.NODE_ENV === 'production') { | ||
return ''; | ||
} | ||
return 'http://localhost:9000/'; | ||
}; | ||
|
||
export default function request( | ||
method: string, | ||
url: string, | ||
body?: any, | ||
) { | ||
method = method.toUpperCase(); | ||
if (method === 'GET') { | ||
body = undefined; | ||
} else { | ||
body = body && JSON.stringify(body); | ||
} | ||
const c_token = cookie.load('x-auth-token'); | ||
return fetch(getIP() + url, { | ||
method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json', | ||
'x-auth-token': c_token, | ||
}, | ||
body, | ||
}).then((res) => { | ||
const token = res.headers.get('x-auth-token'); | ||
if (token) { | ||
cookie.save('x-auth-token', token, { path: '/' }); | ||
} | ||
return res.json().then((res) => { | ||
if (res.statusCode === 401) { | ||
message.error('你无权限或权限到期,请重新登录!').then(() => { | ||
if (!(window.location.pathname === '/')) { | ||
window.history.pushState('', '', '/'); | ||
window.history.go(); | ||
} | ||
}) | ||
const request = axios.create({ | ||
baseURL: getIP(), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json', | ||
}, | ||
timeout: 3000, | ||
}) | ||
|
||
request.interceptors.request.use((config: any) => { | ||
config['headers']['x-auth-token'] = cookie.load('x-auth-token'); | ||
return config; | ||
}); | ||
|
||
request.interceptors.response.use((response: any) => { | ||
const { data, config: { headers } } = response; | ||
switch (data.statusCode) { | ||
case 0: { | ||
const token = headers['x-auth-token']; | ||
if (token) { | ||
cookie.save('x-auth-token', token, { path: '/' }); | ||
} | ||
return res; | ||
}) | ||
}); | ||
} | ||
return data.data; | ||
}; | ||
case 400: return Promise.reject(data.data); | ||
case 401: { | ||
return notification['error']({ | ||
message: data.message, | ||
description: data.data, | ||
}); | ||
}; | ||
} | ||
}); | ||
|
||
export default request; |