Bladeren bron

local assets

bianjiang 1 jaar geleden
bovenliggende
commit
f5ccbbbec0

+ 2 - 2
src/comm/controllers/appMsgRecvCtrl.ts

@@ -6,7 +6,7 @@ import { Controller } from "../core/controller";
 import { useCtx } from "../ctx";
 
 export type AssetSendedCallback = (assetUri:string)=>Promise<boolean>;
-export type AssetType = "empty" | "Pack"
+export type AssetType = "empty" | "webpackuri"
 const RevcChangeEvent = "app.recv.change"
 class AssetListener {
     id = "";
@@ -69,7 +69,7 @@ export class AppMsgRecvController extends Controller {
             return listen.id;
         }
         listen = new AssetListener(actionName, handle)
-        listen.type = "Pack";
+        listen.type = "webpackuri";
         this.listeners.push(listen);
     }
 

+ 242 - 8
src/comm/controllers/appScreenMsgCtrl.ts

@@ -5,23 +5,257 @@
  *   当前模块,导致nats.ws文件被引用,引发 部分手机 share页面打不开的bug
  */
 
+import { useList } from "@/modules/list";
 import { Controller } from "../core/controller";
 import { useCtx } from "../ctx";
+import { cloneDeep } from "lodash";
+import JSZip from "jszip";
+import FileSaver from "file-saver";
 import { queenApi } from "queenjs";
-
 export class AppScreenMsgController extends Controller {
+  screenModule: any;
   constructor() {
     super();
-    this.init();
-  }
-  init() {
-    console.log("controller init");
   }
 
   onReady() {
-    console.log("here can call other controller");
+    // this.initScreen();
+  }
+
+  initScreen() {
+    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) {
+        return false;
+      }
+      this.downloadLocalPack(packId);
+      return true;
+    });
+    recvCtrl.emitChange();
+  }
+  async downloadLocalPack(packId: string) {
+    const { actions } = this.screenModule;
+    const assetDtl = await actions.getAssetDetail(packId);
+    if (assetDtl) {
+      this.exportLocalWebPack(assetDtl);
+    }
   }
