Jelajahi Sumber

fix: 修复ipc api交互异常
1. 修复ipc api的交互异常问题
2. 修复磁贴数据存储问题

kindring 8 bulan lalu
induk
melakukan
b85072fce0

+ 11 - 4
src/apis/baseApi.ts

@@ -61,15 +61,19 @@ export class ApiController {
     logFn: (...args: any[]) => void = console.log
     constructor() {
     }
-    init(type: string, sendCallback: SendFunction, listenerCallback: ListenerFunction, sendKey: string, listerKey: string){
+    init(type: string, sendCallback: SendFunction, listenerCallback: ListenerFunction, sendKey: string, listerKey: string, signId: string){
         this.isInit = true
         this.type = type
         this.sendCallback = sendCallback
         this.listenerCallback = listenerCallback
         this.sendKey = sendKey
         this.listenerKey = listerKey
-        this.listenerCallback(listerKey, this.apiControllerHandler)
+        this.signId = signId
+
+        // listenerCallback(listerKey, this.apiControllerHandler)
         this.sendTasks.forEach(requestData => {
+            requestData.signId = this.signId
+            console.log(`[I] sendTasks: ${JSON.stringify(requestData)}`)
             sendCallback(sendKey, requestData)
             this.refreshTimeout(requestData.callId, requestData.timeout)
         })
@@ -106,8 +110,9 @@ export class ApiController {
         }
         return this.buildNotifyId(action)
     }
-    private apiControllerHandler = (_:any, data: ResponseData<any> | NotifyData) =>
+    public apiControllerHandler = (_:any, data: ResponseData<any> | NotifyData) =>
     {
+        console.log(`[I] apiControllerHandler: ${JSON.stringify(data)}`)
         switch(data.type){
             case ApiType.res:
                 if(this.calls[data.callId]){
@@ -264,6 +269,7 @@ export class ApiController {
      * @param signId
      */
     changeSign(signId: string) {
+        console.log(`[I] changeSign: ${signId}`)
         this.signId = signId
     }
 
@@ -274,7 +280,7 @@ export class ApiController {
      * @param params
      * @param timeout
      */
-    sendQuery(action: string, params: any, timeout: number = 5000): [callId: string, Promise<ResponseData<any>>] {
+    sendQuery(action: string, params: any, timeout: number = 10 * 1000): [callId: string, Promise<ResponseData<any>>] {
         let callId = this.buildCallId()
         let requestData: RequestData = {
             type: ApiType.req,
@@ -284,6 +290,7 @@ export class ApiController {
             callId: callId,
             timeout: timeout,
         }
+        console.log(requestData)
         if(this.isInit){
             if (this.sendCallback){
                 this.sendCallback(this.sendKey, requestData)

+ 24 - 12
src/common/db/magnetDb.ts

@@ -3,8 +3,8 @@ import {SavedMagnet} from "@/types/magnetType.ts";
 import Logger from "@/util/logger.ts";
 import {handle} from "@/util/promiseHandle.ts";
 let logger = Logger.logger('magnet_db', 'info');
-async function initData() {
-    console.log('初始化数据库')
+export async function initMagnetData() {
+    console.log('初始化磁贴数据库')
     // 1. 判断数据表是否存在
     if(!db){
         logger.error('数据库初始化失败')
@@ -35,6 +35,7 @@ async function initData() {
     logger.info('[初始化磁贴表成功]')
     return true;
 }
+initMagnetData()
 
 
 
@@ -48,8 +49,7 @@ export function findMagnetById(id: string) {
     ).from('magnets').where('id', id)
 }
 
-export async function getMagnetList(): Promise<SavedMagnet[]> {
-    initData()
+export function getMagnetList(): Promise<SavedMagnet[]> {
     return db?.select(
         'id',
         'x',
@@ -60,25 +60,37 @@ export async function getMagnetList(): Promise<SavedMagnet[]> {
 }
 
 // 批量更改
-export async function changeMagnets(magnetList: SavedMagnet[]) {
-    await db?.transaction(async (trx) => {
+export function changeMagnets(magnetList: SavedMagnet[]) {
+    return db?.transaction(async (trx) => {
         for (const magnet of magnetList) {
-            await trx('magnets').where('id', magnet.id).update(magnet)
+            let changeMagnet = {
+                id: magnet.id,
+                size: magnet.size,
+                type: magnet.type,
+                x: magnet.x,
+                y: magnet.y,
+            }
+            await trx('magnets').where('id', changeMagnet.id).update(changeMagnet)
         }
     })
 }
 
 // 新增数据
-export async function addMagnet(magnet: SavedMagnet) {
-    initData()
+export  function addMagnet(magnet: SavedMagnet) {
     // id 字段移除
+    let addMagnet = {
+        size: magnet.size,
+        type: magnet.type,
+        x: magnet.x,
+        y: magnet.y,
+    }
+    return db?.insert(addMagnet).into('magnets')
 
-    await db?.insert(magnet).into('magnets')
 }
 
 // 删除数据
-export async function deleteMagnet(magnetId: string) {
-    await db?.delete().from('magnets').where('id', magnetId)
+export  function deleteMagnet(magnetId: string) {
+    return db?.delete().from('magnets').where('id', magnetId)
 }
 
 

+ 1 - 0
src/main/control/magnet/magnet.ts

@@ -21,6 +21,7 @@ export async function c_fetchMagnetList(requestData: RequestData){
         }
         return responseData;
     }
+    console.log(result)
     responseData = {
         type: ApiType.res,
         code: ErrorCode.success,

+ 12 - 5
src/main/tools/ipcRouter.ts

@@ -20,14 +20,21 @@ function sendDataByType(type: string,  data: ResponseData<any> | NotifyData): bo
     return true;
 }
 
-function sendDataBySign(signId: string, data: ResponseData<any> | NotifyData): boolean
-{
-    let win = AppControl.findWin(signId)
-    if(!win){
+function sendDataBySign(signId: string, data: ResponseData<any> | NotifyData): boolean {
+    let appWindow = AppControl.findWin(signId)
+    if (!appWindow) {
         logger.error(`[发送数据] 未找到id为${signId}的窗口`);
         return false;
     }
-    win.win?.webContents.send(actionMap.apiControl.resCode, data)
+    logger.info(`[发送数据至前端] 寻找id为${signId}的窗口 ${actionMap.apiControl.resCode}`);
+    logger.info(`[发送数据至前端] ${JSON.stringify(data)}`);
+    let win = appWindow?.win
+    if (!win)
+    {
+        logger.error(`[发送数据] id为${signId}的窗口 未初始化`);
+        return false;
+    }
+    win.webContents.send(actionMap.apiControl.resCode, data)
     return true;
 }
 const sendToMain = sendDataByType.bind(null, 'main')

+ 13 - 6
src/util/pageHandle.ts

@@ -4,6 +4,7 @@ import {ipcRenderer} from "electron";
 import {actionMap, IpcAction, windowAction} from "../tools/IpcCmd.ts";
 import {registerWindowData} from "../types/appConfig.ts";
 import baseApi from "@/apis/baseApi.ts";
+import {NotifyData, ResponseData} from "@/types/apiTypes.ts";
 
 // 判断是否为 webMode
 
@@ -52,11 +53,7 @@ export function windowInit(app: App, type: string){
     // 先将验证窗口绑定到全局, 接收到 绑定消息后再进行绑定
     app.config.globalProperties.$winHandle = tryBindWindow(ipcRenderer, type)
     // 初始化api调用函数. 用于统一调用
-    baseApi.init(type,
-        ipcRenderer.send,
-        ipcRenderer.on,
-        actionMap.apiControl.code,
-        actionMap.apiControl.resCode);
+
 
     ipcRenderer.on(
     windowAction.bindSignId.code,
@@ -67,7 +64,17 @@ export function windowInit(app: App, type: string){
             console.warn(`未知的窗口绑定 ${data.type}`);
             return;
         }
-        baseApi.changeSign(data.signId);
+        ipcRenderer.on(actionMap.apiControl.resCode, (_: Electron.IpcRendererEvent, apiData: ResponseData<any> | NotifyData)=>{
+            baseApi.apiControllerHandler(_ , apiData)
+        })
+        baseApi.init(type,
+            ipcRenderer.send,
+            ipcRenderer.on,
+            actionMap.apiControl.code,
+            actionMap.apiControl.resCode,
+            data.signId
+        );
+
         app.config.globalProperties.$winHandle = registerWinHandle(ipcRenderer, data.signId);
         console.log(   `窗口绑定:${windowAction.bindSignId.resCode}` );
         ipcRenderer.send(windowAction.bindSignId.resCode, data.signId);