index.js 3.2 KB
import { app, BrowserWindow } from "electron";
import "../renderer/store";
import { ipcMain } from "electron";
var path = require("path");
var nodemailer = require("nodemailer");
const compressing = require("compressing");
const { exec } = require("child_process");
/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== "development") {
  global.__static = require("path")
    .join(__dirname, "/static")
    .replace(/\\/g, "\\\\");
}

let mainWindow;
const winURL =
  process.env.NODE_ENV === "development"
    ? `http://localhost:9080`
    : `file://${__dirname}/index.html`;

function createWindow() {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 563,
    useContentSize: true,
    width: 1000,
  });

  mainWindow.loadURL(winURL);

  mainWindow.on("closed", () => {
    mainWindow = null;
  });
}

app.on("ready", createWindow);

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});
ipcMain.on("aaa", (event, data) => {
  event.sender.send("bbb", 1);
});
app.on("activate", () => {
  if (mainWindow === null) {
    createWindow();
  }
});
ipcMain.on("send-email", (event, data) => {
  var dataInfo = data.emailForm;
  var {
    title,
    pass,
    fromPeople,
    toPeople,
    ccPeople,
    fileInfo,
    htmlContent,
    isFile,
  } = dataInfo;
  var attachments = [];
  if (fileInfo.length > 0 && isFile == 1) {
    fileInfo.forEach((fileItem) => {
      attachments.push({
        filename: fileItem.fileName,
        path: fileItem.filePath,
      });
    });
  }

  var transporter = nodemailer.createTransport({
    service: "IMAP",
    secureConnection: true, // 使用 SSL
    host: "smtphz.qiye.163.com",
    port: 465,
    auth: {
      user: fromPeople,
      pass, //授权码
    },
  });
  var mailOptions = {
    from: fromPeople, // 发送者
    to: toPeople, // 接受者,可以同时发送多个,以逗号隔开
    cc: ccPeople, // 抄送者,可以同时发送多个,以逗号隔开
    subject: title, // 标题
    //text: '', // 文本
    html: htmlContent,
    attachments,
  };

  transporter.sendMail(mailOptions, function (err, info) {
    if (err) {
      event.sender.send("send-message", {
        code: -1,
        info: err,
      });
      return;
    }
    event.sender.send("send-message", {
      code: 0,
      info: "ok",
    });
  });
});
ipcMain.on("send-build", (event, data) => {
  var fileInfo = data.fileInfo;
  const disk = fileInfo.path.split(":")[0];
  const packageParth = fileInfo.path;
  let build = exec(`cd /d ${packageParth} && npm run build`, (err) => {
    if (err) throw err;
  });
  build.stdout.pipe(process.stdout);
  build.on("exit", () => {
    compress();
  });
  function compress() {
    compressing.zip
      .compressDir(
        path.join(fileInfo.filePath.split(".zip")[0]),
        path.join(fileInfo.filePath)
      )

      .then(() => {
        event.sender.send("build-message", {
          code: 0,
          info: "ok",
        });
      })

      .catch((err) => {
        event.sender.send("build-message", {
          code: -1,
          info: err,
        });
      });
  }
});