BS唤醒:
1、先注册伪协议:
app.setAsDefaultProtocolClient("link");
在浏览器上输入 link://,如果传参的话这样输入?link://appstart?name=1111&password=123456,appstart没有用是随便写的;
2、接收参数有两种方式:
? ① 应用未启用时:用process.argv获取;
log.info("获取【process.argv】的数据:" + process.argv);
ipcMain.on("open-login", function (e, msg) {
// 把数据传给页面,在页面里处理数据
e.sender.send("process-argv", process.argv);
});
? ?②?应用启用时:通过second-instance生命周期获取参数;
let paramsObj = {};
// 单例锁
const gotTheLock = app.requestSingleInstanceLock();
if (gotTheLock) {
// 应用第二实例,commandLine数据通过处理获取传入的参数
app.on("second-instance", (event, commandLine, workingDirectory) => {
log.info("commandLine数据:" + commandLine);
// 当运行第二个实例时,将会聚焦到mainWindow这个窗口
if (mainWindow) {
mainWindow.show();
mainWindow.setSkipTaskbar(false);
mainWindow.focus();
}
if (process.platform === "win32") {
let commands = commandLine.slice();
// commandLine 是一个数组, 其中最后一个数组元素为我们唤醒的链接
let paramsUrl = decodeURI(commands.pop());
log.info("BS跳转连接url:" + JSON.stringify(paramsUrl));
mainWindow.webContents.send("jump-message", paramsUrl);
}
});
}
3、处理传入页面的数据:
// 页面首次启动触发
if (this.$store.state.user.isFirst) {
ipcRenderer.send("open-login");
}
// 监听第二实例 second-instance 触发
ipcRenderer.on("jump-message", (event, paramsUrl) => {
let objParams = this.findUrlParams(paramsUrl);
// 自动登录
this.loginAuto(objParams);
}
});
// 监听第一启动获取参数 process.argv
ipcRenderer.on("process-argv", (event, argvArr) => {
// 判断是BS链接唤醒还是CS命令行唤醒:findObj有值是BS链接唤醒,否则是CS命令行唤醒
let findObj = argvArr.find((item) => {
return item.includes("link://startapp");
});
let objParams = {};
if ((findObj ?? "") !== "") {
// BS链接唤醒
objParams = this.findUrlParams(findObj);
} else {
// CS命令行唤醒
argvArr.forEach((item) => {
if (item.includes("=")) {
let arr = item.split("=");
objParams[arr[0]] = arr[1];
}
});
}
if (Object.keys(objParams).includes("account")) {
this.argvObj = objParams;
this.loginAuto(objParams);
this.$store.commit("SET_ISFIRST", false);
}
});
// BS参数整理
findUrlParams(url) {
let paramsObj = {};
if (url.includes("?")) {
let ind = url.indexOf("?");
let splitArr = url.slice(ind + 1).split("&");
if (splitArr.length > 0) {
splitArr.forEach((ele) => {
let splitEle = ele.split("=");
paramsObj[splitEle[0]] = splitEle[1];
});
}
}
return paramsObj;
},
参考资料:
https://www.jianshu.com/p/05b9f2fbf4f7
CS唤醒:
通过cmd命令行唤醒并传参:
cmd /c C:\Users\lenovo\AppData\Local\Programs\project\project.exe username=1111 password=123456
命令行唤醒参考:http://ty2y.com/study/cmdmlhdcqdcx.html
通过process.argv获取数据
// process.argv 获取参数
log.info("获取【process.argv】的数据:" + process.argv);
ipcMain.on("open-login", function (e, msg) {
e.sender.send("process-argv", process.argv);
});
处理参数:
ipcRenderer.on("process-argv", (event, argvArr) => {
// 判断是BS链接唤醒还是CS命令行唤醒:findObj有值是BS链接唤醒,否则是CS命令行唤醒
let findObj = argvArr.find((item) => {
return item.includes(link://startapp");
});
let objParams = {};
if ((findObj ?? "") !== "") {
// BS链接唤醒
objParams = this.findUrlParams(findObj);
} else {
// CS命令行唤醒
argvArr.forEach((item) => {
if (item.includes("=")) {
let arr = item.split("=");
objParams[arr[0]] = arr[1];
}
});
}
});
|