feat: 新增基础配置获取

This commit is contained in:
gaze 2024-07-14 02:53:51 +08:00
parent c2a806ed0b
commit ab6cdfb797
5 changed files with 78 additions and 10 deletions

View File

@ -12,6 +12,7 @@ asarUnpack:
- resources/** - resources/**
win: win:
executableName: lol-assistant executableName: lol-assistant
requestedExecutionLevel: requireAdministrator
nsis: nsis:
artifactName: ${name}-${version}-setup.${ext} artifactName: ${name}-${version}-setup.${ext}
shortcutName: ${productName} shortcutName: ${productName}

View File

@ -9,13 +9,18 @@
"format": "prettier --write .", "format": "prettier --write .",
"lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix", "lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix",
"start": "electron-vite preview", "start": "electron-vite preview",
"dev": "electron-vite dev", "dev": "powershell -Command \"Start-Process cmd -ArgumentList '/k', 'cd %cd% && npm run dev:base' -Verb RunAs\"",
"dev:base": "electron-vite dev",
"build": "electron-vite build", "build": "electron-vite build",
"postinstall": "electron-builder install-app-deps", "postinstall": "electron-builder install-app-deps",
"build:unpack": "npm run build && electron-builder --dir", "build:unpack:base": "npm run build && electron-builder --dir",
"build:win": "npm run build && electron-builder --win", "build:win:base": "npm run build && electron-builder --win",
"build:mac": "npm run build && electron-builder --mac", "build:mac:base": "npm run build && electron-builder --mac",
"build:linux": "npm run build && electron-builder --linux" "build:linux:base": "npm run build && electron-builder --linux",
"build:unpack": "powershell -Command \"Start-Process cmd -ArgumentList '/k', 'cd %cd% && npm run build:unpack:base' -Verb RunAs\"",
"build:win": "powershell -Command \"Start-Process cmd -ArgumentList '/k', 'cd %cd% && npm run build:win:base' -Verb RunAs\"",
"build:mac": "powershell -Command \"Start-Process cmd -ArgumentList '/k', 'cd %cd% && npm run build:mac:base' -Verb RunAs\"",
"build:linux": "powershell -Command \"Start-Process cmd -ArgumentList '/k', 'cd %cd% && npm run build:linux:base' -Verb RunAs\""
}, },
"dependencies": { "dependencies": {
"@electron-toolkit/preload": "^3.0.1", "@electron-toolkit/preload": "^3.0.1",

View File

@ -2,6 +2,47 @@ import { app, shell, BrowserWindow, ipcMain } from 'electron'
import { join } from 'path' import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils' import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset' import icon from '../../resources/icon.png?asset'
import { exec } from 'child_process'
const getBaseConfig = () => {
return new Promise((resolve) =>
exec(
"chcp 936 && WMIC PROCESS WHERE name='LeagueClientUx.exe' GET commandline",
(error, stdout, stderr) => {
if (error) {
resolve({
Auth: '',
BASE_URL: ''
})
return
}
if (stderr) {
resolve({
Auth: '',
BASE_URL: ''
})
return
}
const tokenMatch = stdout.match(/--remoting-auth-token=([^\s"]+)/)
const portMatch = stdout.match(/--app-port=([^\s"]+)/)
const token = tokenMatch?.[1] || ''
const auth = Buffer.from(`riot:${token}`).toString('base64')
const port = portMatch?.[1] || ''
resolve(
token !== ''
? {
Auth: `Basic ${auth}`,
BASE_URL: `https://127.0.0.1:${port}`
}
: {
Auth: 'Auth required',
BASE_URL: ''
}
)
}
)
)
}
function createWindow() { function createWindow() {
// Create the browser window. // Create the browser window.
@ -42,6 +83,7 @@ app.whenReady().then(() => {
// Set app user model id for windows // Set app user model id for windows
electronApp.setAppUserModelId('com.electron') electronApp.setAppUserModelId('com.electron')
ipcMain.handle('getBaseConfig', getBaseConfig)
// Default open or close DevTools by F12 in development // Default open or close DevTools by F12 in development
// and ignore CommandOrControl + R in production. // and ignore CommandOrControl + R in production.
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
@ -49,9 +91,6 @@ app.whenReady().then(() => {
optimizer.watchWindowShortcuts(window) optimizer.watchWindowShortcuts(window)
}) })
// IPC test
ipcMain.on('ping', () => console.log('pong'))
createWindow() createWindow()
app.on('activate', function () { app.on('activate', function () {

View File

@ -1,4 +1,4 @@
import { contextBridge } from 'electron' import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload' import { electronAPI } from '@electron-toolkit/preload'
// Custom APIs for renderer // Custom APIs for renderer
@ -11,10 +11,16 @@ if (process.contextIsolated) {
try { try {
contextBridge.exposeInMainWorld('electron', electronAPI) contextBridge.exposeInMainWorld('electron', electronAPI)
contextBridge.exposeInMainWorld('api', api) contextBridge.exposeInMainWorld('api', api)
contextBridge.exposeInMainWorld('electronAPI', {
getBaseConfig: () => ipcRenderer.invoke('getBaseConfig')
})
} catch (error) { } catch (error) {
console.error(error) console.error(error)
} }
} else { } else {
window.electron = electronAPI window.electron = electronAPI
window.api = api window.api = api
window.electronAPI = {
getBaseConfig: () => ipcRenderer.invoke('getBaseConfig')
}
} }

View File

@ -1,7 +1,24 @@
<script setup> <script setup>
import Versions from './components/Versions.vue' import Versions from './components/Versions.vue'
const ipcHandle = () => window.electron.ipcRenderer.send('ping') const ipcHandle = async () => {
const baseConfig = await window.electronAPI.getBaseConfig()
console.log(baseConfig)
}
const intervalId = setInterval(async () => {
const config = await window.electronAPI.getBaseConfig()
//
if (config.Auth && config.Auth === 'Auth required') {
clearInterval(intervalId)
console.log('请以管理员权限启动')
} else if (config.Auth && config.Auth !== '') {
clearInterval(intervalId)
console.log('检测到客户端运行')
}
}, 500)
</script> </script>
<template> <template>