bianjiang vor 1 Jahr
Ursprung
Commit
08faf2b2fd
2 geänderte Dateien mit 67 neuen und 77 gelöschten Zeilen
  1. 62 60
      src/comm/controllers/appMsgRecvCtrl.ts
  2. 5 17
      src/comm/controllers/appScreenMsgCtrl.ts

+ 62 - 60
src/comm/controllers/appMsgRecvCtrl.ts

@@ -5,79 +5,81 @@ import { nanoid } from "nanoid";
 import { Controller } from "../core/controller";
 import { useCtx } from "../ctx";
 
-export type AssetSendedCallback = (assetUri:string)=>Promise<boolean>;
-export type AssetType = "empty" | "webpackuri"
-const RevcChangeEvent = "app.recv.change"
+export type AssetSendedCallback = (assetUri: string) => Promise<boolean>;
+export type AssetType = "empty" | "mat";
+const RevcChangeEvent = "app.recv.change";
 class AssetListener {
-    id = "";
-    type = "empty" as AssetType;
-    constructor(public actionName:string, public callback: AssetSendedCallback){
-        this.id = actionName;
-    }
-    
-    toJson() {
-        return {
-            id: this.id,
-            type: this.type,
-            action: this.actionName,
-        }
-    }
-}
+  id = "";
+  type = "empty" as AssetType;
+  constructor(public actionName: string, public callback: AssetSendedCallback) {
+    this.id = actionName;
+  }
 
+  toJson() {
+    return {
+      id: this.id,
+      type: this.type,
+      action: this.actionName,
+    };
+  }
+}
 
 export class AppMsgRecvController extends Controller {
+  listeners = [] as AssetListener[];
+  appGuid = "";
 
-    listeners = [] as AssetListener[];
-    appGuid = "";
+  async onReady() {
+    const { deviceCtrl, natsCtrl } = useCtx();
 
-    async onReady() {
-        const {deviceCtrl, natsCtrl} = useCtx();
+    this.appGuid = deviceCtrl.profile.appGuid;
 
-        this.appGuid = deviceCtrl.profile.appGuid;
+    natsCtrl.subscribe(
+      `send.${this.appGuid}`,
+      async (msg: { id: string; shareId: string }) => {
+        const listen = this.listeners.find((item) => item.id == msg.id);
+        if (!listen)
+          return {
+            isOk: false,
+            error: "nolistener",
+          };
 
-        natsCtrl.subscribe(`send.${this.appGuid}`, async (msg:{id:string,fromKey:string, uri:string,name?:string, thumbnail?:string})=>{
-            const listen = this.listeners.find(item=>item.id == msg.id)
-            if (!listen) return {
-                isOk: false,
-                error: "nolistener"
-            }
-            const ok = await listen.callback(msg.uri);
-            return JSON.stringify({ isOk: ok})
-        })
-
-        natsCtrl.subscribe(`recv.actions.${this.appGuid}`, async ()=>{
-            return JSON.stringify(this.listeners.map(item=>item.toJson()))
-        })
-    }
+        const ok = await listen.callback(msg.shareId);
+        return JSON.stringify({ isOk: ok });
+      }
+    );
 
+    natsCtrl.subscribe(`recv.actions.${this.appGuid}`, async () => {
+      return JSON.stringify(this.listeners.map((item) => item.toJson()));
+    });
+  }
 
-    //清除所有监听者
-    clearListeners() {
-        this.listeners = [];
-    }
+  //清除所有监听者
+  clearListeners() {
+    this.listeners = [];
+  }
 
-    emitChange() {
-        const {natsCtrl} = useCtx();
-        natsCtrl.publish(RevcChangeEvent, JSON.stringify({Guid: this.appGuid}))
-    }
+  emitChange() {
+    const { natsCtrl } = useCtx();
+    natsCtrl.publish(RevcChangeEvent, JSON.stringify({ Guid: this.appGuid }));
+  }
 
-    //添加资源监听者  
-    addAssetsListener(actionName:string, handle: AssetSendedCallback) {
-        let listen = this.listeners.find(item=>item.actionName == actionName);
-        if (listen)  {
-            listen.callback = handle;
-            return listen.id;
-        }
-        listen = new AssetListener(actionName, handle)
-        listen.type = "webpackuri";
-        this.listeners.push(listen);
+  //添加资源监听者
+  addAssetsListener(actionName: string, handle: AssetSendedCallback) {
+    let listen = this.listeners.find((item) => item.actionName == actionName);
+    if (listen) {
+      listen.callback = handle;
+      return listen.id;
     }
+    listen = new AssetListener(actionName, handle);
+    listen.type = "mat";
+    this.listeners.push(listen);
+  }
 
-    removeListener(id:string) {
-        let listen = this.listeners.find(item=>item.id == id);
-        if (!listen)  {
-            return;
-        }
-        this.listeners.splice(this.listeners.indexOf(listen), 1);
+  removeListener(id: string) {
+    let listen = this.listeners.find((item) => item.id == id);
+    if (!listen) {
+      return;
     }
+    this.listeners.splice(this.listeners.indexOf(listen), 1);
+  }
 }

+ 5 - 17
src/comm/controllers/appScreenMsgCtrl.ts

@@ -23,30 +23,18 @@ export class AppScreenMsgController extends Controller {
     const { recvCtrl } = useCtx();
 
     recvCtrl.clearListeners();
-    recvCtrl.addAssetsListener("发送模型", async (packUrl: string) => {
-      const urlReg = /[a-zA-z]+:\/\/[^\s]*/;
-      let packId = packUrl;
-      if (urlReg.test(packUrl)) {
-        const urlArr = packUrl.split("?");
-        const params = urlArr[1].split("&");
-        let obj: any = {};
-        for (let i = 0; i < params.length; i++) {
-          let param = params[i].split("=");
-          obj[param[0]] = param[1];
-        }
-        packId = obj.id;
-      }
-      if (!packId) {
+    recvCtrl.addAssetsListener("发送模型", async (shareId: string) => {
+      if (!shareId) {
         return false;
       }
-      this.downloadLocalPack(packId);
+      this.downloadLocalPack(shareId);
       return true;
     });
     recvCtrl.emitChange();
   }
-  async downloadLocalPack(packId: string) {
+  async downloadLocalPack(shareId: string) {
     const { actions } = this.screenModule;
-    const assetDtl = await actions.getAssetDetail(packId);
+    const assetDtl = await actions.getAssetDetail(shareId);
     if (assetDtl) {
       this.exportLocalWebPack(assetDtl);
     }