diff --git a/src/common/event/PlatForm/index.ts b/src/common/event/PlatForm/index.ts index a85a591..d03501a 100644 --- a/src/common/event/PlatForm/index.ts +++ b/src/common/event/PlatForm/index.ts @@ -24,6 +24,16 @@ class PlatForm extends _Base { return this.api.call("PlatFormCommand.showAbout") } + async showSrd() { + // return this.api.call("BasicService.showAbout") + return this.api.call("PlatFormCommand.showSrd") + } + + async getSrdCookie() { + // return this.api.call("BasicService.showAbout") + return this.api.call("PlatFormCommand.getSrdCookie") + } + async crash() { return this.api.call("PlatFormCommand.crash") } diff --git a/src/common/event/PlatForm/main/command.ts b/src/common/event/PlatForm/main/command.ts index 5452171..c1fbb00 100644 --- a/src/common/event/PlatForm/main/command.ts +++ b/src/common/event/PlatForm/main/command.ts @@ -33,6 +33,39 @@ export default class PlatFormCommand { } } + getSrdCookie() { + let srdWindow = this._WindowManager.get("srd") + let mainWindow = this._WindowManager.getMainWindow() + if (srdWindow && mainWindow) { + console.log(srdWindow.webContents.session); + console.log(srdWindow.webContents.session.storagePath); + console.log(srdWindow.webContents.session.cookies); + } + } + + showSrd(){ + this._WindowManager.createWindow("srd", { + url: "https://www.srdcloud.cn/", + overideWindowOpts: true, + confrimWindowClose: false, + type: "info", + windowOpts: { + width: 600, + height: 400, + darkTheme: true, + modal: true, + show: false, + resizable: true, + webPreferences: { + devTools: false, + sandbox: false, + nodeIntegration: false, + contextIsolation: true, + }, + }, + }) + } + showAbout() { this._WindowManager.createWindow("about", { url: getFileUrl("about.html"), diff --git a/src/main/utils/session/cookies.ts b/src/main/utils/session/cookies.ts new file mode 100644 index 0000000..3088021 --- /dev/null +++ b/src/main/utils/session/cookies.ts @@ -0,0 +1,47 @@ +import { BrowserView, BrowserWindow } from "electron" + +const cookies = { + getCurrCookies(params = {}, currWin: BrowserView | BrowserWindow) { + let currSession = currWin.webContents.session + return currSession.cookies.get(Object.assign({}, params)) + }, + removeCurrCookies(cookies = [], currWin: BrowserView | BrowserWindow) { + let currSession = currWin.webContents.session + let err = [] + let apiCount = 0 + return new Promise((resove, reject) => { + cookies.forEach(async (item: any) => { + await currSession.cookies.remove(`http://${item.domain}`, item.name) + apiCount = apiCount + 1 + if (err.length === apiCount) { + resove({ message: "cookie 清除成功" }) + } else { + reject(err) + } + }) + }) + }, + setCurrCookies(cookies = [], currWin: BrowserView | BrowserWindow) { + let currSession = currWin.webContents.session + let err = [] + let apiCount = 0 + return new Promise((resove, reject) => { + cookies.forEach(async (item: any) => { + await currSession.cookies.set( + Object.assign({}, item, { + url: `http://${item.domain}`, + name: item.name, + }), + ) + apiCount = apiCount + 1 + if (err.length === apiCount) { + resove({ message: "cookie 设置成功!" }) + } else { + reject(err) + } + }) + }) + }, +} + +export default cookies \ No newline at end of file diff --git a/src/main/utils/session/index.ts b/src/main/utils/session/index.ts new file mode 100644 index 0000000..89974e7 --- /dev/null +++ b/src/main/utils/session/index.ts @@ -0,0 +1,63 @@ +// https://blog.guowenfh.com/2017/10/21/2017/electron-multiple-session/#%E5%9C%A8-webview-%E4%B8%AD + +import { BrowserWindow } from "electron" +/** + * 创建一个 登录 的窗口。 + * 用于 session 隔离 + * Promise 中有 {partition,userinfo,cookies} + * @returns Promise + */ +function createLoginWin(partition) { + partition = partition || `persist:${Math.random()}` + const charset = require("superagent-charset") + const request = charset(require("superagent")) // HTTP + let presWindow = new BrowserWindow({ + width: 1280, + height: 768, + title: "用户登陆", + webPreferences: { + webSecurity: false, + allowRunningInsecureContent: true, + partition, + }, + }) + let webContents = presWindow.webContents + return new Promise(function (resove, reject) { + // webContents.openDevTools(); + presWindow.loadURL("http://taobao.com/#/login") + webContents.on("did-navigate-in-page", async function () { + // 这里可以看情况进行参数的传递,获取制定的 cookies + const cookies = await webContents.session.cookies.get({}) + let obj = { partition, cookies } + resove(obj) + // webContents.session.cookies.get({}, function (err, cookies) { + // if (err) { + // presWindow.close() // 关闭登陆窗口 + // return reject(err) + // } + // let obj = { partition, cookies } + // resove(obj) + // 这一步并不是必需的。 + // request + // .get("http://taobao.com/userinfo") + // .query({ _: Date.now() }) // query string + // .set("Cookie", cookies.map(item => `${item.name}=${item.value};`).join(" ")) + // .end(function (err, res) { + // presWindow.close() + // if (err) { + // return reject(err) + // } + // if (!res || !res.body || !res.body.result !== 1) { + // return reject(res.body) + // } + // let obj = { partition, cookies, userinfo: res.body.data } + // resove(obj) + // }) + }) + // }) + }) +} + +export { + createLoginWin +} \ No newline at end of file diff --git a/src/renderer/src/components/NavBar.vue b/src/renderer/src/components/NavBar.vue index 10facc2..194ab82 100644 --- a/src/renderer/src/components/NavBar.vue +++ b/src/renderer/src/components/NavBar.vue @@ -114,6 +114,18 @@ import { LogLevel } from "logger/common" }, }, { + label: "打开研发云", + async click() { + PlatForm.showSrd() + }, + }, + { + label: "打开研发云Cookie", + async click() { + PlatForm.getSrdCookie() + }, + }, + { label: curLogLevel.value === LogLevel.TRACE ? "关闭调试模式" : "开启调试模式", async click() { if(curLogLevel.value === LogLevel.TRACE) {