-  saveFiles(){
-    console.log("");
+  // loadHttp(url: string) {
+  //   const req = new XMLHttpRequest();
+  //   return new Promise((resolve, reject) => {
+  //     req.onload = function () {
+  //       if (req.status == 200) {
+  //         resolve(req.response);
+  //       }
+  //     };
+  //     // req.onreadystatechange = function() {
+  //     //   if (req.readyState == 4 && req.status == 200) {
+  //     //     resolve(req.response);
+  //     //   }
+  //     // }
+  //     req.timeout = 1000 * 60 * 3;
+  //     req.responseType = "blob";
+  //     req.open("GET", url, true);
+  //     req.send(null);
+  //   });
+  // }
+
+  loadfile(dir: string, url: string) {
+    if (!url) return;
+    const { deviceCtrl } = useCtx();
+    let names = url.split("/");
+    let name = names[names.length - 1];
+
+    const screenDir = deviceCtrl.appDataDir + "/screen";
+    const filepath = screenDir + dir.substring(1) + name;
+    deviceCtrl.DownloadFile(url, filepath);
+    return {
+      file: dir + name,
+      // promise: new Promise((resolve, reject) => {
+      //   // scope.assetCtroller.db.ctx.http.request(url, {method:"GET",baseURL:"", noAuth:true, responseType:"blob", originBody:true})
+      //   this.loadHttp(url).then((blob: any) => {
+      //     resolve({ file: dir.substring(2) + name, blob: blob });
+      //   });
+      // }),
+    };
+  }
+  async exportLocalWebPack(asset: any) {
+    // this.exportLocalEnv3ds();
+    // return;
+    queenApi.showLoading("资源下载中");
+    const { deviceCtrl } = useCtx();
+
+    let files: any = [];
+    let pack = cloneDeep(asset);
+    let source = pack.source;
+    let wk = `./assets/packs/${pack._id}/`;
+    let scope = this;
+
+    function getFileName(url: string) {
+      if (!url) return;
+
+      let names = url.split("/");
+      let name = names[names.length - 1];
+      return name;
+    }
+
+    //模型处理
+    let geom = source.geoms[0];
+    let osgbinUrl = geom.osgjs.url;
+    source.geoms = [geom];
+    let f = this.loadfile(wk, geom.osgjs.url);
+    // files.push(f?.promise);
+    geom.osgjs.url = f?.file;
+
+    osgbinUrl = osgbinUrl.substring(0, osgbinUrl.length - 5) + "bin";
+    f = this.loadfile(wk, osgbinUrl);
+    // files.push(f?.promise);
+
+    f = this.loadfile(wk, geom.shadow.url);
+    if (f) {
+      geom.shadow.url = f.file;
+      // files.push(f.promise);
+    }
+    delete geom.file;
+    delete geom.glb;
+
+    //材质处理
+    source.mats.forEach((m: any) => {
+      //base map
+      let f = scope.loadfile(wk, m.albedo.texture?.url);
+      if (f) {
+        // files.push(f.promise);
+        m.albedo.texture.url = f.file;
+      }
+
+      //法线
+      if (m.normal?.texture) {
+        f = scope.loadfile(wk, m.normal.texture?.url);
+        if (f) {
+          // files.push(f.promise);
+          m.normal.texture.url = f.file;
+        }
+      }
+
+      //粗糙度
+      if (m.roughness.texture) {
+        f = scope.loadfile(wk, m.roughness.texture.url);
+        if (f) {
+          // files.push(f.promise);
+          m.roughness.texture.url = f.file;
+        }
+      }
+
+      //金属度
+      if (m.metalness.texture) {
+        f = scope.loadfile(wk, m.metalness.texture.url);
+        if (f) {
+          // files.push(f.promise);
+          m.metalness.texture.url = f.file;
+        }
+      }
+
+      //透明度
+      if (m.opacity.texture) {
+        f = scope.loadfile(wk, m.opacity.texture.url);
+        if (f) {
+          // files.push(f.promise);
+          m.opacity.texture.url = f.file;
+        }
+      }
+    });
+    //封面图
+    let thumbnail = pack.thumbnail;
+    const thumb = this.loadfile(wk, thumbnail.url);
+    pack.thumbnail.url = thumb?.file;
+    //环境球
+    const wkenv3d = "./assets/env3d/";
+    source.env3ds.forEach((item: any) => {
+      const id = item.userData.assetId || item.id;
+
+      const wk = wkenv3d + id + "/bg/";
+      if (item.background?.image?.url) {
+        item.background.image.url = wk + getFileName(item.background.image.url);
+      }
+      const twk = wkenv3d + id + "/t/";
+      item.config.textures.forEach((t: any) => {
+        t.images.forEach((im: any) => {
+          im.file = twk + getFileName(im.file);
+        });
+      });
+    });
+
+    const wk3 = wkenv3d + "bg/";
+    source.scenes.forEach((c: any) => {
+      //@ts-ignore
+      const url = c.background?.image?.url;
+      if (url) {
+        //@ts-ignore
+        c.background.image.url = wk3 + getFileName(url);
+      }
+    });
+
+    const configFile = JSON.stringify(pack);
+
+    const res = await deviceCtrl.WriteFileText(
+      `${deviceCtrl.appDataDir}/screen/assets/packs/${pack._id}.json`,
+      configFile
+    );
+    const currentCategory = localStorage.getItem("category") || "default";
+    const assetsJson = await deviceCtrl.ReadFileText(
+      `${deviceCtrl.appDataDir}/screen/assets_${currentCategory}.json`
+    );
+    if (assetsJson.text) {
+      const assets = JSON.parse(assetsJson.text);
+      const assetItem = {
+        _id: pack._id,
+        name: pack.name,
+        thumbnail: pack.thumbnail.url,
+        type: "3D",
+        meshId: `./assets/packs/${pack._id}.json`,
+      };
+      assets.push(assetItem);
+      await deviceCtrl.WriteFileText(
+        `${deviceCtrl.appDataDir}/screen/assets_${currentCategory}.json`,
+        JSON.stringify(assets)
+      );
+    }
+    queenApi.hideLoading();
+    this.screenModule.actions.getAssetList();
+    // Promise.all(files).then((blobs) => {
+    //   let zip = new JSZip();
+    //   blobs.forEach((blob: any, i) => {
+    //     zip.file(`${blob.file}`, blob.blob, { binary: true });
+    //   });
+    //   zip.file(
+    //     `${pack._id}.json`,
+    //     new Blob([configFile], {
+    //       type: "text/plain",
+    //     })
+    //   );
+    //   const appDataDir = deviceCtrl.appDataDir;
+    //   const zipDir = `${appDataDir}/screen/${pack._id}.zip`;
+    //   zip.generateAsync({ type: "blob" }).then((content: any) => {
+    //     // deviceCtrl.DownloadFile(content, zipDir);
+    //     console.log(zipDir);
+    //     FileSaver.saveAs(content, zipDir);
+    //     queenApi.hideLoading();
+    //     // const ok=deviceCtrl.Unzip(zipDir,)
+    //   });
+    // });
   }
 }

+ 6 - 0
src/comm/controllers/deviceCtrl.ts

@@ -93,6 +93,12 @@ export class DeviceController extends Controller {
       appGuid: "",
     };
     this.appDataDir = await this.GetAppDataDir();
