diff --git a/.umirc.ts b/.umirc.ts index d6c9803..2fe8495 100644 --- a/.umirc.ts +++ b/.umirc.ts @@ -32,9 +32,4 @@ export default defineConfig({ secure: false, }, }, - define: { - 'global.URL_PREFIX': isBuildingElectron - ? 'http://139.198.180.242:9003/' - : '', - }, }); diff --git a/electron/config.js b/electron/config.js new file mode 100644 index 0000000..fe4b1c0 --- /dev/null +++ b/electron/config.js @@ -0,0 +1,4 @@ +module.exports = { + remoteUrl: 'http://139.198.180.242:9003/', + gatewayPort: 7888, +}; diff --git a/electron/preload.js b/electron/preload.js index fefe04d..a04fd41 100644 --- a/electron/preload.js +++ b/electron/preload.js @@ -1,12 +1,9 @@ const electron = require('electron'); const { contextBridge, ipcRenderer } = electron; +const config = require('./config'); contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer); contextBridge.exposeInMainWorld('addIpcRendererListener', (event, callback) => { ipcRenderer.on(event, callback); }); - -/** - * todo: - * websocket事件处理 - */ +contextBridge.exposeInMainWorld('systemConfig', config); diff --git a/electron/socket.js b/electron/socket.js index b7d9fae..8cc0c2e 100644 --- a/electron/socket.js +++ b/electron/socket.js @@ -1,16 +1,19 @@ const io = require('ws'); +const config = require('./config'); -function initialWebsocket(onMessage) { - const socket = new io('ws://127.0.0.1:7888/websocket/subscriptionTaskSync'); +function initialWebsocket(onMessage, onError) { + const socket = new io( + `ws://127.0.0.1:${config.gatewayPort}/websocket/subscriptionTaskSync`, + ); - socket.on('open', (socket) => { + socket.on('open', () => { // socket.emit("hello", "world"); console.log('socket connection'); socket.on('message', onMessage); }); socket.on('error', (...args) => { - // socket.emit("hello", "world"); console.log('socket error:', args); + onError && onError(...args); }); } @@ -18,6 +21,6 @@ module.exports.initialWebsocketEvents = function initialWebsocketEvents( onMessage, onError, ) { - initialWebsocket(); + initialWebsocket(onMessage, onError); // ipcMain.handle('socket:on', ) }; diff --git a/electron/tool.js b/electron/tool.js index 0f02007..47e37f7 100644 --- a/electron/tool.js +++ b/electron/tool.js @@ -1,4 +1,4 @@ -export class Subject { +class Subject { constructor() { this.observers = []; } @@ -12,3 +12,5 @@ export class Subject { this.observers.forEach((f) => f(...args)); } } + +module.exports.Subject = Subject; diff --git a/src/global.d.ts b/src/global.d.ts index da1694e..a7447d5 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -7,6 +7,10 @@ declare global { eventName: string, callback: (event: any, data: any) => void, ): void; + systemConfig?: { + remoteUrl: string; + gatewayPort: number; + }; // initialStorage?: { // keyList: string[]; // [key: string]: any; diff --git a/src/utils/request.ts b/src/utils/request.ts index b3f281a..12bf03a 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -1,7 +1,10 @@ import { message } from 'antd'; import { request } from 'umi'; import { parseRequest } from './request.config'; -import { firstCharToLowerCase, handleRequest } from './tool'; +import { errorReponse, firstCharToLowerCase, handleRequest } from './tool'; + +const remoteUrl = window.systemConfig?.remoteUrl || ''; +const gatewayPort = window.systemConfig?.gatewayPort || 0; export async function fetchApi( path: string, @@ -10,8 +13,30 @@ export async function fetchApi( ) { const [method, fullpath] = parseRequest(path); const { silent, ...restOptions } = options; + const res = await request>(`${remoteUrl}${fullpath}`, { + method, + [method === 'GET' ? 'params' : 'data']: params, + ...restOptions, + }); + if (!silent) { + handleRequest(res).error(() => { + message.error(res.message); + }); + } + return res; +} + +export async function fetchLocalApi( + path: string, + params = {}, + options = { silent: false, method: 'GET' }, +) { + if (!gatewayPort) { + return errorReponse('gateway infomation failed'); + } + const { silent, method = 'GET', ...restOptions } = options; const res = await request>( - `${global.URL_PREFIX || ''}${fullpath}`, + `http://127.0.0.1:${gatewayPort}/api/${path}`, { method, [method === 'GET' ? 'params' : 'data']: params, ...restOptions }, ); if (!silent) {