UNPKG

maxme-electron

Version:

The electron wrap of MaxME.

239 lines (205 loc) 6.34 kB
// Modules to control application life and create native browser window const {app, BrowserWindow, ipcMain} = require('electron') const storage = require('electron-localstorage') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let mainWindow = null global.userName = storage.getItem('userName') global.avatar = storage.getItem('avatar') global.token = storage.getItem('token') global.expires = storage.getItem('expires') global.mainWin = null function getClientToken() { let options = { hostname: 'u.maxhub.vip', path: '/api/pauth/token?grant_type=client_credentials&state=logining', method: 'POST', headers: { 'Authorization': 'Basic YWFlN2UyNDQtYWU5Ni00NGY3LWJlMzItM2ZmYTk5NzlmNjI0Okw0eTBJZGhRSHRydm1EQzY=' } } let data='' return new Promise(function (resolve, reject) { let req = require('https').request(options, function(res){ res.setEncoding('utf-8'); res.on('data', function(chunk) { data += chunk; }) res.on('end', function() { resolve({result: true, data: data}); }) }) req.on('error', (e) => { resolve({result: false, errmsg: e.message}); }); req.end() }) } function getAccessToken(clientToken, code) { let options = { hostname: 'u.maxhub.vip', path: '/api/pauth/token?grant_type=authorization_code&code='+ code, method: 'POST', headers: { 'Authorization': 'Bearer '+ clientToken } } let data='' return new Promise(function (resolve, reject) { let req = require('https').request(options, function(res){ res.setEncoding('utf-8'); res.on('data', function(chunk) { data += chunk; }) res.on('end', function() { resolve({result: true, data: data}); }) }) req.on('error', (e) => { resolve({result: false, errmsg: e.message}); }); req.end() }) } function getUserinfo(clientToken, token) { let options = { hostname: 'u.maxhub.vip', path: '/api/client/auth/userinfo?access_token='+ token, method: 'GET', headers: { 'Authorization': 'Bearer '+ clientToken } } let data='' return new Promise(function (resolve, reject) { let req = require('https').request(options, function(res){ res.setEncoding('utf-8'); res.on('data', function(chunk) { data += chunk; }) res.on('end', function() { resolve({result: true, data: data}); }) }) req.on('error', (e) => { resolve({result: false, errmsg: e.message}); }); req.end() }) } async function authorize(code) { let rlt = await getClientToken() if (!rlt.result) { return -1 } let data = JSON.parse(rlt.data) if (data.access_token == undefined) { return -1 } const clientToken = data.access_token rlt = await getAccessToken(clientToken, code) if (!rlt.result) { return -1 } data = JSON.parse(rlt.data) if (data.access_token == undefined) { return -1 } const token = data.access_token const expires = new Date().getTime() + (data.expires_in - 1200) * 1000 rlt = await getUserinfo(clientToken, token) if (!rlt.result) { return -1 } data = JSON.parse(rlt.data) global.userName = data.displayName global.avatar = data.avatar global.token = token global.expires = expires storage.setItem('token', global.token) storage.setItem('userName', global.userName) storage.setItem('expires', global.expires) storage.setItem('avatar', global.avatar) return 0 } function authenticated() { const time = new Date().getTime() return (global.token && global.expires && global.userName && global.avatar && time < global.expires) } function createWindow() { if (authenticated()) { mainWindow = new BrowserWindow( { width: 1120, height: 630, resizable: false, useContentSize:true, autoHideMenuBar:true, backgroundColor:'black', webPreferences: { webSecurity: false } }) // and load the index.html of the app. mainWindow.loadURL(`file://${__dirname}/pub/index.html`) } else { mainWindow = new BrowserWindow( { width:540, height:320, resizable:false, autoHideMenuBar:true, backgroundColor:'black', webPreferences: { webSecurity: false } }) // and load the index.html of the app. mainWindow.loadURL('https://u.maxhub.vip/api/pauth/authorize?response_type=code&scope=basic&client_id=aae7e244-ae96-44f7-be32-3ffa9979f624&redirect_uri=http://localhost/login_success&state=login&platform=pc-app') mainWindow.webContents.on('will-navigate', async function (ev, url) { let decodedUrl = require('url').parse(url) let pathname = decodedUrl.pathname let args = require("querystring").parse(decodedUrl.query) let code = undefined if (pathname == '/thirdparty/weixin/callback/userinfoqr') { code = args.state } else if(pathname == '/login_success') { code = args.code } if (code) { app.relaunch(); } }) } // Open the DevTools. // mainWindow.webContents.openDevTools() mainWindow.once('ready-to-show', function(){ mainWindow.show() }) // Emitted when the window is closed. mainWindow.on('closed', function () { // Dereference the window object, usually you would store windows // in an array if your app supports multi windows, this is the time // when you should delete the corresponding element. mainWindow = null }) } // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.on('ready', createWindow) // Quit when all windows are closed. app.on('window-all-closed', function () { // On macOS it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform !== 'darwin') { app.quit() } }) app.on('activate', function () { // On macOS it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (mainWindow === null) { createWindow() } })