load.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { useCtx } from "@/comm/ctx";
  2. import ListModule from "..";
  3. import { ItemObject, ShowItem } from "../objects/item";
  4. const defaultImage = require("@/assets/default.png");
  5. function loadImg(url: string, id: string, name: string): Promise<ShowItem> {
  6. return new Promise((resolve, reject) => {
  7. const { deviceCtrl } = useCtx();
  8. const m = new Image();
  9. if (url) {
  10. m.src = deviceCtrl.httpServer + url;
  11. } else {
  12. m.src = defaultImage;
  13. }
  14. m.onload = () => {
  15. resolve({
  16. id: id,
  17. w: m.width,
  18. h: m.height,
  19. url: url,
  20. offsetX: 0,
  21. offsetY: 0,
  22. x: 0,
  23. y: 0,
  24. z: 0,
  25. padding: 0,
  26. opacity: 0,
  27. currX: 0,
  28. currY: 0,
  29. show: false,
  30. offsetZ: 0,
  31. timer: 0,
  32. name,
  33. img: m,
  34. });
  35. };
  36. });
  37. }
  38. export default ListModule.action({
  39. async shuffleSelf(arr, size) {
  40. let index = -1;
  41. const length = arr.length;
  42. const lastIndex = length - 1;
  43. size = size === undefined ? length : size;
  44. while (++index < size) {
  45. const rand = index + Math.floor(Math.random() * (lastIndex - index + 1));
  46. const value = arr[rand];
  47. arr[rand] = arr[index];
  48. arr[index] = value;
  49. }
  50. arr.length = size;
  51. return arr;
  52. },
  53. async loadData() {
  54. // await this.actions.getAssetList();
  55. const ps = this.store.list.map((item: ItemObject, index) =>
  56. loadImg(item.thumbnail, item._id, item.name)
  57. );
  58. const rets = await Promise.all(ps);
  59. return rets;
  60. },
  61. async initLoadShowItems(rets: any[] = []) {
  62. const totalRow = this.store.totalRow;
  63. for (let i = 0; i < totalRow; i++) {
  64. //随机排序下
  65. this.actions.shuffleSelf(rets, rets.length);
  66. const offset = 0;
  67. rets.forEach((item, index) => {
  68. const it = { ...item };
  69. this.store.items.push(it);
  70. });
  71. }
  72. return rets;
  73. },
  74. async loadPack3d(item: ItemObject) {
  75. const { deviceCtrl } = useCtx();
  76. const meshId = item.meshId;
  77. if (!meshId) return;
  78. const ret:any = await this.https.loadLocal(deviceCtrl.httpServer + meshId);
  79. console.log("loadPack3d",ret);
  80. return ret;
  81. },
  82. });