first commit

This commit is contained in:
wang_yp 2024-09-30 09:49:33 +08:00
parent c1c33e3b29
commit 87963fa8e1
6 changed files with 171 additions and 46 deletions

View File

@ -2,21 +2,32 @@ import { Outlet } from "react-router";
import MyComponent from "./components/errorComp"; import MyComponent from "./components/errorComp";
// import MapUtl from "./components/map/mapUtil"; // import MapUtl from "./components/map/mapUtil";
import { useEffect } from "react"; import { useEffect } from "react";
const { socket } = require("./util/socket"); // const SocketService = require("./util/socket");
socket.init(); import SocketService from "./util/socket";
socket.onmessage((e) => { const socketService = SocketService.getInstance();
// const data = JSON.parse(e.data); const onMessage = (data: any) => {
// if (data.type === "heartbeat") { console.log("message", data);
// MapUtl.makerList[0].setPosition([103.55, 30.342]); };
// var m = MapUtl.amap; socketService.on("message", onMessage);
// var newIcon = new m.Icon({ const onConnected = () => {
// image: "//a.amap.com/jsapi_demos/static/demo-center/icons/dir-marker.png", //Icon 的图像 console.log("connected");
// size: new m.Size(25, 34), // 图标大小 };
// anchor: new m.Pixel(12, 32), // 图标锚点 socketService.on("connected", onConnected);
// });
// MapUtl.makerList[0].setIcon(newIcon); // socketService.
// } // socket.onmessage((e) => {
}); // const data = JSON.parse(e.data);
// if (data.type === "heartbeat") {
// MapUtl.makerList[0].setPosition([103.55, 30.342]);
// var m = MapUtl.amap;
// var newIcon = new m.Icon({
// image: "//a.amap.com/jsapi_demos/static/demo-center/icons/dir-marker.png", //Icon 的图像
// size: new m.Size(25, 34), // 图标大小
// anchor: new m.Pixel(12, 32), // 图标锚点
// });
// MapUtl.makerList[0].setIcon(newIcon);
// }
// });
const App = () => { const App = () => {
useEffect(() => { useEffect(() => {
console.log("app mounted"); console.log("app mounted");

View File

@ -28,7 +28,7 @@ const BTable = (props: any) => {
<Table <Table
style={{ height: "100%", overflow: "auto" }} style={{ height: "100%", overflow: "auto" }}
pagination={false} pagination={false}
scroll={{ x: 'max-content' }} // scroll={{ x: 'max-content' }}
loading={store.listStatus} loading={store.listStatus}
rowSelection={rowSelection} rowSelection={rowSelection}
columns={props.columns} columns={props.columns}

View File

@ -13,6 +13,7 @@ const LayOut = (props: Store) => {
const location = useLocation(); const location = useLocation();
useEffect(() => { useEffect(() => {
console.log("layout ",usrStore.isNeedLogin);
if (usrStore.isNeedLogin) { if (usrStore.isNeedLogin) {
nav("/login"); nav("/login");
} }

View File

@ -39,6 +39,8 @@ const Dispath = (props: Store) => {
const [userList, setUserList] = useState<any>([]); const [userList, setUserList] = useState<any>([]);
useEffect(() => { useEffect(() => {
try {
trainingCatStore.getlist().then(() => { trainingCatStore.getlist().then(() => {
setStashList(trainingCatStore.list); setStashList(trainingCatStore.list);
}); });
@ -50,6 +52,10 @@ const Dispath = (props: Store) => {
}); });
setUserList(data ?? []); setUserList(data ?? []);
}); });
} catch (error) {
console.log(error);
}
}, [trainingCatStore]); }, [trainingCatStore]);
const onFinish = (values: any) => { const onFinish = (values: any) => {

View File

@ -52,7 +52,9 @@ const PoliticalStudy = (props: Store) => {
...values, ...values,
score:Number(values.score) score:Number(values.score)
} }
data.file_url = ""; if (values.file_url){
data.file_url = values.file_url[0].url;
}
if (!record?.id) { if (!record?.id) {
politicalStudyStore.add(data); politicalStudyStore.add(data);
} else { } else {

View File

@ -1,38 +1,143 @@
class Socket { export type AutoReconnectOptions = boolean | {
static socketUrl = "ws://localhost:12214/ws"; maxRetries?: number
static ws: WebSocket; retryInterval?: number
init() { onMaxRetriesReached?: Function
try { }
Socket.ws = new WebSocket(Socket.socketUrl);
this.heartbeat(); export enum ConnectionStatus {
} catch (error) { Disconnected = 'DISCONNECTED',
console.log(error); Connected = 'CONNECTED',
Error = 'ERROR'
}
class SocketService {
private static instance: SocketService | null = null
private ws: WebSocket | null = null
private listeners: Record<string, Function[]> = {}
private autoReconnect: AutoReconnectOptions = true
private times: any = null
private retries: number = 0
private connectionStatus: ConnectionStatus = ConnectionStatus.Disconnected
private constructor() {
this.connect()
}
public static getInstance(): SocketService {
if (!SocketService.instance) {
SocketService.instance = new SocketService()
}
return SocketService.instance
}
public setAutoReconnectOptions(options: AutoReconnectOptions) {
this.autoReconnect = options
}
public connect() {
this.ws = new WebSocket('ws://127.0.0.1:12214/ws?id=admin')
this.ws.onopen = () => {
this.connectionStatus = ConnectionStatus.Connected
this.emit('connected', null)
this.hert()
}
this.ws.onerror = () => {
this.connectionStatus = ConnectionStatus.Error
clearTimeout(this.times)
this.emit('error', null)
}
this.ws.onclose = () => {
this.connectionStatus = ConnectionStatus.Disconnected
clearTimeout(this.times)
this.emit('disconnected', null)
if (this.shouldReconnect()) {
setTimeout(() => this.connect(), this.getRetryInterval())
} }
}
this.ws.onmessage = (event) => {
this.emit('message', event.data)
}
} }
send(data: any) { public hert(){
Socket.ws.send(data); this.times = setTimeout(() => this.send({"type":"heartbeat"}), 3000)
} }
onmessage(callback: any) { private shouldReconnect(): boolean {
Socket.ws.onmessage = callback; if (typeof this.autoReconnect === 'boolean') {
return this.autoReconnect
} else if (this.autoReconnect) {
const { maxRetries } = this.autoReconnect
if (maxRetries !== undefined) {
if (this.retries < maxRetries) {
this.retries++
return true
} else if (this.retries >= maxRetries) {
this.autoReconnect.onMaxRetriesReached && this.autoReconnect.onMaxRetriesReached()
return false
}
}
}
return false
} }
onclose(callback: any) {
Socket.ws.onclose = callback; private getRetryInterval(): number {
if (typeof this.autoReconnect === 'boolean') {
return 1000
} else if (this.autoReconnect && this.autoReconnect.retryInterval) {
return this.autoReconnect.retryInterval
}
return 1000
} }
onopen(callback: any) {
Socket.ws.onopen = callback; public send(data: any) {
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
this.ws.send(JSON.stringify(data))
} else {
console.error('WebSocket 连接未打开')
}
} }
onerror(callback: any) {
Socket.ws.onerror = callback; public close() {
if (this.ws) {
this.ws.close()
SocketService.instance = null
}
} }
close() {
Socket.ws.close(); private emit(event: string, data: any) {
if (!this.listeners[event]) {
return
}
this.listeners[event].forEach((listener) => listener(data))
} }
// 心跳
heartbeat() { public on(event: string, listener: Function) {
setInterval(() => { if (!this.listeners[event]) {
Socket.ws.send(`{"type":"heartbeat"}`); this.listeners[event] = []
}, 1000*60); }
this.listeners[event].push(listener)
if (event === 'connected' && this.connectionStatus === ConnectionStatus.Connected) {
listener()
}
} }
}
public off(event: string, listener: Function) {
export const socket = new Socket(); if (!this.listeners[event]) {
return
}
this.listeners[event] = this.listeners[event].filter((l) => l !== listener)
}
public getConnectionStatus(): ConnectionStatus {
return this.connectionStatus
}
public watchConnectionStatus(callback: (status: ConnectionStatus) => void) {
this.on('connected', () => callback(ConnectionStatus.Connected))
this.on('disconnected', () => callback(ConnectionStatus.Disconnected))
this.on('error', () => callback(ConnectionStatus.Error))
}
}
export default SocketService