+    // try {
+    //   this.StartHttpServer(`${this.appDataDir}/screen`);
+    // } catch (e) {
+    //   console.log(e);
+    // }
+
     console.log("current app profile=>", this.profile);
   }
 

+ 9 - 1
src/modules/list/actions/list.ts

@@ -1,6 +1,6 @@
 import { useCtx } from "@/comm/ctx";
 import ListModule from "..";
-
+import { queenApi } from "queenjs";
 export default ListModule.action({
   async getAssetList() {
     const { deviceCtrl } = useCtx();
@@ -22,4 +22,12 @@ export default ListModule.action({
       this.store.list = assets;
     }
   },
+  async getAssetDetail(id: string) {
+    const res = await this.https.getAssetDetail(id);
+    if(res.errorNo!=200){
+      queenApi.messageError(res.errorDesc)
+      return
+    }
+    return res.result
+  },
 });

+ 5 - 0
src/modules/list/http.ts

@@ -7,6 +7,11 @@ export const https = ListModule.http({
       method: "GET",
     });
   },
+  getAssetDetail(id: string) {
+    return this.request(`/share/pack/detail/${id}`, {
+      method: "GET",
+    });
+  },
   loadAsset(dbId: string, defineId: string, baseURL: string) {
     return this.request(`/list/${dbId}/${defineId}`, {
       method: "GET",

+ 1 - 2
src/modules/list/index.ts

@@ -7,7 +7,7 @@ import { stores } from "./stores";
 export default class ListModule extends ModuleRoot {
   config = this.setConfig({
     httpConfig: {
-      baseURL: "./",
+      baseURL: "https://www.infish.cn/sku3d/v1/website",
     },
   });
   store = this.createStore([stores]);
@@ -19,7 +19,6 @@ export default class ListModule extends ModuleRoot {
   };
 
   onReady() {
-    this.actions.getAssetList();
     this.actions.initAnimateAttr();
   }
 }

+ 2 - 0
src/pages/App.tsx

@@ -7,12 +7,14 @@ import "windi.css";
 import "@/styles/global.less";
 import { InitControllers } from "@/comm/ctx";
 import { Spin } from "ant-design-vue";
+
 let setModuleHooks: any[] = [];
 
 const App = defineComponent(() => {
   const state = reactive({
     loading: true,
   });
+
   try {
     InitControllers().then(() => {
       state.loading = false;

+ 0 - 1
src/pages/frame3d/routes/frame3d/index.tsx

@@ -7,7 +7,6 @@ import { object } from "vue-types";
 import { LoadingOutlined } from "@ant-design/icons-vue";
 import { Spin } from "ant-design-vue";
 
-import { useList } from "@/modules/list";
 import { useQueditor } from "@queenjs-modules/queditor";
 
 function getQuery() {

+ 7 - 1
src/pages/website/routes/backend/List.tsx

@@ -9,7 +9,9 @@ import CategoryModal from "./categoryModal";
 
 export default defineComponent({
   setup() {
-    const { store, showModal, actions } = useList();
+    const ctx = useList();
+    const { store, showModal, actions } = ctx;
+
     const state = reactive({
       select: "default",
       optsions: [
@@ -34,6 +36,7 @@ export default defineComponent({
         key: "name",
       },
     ];
+    actions.getAssetList();
     const initShowCategory = async () => {
       const { deviceCtrl } = useCtx();
       const appDataDir = deviceCtrl.appDataDir;
@@ -47,7 +50,10 @@ export default defineComponent({
         state.optsions = assets;
       }
     };
+    const { msgCtrl } = useCtx();
+    msgCtrl.screenModule = ctx;
     initShowCategory();
+    msgCtrl.initScreen();
     const setShowCategory = (value: any) => {
       localStorage.setItem("category", value || "default");
       state.select = value;

+ 1 - 1
src/pages/website/routes/home/index.tsx

@@ -12,7 +12,7 @@ export default defineComponent({
     const canvasRef = ref();
 
     const rootRef = ref<HTMLElement>();
-
+    actions.getAssetList();
     onMounted(() => {
       actions.on("getAssetList:success", async () => {
         const res = await actions.initListCanvasData(

+ 1 - 1
yarn.lock

@@ -4969,7 +4969,7 @@ jstoxml@^2.0.0:
 
 jszip@^3.10.1:
   version "3.10.1"
-  resolved "http://124.70.149.18:4873/jszip/-/jszip-3.10.1.tgz"
+  resolved "http://124.70.149.18:4873/jszip/-/jszip-3.10.1.tgz#34aee70eb18ea1faec2f589208a157d1feb091c2"
   integrity sha512-xXDvecyTpGLrqFrvkrUSoxxfJI5AH7U8zxxtVclpsUtMCq4JQ290LY8AW5c7Ggnr/Y/oK+bQMbqK2qmtk3pN4g==
   dependencies:
     lie "~3.3.0"