const { ipcMain, app, Menu, Tray, BrowserWindow, dialog, Notification } = require('electron') const path = require('path') const fs = require('fs'); const webdata = require(path.join(__dirname, 'web/data.js')); const libdata = require(path.join(__dirname, 'data.js')); const git = require(path.join(__dirname, 'git.js')); const mainwin = require(path.join(__dirname, 'main.js')); let tray = null ipcMain.on('new-notification', (event, title, body, url) => { let n = new Notification({ title: title, body: body, }) n.on('click', function () { console.log(url); if (url) { mainwin.MainWindow.loadURL(url) mainwin.MainWindow.show(); } }) n.show() }); ipcMain.on('new-task-notification', (event) => { (new Notification({ title: "可能有新的任务", body: `当前总任务数${libdata.GetConfig().DaibanCount}` })).show() }); ipcMain.on('test', (event, a, b, c) => { console.log(event, a, b, c) }) let isUpdate = false; ipcMain.on('put-in-tray', (event) => { if (!tray) { mainwin.MainWindow.isClose = false; tray = new Tray(path.join(__dirname, '../windows-icon.png')) tray.on("double-click", function () { mainwin.MainWindow.show(); }) tray.on("right-click", function () { let data = webdata.getTask() let project_map = webdata.getProjectMap(); let ot = JSON.parse(fs.readFileSync(libdata.OpenTypeMapPath)); result = []; for (var i in data) { let submenu = []; for (var k in data[i]) { submenu.push({ label: data[i][k]["title"], type: "submenu", submenu: [ { label: "查看详情", click: (function () { let id = data[i][k]["id"]; return function () { mainwin.MainWindow.loadURL(`http://oa.ouxuan.net/task.php?a=x&num=work&show=we&mid=${id}`) mainwin.MainWindow.show(); } })() }, { label: "切换到该任务分支", click: (function () { let project_name = i; let id = data[project_name][k]["id"]; let title = `${id}-${data[project_name][k]["title"]}`; return function () { if (!project_map[project_name] || project_map[project_name] == "") { dialog.showErrorBox(`无法切换分支`, `该项目找不到所在路径(${project_name})`); return; } const workingDirPath = project_map[project_name] const simpleGit = require('simple-git')(workingDirPath); git.autoCheckoutLocalBranch(simpleGit, title, function () { console.log("autoCheckoutLocalBranch") fs.copyFileSync(path.join(__dirname, '../res/post-commit'), path.join(workingDirPath, ".git//hooks/post-commit")) if (ot[project_name] && ot[project_name] != "" && project_map[project_name] && project_map[project_name] != "") { var exec = require('child_process').execFile; exec("cmd.exe", ["/c", "start", "", ot[project_name], project_map[project_name]], function (err, data) { if (err) console.log(err) console.log(data.toString()); }); } }, function (err) { dialog.showErrorBox(`无法切换分支`, err); }) } })() }, ] }); } let project_dir = project_map[i] if (!project_dir) { project_dir = "" } else { project_dir = `(${project_dir})` } let project_opentype = ot[i] if (!project_opentype) { project_opentype = "(未设定)" } else { project_opentype = `(${project_opentype})` } let op = [ { label: "关联项目路径" + project_dir, click: (function (i) { return function (menuItem, browserWindow, event) { let project_map = JSON.parse(fs.readFileSync(libdata.ProjectMapPath)); let a = dialog.showOpenDialogSync({ properties: ['openDirectory'] }) if (a) { project_map[i] = a[0] fs.writeFileSync(libdata.ProjectMapPath, JSON.stringify(project_map)) } } })(i) }, { label: "关联程序" + project_opentype, click: (function (i) { return function (menuItem, browserWindow, event) { ot = JSON.parse(fs.readFileSync(libdata.OpenTypeMapPath)); let a = dialog.showOpenDialogSync({ properties: ['openFile'] }) if (a) { ot[i] = a[0] fs.writeFileSync(libdata.OpenTypeMapPath, JSON.stringify(ot)) } } })(i) }, , { label: "打开项目", click: (function (i) { return function (menuItem, browserWindow, event) { let ot = JSON.parse(fs.readFileSync(libdata.OpenTypeMapPath)); let project_map = JSON.parse(fs.readFileSync(libdata.ProjectMapPath)); if (ot[i] && ot[i] != "" && project_map[i] && project_map[i] != "") { var exec = require('child_process').execFile; exec("cmd.exe", ["/c", "start", "", ot[i], project_map[i]], function (err, data) { console.log(err) console.log(data.toString()); }); } } })(i) }, ] op = [ { label: "项目操作", type: "submenu", submenu: op }, { type: "separator" } ] let item = { label: i, type: "submenu", submenu: op.concat(submenu) } result.push(item); } tray.popUpContextMenu(Menu.buildFromTemplate(result.concat({ type: "separator" }, { label: "添加任务", click: function (menuItem, browserWindow, event) { mainwin.MainWindow.loadURL(`http://oa.ouxuan.net/index.php?a=lum&m=input&d=flow&num=work&show=we`) mainwin.MainWindow.show(); }, }, { label: "加班申请", click: function (menuItem, browserWindow, event) { mainwin.MainWindow.loadURL(`http://oa.ouxuan.net/?a=lum&m=input&d=flow&num=jiaban&show=we`) mainwin.MainWindow.show(); }, }, { type: "separator" }, { label: "仅查看未完成", type: "checkbox", checked: libdata.GetConfig().TaskType == "wwc", click: function (menuItem, browserWindow, event) { var config = libdata.GetConfig() config.TaskType = "my" if (menuItem.checked) { config.TaskType = "wwc" } libdata.SyncConfig(config) mainwin.MainWindow.reload() } }, { label: "检查更新", click: function () { if (isUpdate) { dialog.showErrorBox(`正在更新`, "请稍后重试"); return } isUpdate = true; let git_url = "https://git.ouxuan.net/3136352472/ouxuan.oa.git"; let workingDirPath = path.join(__dirname, "../../ouxuan.oa/"); let appDir = path.join(__dirname, "../../app/"); if (!fs.existsSync(workingDirPath)) { fs.mkdirSync(workingDirPath) const simpleGit = require('simple-git')(workingDirPath); simpleGit.clone(git_url, workingDirPath, function (e) { console.log(e) require('sync-copydir')("resources/ouxuan.oa/", "resources/app/") isUpdate = false; dialog.showMessageBoxSync({ "message": "更新完成" }) mainwin.MainWindow.isClose = true; app.quit() app.relaunch(); }) } else { const simpleGit = require('simple-git')(workingDirPath); simpleGit.checkIsRepo(function (err, ok) { if (ok) { simpleGit.pull(function (r, d) { isUpdate = false; if (d.files.length > 0) { require('sync-copydir')("resources/ouxuan.oa/", "resources/app/") dialog.showMessageBoxSync({ "message": "更新完成" }) mainwin.MainWindow.isClose = true; app.quit() app.relaunch(); } else { dialog.showMessageBoxSync({ "message": "无需更新" }) } }) } else { isUpdate = false; dialog.showErrorBox(`更新时发生错误`, workingDirPath + err); } }); } } }, { label: "退出", click: function () { mainwin.MainWindow.isClose = true; app.quit() } }))) }) } }) // exports.createTrayWindow = function () { // // Create the browser window. // const mainWindow = new BrowserWindow({ // width: 300, // height: 600, // webPreferences: { // preload: path.join(__dirname, 'web/tray.js') // } // }) // mainWindow.loadFile("res/tray.html") // mainWindow.hide() // }