Sfoglia il codice sorgente

纯双评版本(取的sheetUrls)

刘洋 2 anni fa
parent
commit
825612d712
4 ha cambiato i file con 52 aggiunte e 30 eliminazioni
  1. 2 0
      .eslintrc.js
  2. 5 1
      src/background.ts
  3. 41 2
      src/features/ImageDownload/ImageDownload.vue
  4. 4 27
      src/lib/watermark.ts

+ 2 - 0
.eslintrc.js

@@ -16,5 +16,7 @@ module.exports = {
   rules: {
     "no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
     "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off",
+    '@typescript-eslint/no-explicit-any' : 'off',
+    '@typescript-eslint/no-var-requires' : 'off'
   },
 };

+ 5 - 1
src/background.ts

@@ -27,7 +27,11 @@ async function createWindow() {
       webSecurity: false,
     },
   });
-
+  win.webContents.on("before-input-event", (event, input) => {
+    if (input.key === "F12") {
+      win.webContents.openDevTools();
+    }
+  });
   if (process.env.WEBPACK_DEV_SERVER_URL) {
     // Load the url of the dev server if in development mode
     await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string);

+ 41 - 2
src/features/ImageDownload/ImageDownload.vue

@@ -80,9 +80,12 @@ let errorCount = ref(0);
 let students = [];
 
 const config = store.pageInputs["/image-download"];
-console.log("config::", config);
+// console.log("config::", config);
 onMounted(async () => {
   const storePassedToNodeJs = JSON.parse(JSON.stringify(store));
+  const color = storePassedToNodeJs.config.watermark.color;
+  const nextColor = storePassedToNodeJs.config.watermark.nextColor;
+  const otherColor = storePassedToNodeJs.config.watermark.otherColor;
   try {
     if (config.type === "1") {
       console.log("download start ", Date.now());
@@ -125,6 +128,41 @@ onMounted(async () => {
         cacheImages(urls);
 
         for (const student of students) {
+          let allTags = Object.values(student.tags)
+            .filter((x) => !!x)
+            .flat();
+          // console.log("allTags:", allTags);
+          allTags.sort((a: any, b: any) => {
+            return a.userId - b.userId;
+          });
+          let colorMap: any = {};
+          for (let i = 0; i < allTags.length; i++) {
+            const tag: any = allTags[i];
+            const { groupNumber } = tag;
+            if (!colorMap[groupNumber + ""]) {
+              colorMap[groupNumber + ""] = {};
+              colorMap[groupNumber + ""][tag.userId + ""] = color;
+            } else {
+              let targetGroupObjKeys = Object.keys(colorMap[groupNumber + ""]);
+              const len = targetGroupObjKeys.length;
+              if (len == 1) {
+                if (!targetGroupObjKeys.includes(tag.userId + "")) {
+                  colorMap[groupNumber + ""][tag.userId + ""] = nextColor;
+                } else {
+                  continue;
+                }
+              } else if (len > 1) {
+                if (!targetGroupObjKeys.includes(tag.userId + "")) {
+                  colorMap[groupNumber + ""][tag.userId + ""] = otherColor;
+                } else {
+                  continue;
+                }
+              }
+              // let c = len === 1 ? nextColor : otherColor;
+              // colorMap[groupNumber + ""][tag.userId + ""] = c;
+            }
+          }
+          console.log("colorMap:", colorMap);
           for (const sheetUrl of student.sheetUrls) {
             if (stopSignal) return;
             try {
@@ -172,7 +210,8 @@ onMounted(async () => {
                 index + 1,
                 config.trackMode,
                 config.x,
-                config.y
+                config.y,
+                colorMap
               );
             } catch (error) {
               errorCount.value += 1;

+ 4 - 27
src/lib/watermark.ts

@@ -17,7 +17,8 @@ export async function addWatermark(
   index: number,
   trackMode: string,
   x = 0.01,
-  y = 0.03
+  y = 0.03,
+  colorMap: any = {}
 ): Promise<boolean> {
   const file = path.join(...filePath);
   if (
@@ -50,8 +51,6 @@ export async function addWatermark(
   }
   const fontFile = store.config.watermark.fontFile;
   const color = store.config.watermark.color;
-  const nextColor = store.config.watermark.nextColor;
-  const otherColor = store.config.watermark.otherColor;
   const image = Buffer.from(imageData);
   // const size = sizeOf(image);
   // console.log(size);
@@ -243,35 +242,13 @@ export async function addWatermark(
     // imgData.font(fontFile, fontSize).fill(color);
     const tags = student.tags[index];
 
-    let userIds: any = tags.map((item: any) => item.userId || 0);
-    userIds = Array.from(new Set(userIds));
-    userIds.sort();
-    const colorMap: any = {
-      "0": color,
-    };
-    let startIndex = 1;
-    if (!userIds.includes(0)) {
-      startIndex = 0;
-    }
-
-    for (let i = 0; i < userIds.length; i++) {
-      const uId: string | number = userIds[i];
-      if (i == startIndex) {
-        colorMap[uId + ""] = color;
-      } else if (i == startIndex + 1) {
-        colorMap[uId + ""] = nextColor;
-      } else if (i > startIndex + 1) {
-        colorMap[uId + ""] = otherColor;
-      }
-    }
-
     for (let i = 0; i < tags.length; i++) {
       const tag = tags[i];
       if (tag.content != undefined) {
         let top = tag.top;
         imgData
           .font(fontFile, fontSize)
-          .fill(colorMap[tag.userId + ""] || color);
+          .fill(colorMap[tag.groupNumber + ""][tag.userId + ""] || "gray");
         for (let j = 0; j < tag.content.length; j++) {
           imgData.drawText(tag.left, top, tag.content[j]);
           top += height;
@@ -300,7 +277,7 @@ export async function saveImage(
   filePath: string[]
 ): Promise<boolean> {
   const file = path.join(...filePath);
-  console.log("saveImage file:", file);
+  // console.log("saveImage file:", file);
   if (store.pageInputs["/image-download"].append && fs.existsSync(file)) {
     console.log(file + " already exists");
     return true;