ソースを参照

'调整配置读取方式;增加本地网关请求通用方法;'

main
郑州 3年前
コミット
0872012fa2
7個のファイルの変更48行の追加18行の削除
  1. +0
    -5
      .umirc.ts
  2. +4
    -0
      electron/config.js
  3. +2
    -5
      electron/preload.js
  4. +8
    -5
      electron/socket.js
  5. +3
    -1
      electron/tool.js
  6. +4
    -0
      src/global.d.ts
  7. +27
    -2
      src/utils/request.ts

+ 0
- 5
.umirc.ts ファイルの表示

@@ -32,9 +32,4 @@ export default defineConfig({
secure: false,
},
},
define: {
'global.URL_PREFIX': isBuildingElectron
? 'http://139.198.180.242:9003/'
: '',
},
});

+ 4
- 0
electron/config.js ファイルの表示

@@ -0,0 +1,4 @@
module.exports = {
remoteUrl: 'http://139.198.180.242:9003/',
gatewayPort: 7888,
};

+ 2
- 5
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);

+ 8
- 5
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', )
};

+ 3
- 1
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;

+ 4
- 0
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;


+ 27
- 2
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<T = any>(
path: string,
@@ -10,8 +13,30 @@ export async function fetchApi<T = any>(
) {
const [method, fullpath] = parseRequest(path);
const { silent, ...restOptions } = options;
const res = await request<API.ResponseData<T>>(`${remoteUrl}${fullpath}`, {
method,
[method === 'GET' ? 'params' : 'data']: params,
...restOptions,
});
if (!silent) {
handleRequest(res).error(() => {
message.error(res.message);
});
}
return res;
}

export async function fetchLocalApi<T = any>(
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<API.ResponseData<T>>(
`${global.URL_PREFIX || ''}${fullpath}`,
`http://127.0.0.1:${gatewayPort}/api/${path}`,
{ method, [method === 'GET' ? 'params' : 'data']: params, ...restOptions },
);
if (!silent) {


読み込み中…
キャンセル
保存