mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-03-24 23:06:51 +00:00
feat: 保存api服务的配置,实现随weflow静默启动
This commit is contained in:
@@ -2825,6 +2825,8 @@ app.whenReady().then(async () => {
|
|||||||
// 启动时检测更新(不阻塞启动)
|
// 启动时检测更新(不阻塞启动)
|
||||||
checkForUpdatesOnStartup()
|
checkForUpdatesOnStartup()
|
||||||
|
|
||||||
|
await httpService.autoStart()
|
||||||
|
|
||||||
app.on('activate', () => {
|
app.on('activate', () => {
|
||||||
if (BrowserWindow.getAllWindows().length === 0) {
|
if (BrowserWindow.getAllWindows().length === 0) {
|
||||||
mainWindow = createWindow()
|
mainWindow = createWindow()
|
||||||
|
|||||||
@@ -52,6 +52,8 @@ interface ConfigSchema {
|
|||||||
notificationFilterMode: 'all' | 'whitelist' | 'blacklist'
|
notificationFilterMode: 'all' | 'whitelist' | 'blacklist'
|
||||||
notificationFilterList: string[]
|
notificationFilterList: string[]
|
||||||
messagePushEnabled: boolean
|
messagePushEnabled: boolean
|
||||||
|
httpApiEnabled: boolean
|
||||||
|
httpApiPort: number
|
||||||
httpApiToken: string
|
httpApiToken: string
|
||||||
windowCloseBehavior: 'ask' | 'tray' | 'quit'
|
windowCloseBehavior: 'ask' | 'tray' | 'quit'
|
||||||
quoteLayout: 'quote-top' | 'quote-bottom'
|
quoteLayout: 'quote-top' | 'quote-bottom'
|
||||||
@@ -121,6 +123,8 @@ export class ConfigService {
|
|||||||
notificationFilterMode: 'all',
|
notificationFilterMode: 'all',
|
||||||
notificationFilterList: [],
|
notificationFilterList: [],
|
||||||
httpApiToken: '',
|
httpApiToken: '',
|
||||||
|
httpApiEnabled: false,
|
||||||
|
httpApiPort: 5031,
|
||||||
messagePushEnabled: false,
|
messagePushEnabled: false,
|
||||||
windowCloseBehavior: 'ask',
|
windowCloseBehavior: 'ask',
|
||||||
quoteLayout: 'quote-top',
|
quoteLayout: 'quote-top',
|
||||||
@@ -664,11 +668,9 @@ export class ConfigService {
|
|||||||
|
|
||||||
// 即使 authEnabled 被删除/篡改,如果密钥是 lock: 格式,说明曾开启过应用锁
|
// 即使 authEnabled 被删除/篡改,如果密钥是 lock: 格式,说明曾开启过应用锁
|
||||||
const rawDecryptKey: any = this.store.get('decryptKey')
|
const rawDecryptKey: any = this.store.get('decryptKey')
|
||||||
if (typeof rawDecryptKey === 'string' && rawDecryptKey.startsWith(LOCK_PREFIX)) {
|
return typeof rawDecryptKey === 'string' && rawDecryptKey.startsWith(LOCK_PREFIX);
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// === 工具方法 ===
|
// === 工具方法 ===
|
||||||
|
|||||||
@@ -246,6 +246,19 @@ class HttpService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async autoStart(): Promise<void> {
|
||||||
|
const enabled = this.configService.get('httpApiEnabled')
|
||||||
|
if (enabled) {
|
||||||
|
const port = Number(this.configService.get('httpApiPort')) || 5031
|
||||||
|
try {
|
||||||
|
await this.start(port)
|
||||||
|
console.log(`[HttpService] Auto-started on port ${port}`)
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[HttpService] Auto-start failed:', err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析 POST 请求的 JSON Body
|
* 解析 POST 请求的 JSON Body
|
||||||
*/
|
*/
|
||||||
@@ -282,9 +295,9 @@ class HttpService {
|
|||||||
if (queryToken && queryToken.trim() === expectedToken) return true
|
if (queryToken && queryToken.trim() === expectedToken) return true
|
||||||
|
|
||||||
const bodyToken = body['access_token']
|
const bodyToken = body['access_token']
|
||||||
if (bodyToken && String(bodyToken).trim() === expectedToken) return true
|
return !!(bodyToken && String(bodyToken).trim() === expectedToken);
|
||||||
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -130,6 +130,8 @@ function SettingsPage({ onClose }: SettingsPageProps = {}) {
|
|||||||
showMessage('已清除 Access Token,API 将允许无鉴权访问', true)
|
showMessage('已清除 Access Token,API 将允许无鉴权访问', true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const [autoTranscribeVoice, setAutoTranscribeVoice] = useState(false)
|
const [autoTranscribeVoice, setAutoTranscribeVoice] = useState(false)
|
||||||
const [transcribeLanguages, setTranscribeLanguages] = useState<string[]>(['zh'])
|
const [transcribeLanguages, setTranscribeLanguages] = useState<string[]>(['zh'])
|
||||||
|
|
||||||
@@ -344,6 +346,9 @@ function SettingsPage({ onClose }: SettingsPageProps = {}) {
|
|||||||
const savedHttpApiToken = await configService.getHttpApiToken()
|
const savedHttpApiToken = await configService.getHttpApiToken()
|
||||||
if (savedHttpApiToken) setHttpApiToken(savedHttpApiToken)
|
if (savedHttpApiToken) setHttpApiToken(savedHttpApiToken)
|
||||||
|
|
||||||
|
const savedApiPort = await configService.getHttpApiPort()
|
||||||
|
if (savedApiPort) setHttpApiPort(savedApiPort)
|
||||||
|
|
||||||
setAuthEnabled(savedAuthEnabled)
|
setAuthEnabled(savedAuthEnabled)
|
||||||
setAuthUseHello(savedAuthUseHello)
|
setAuthUseHello(savedAuthUseHello)
|
||||||
setIsLockMode(savedIsLockMode)
|
setIsLockMode(savedIsLockMode)
|
||||||
@@ -386,6 +391,8 @@ function SettingsPage({ onClose }: SettingsPageProps = {}) {
|
|||||||
const savedAnalyticsConsent = await configService.getAnalyticsConsent()
|
const savedAnalyticsConsent = await configService.getAnalyticsConsent()
|
||||||
setAnalyticsConsent(savedAnalyticsConsent ?? false)
|
setAnalyticsConsent(savedAnalyticsConsent ?? false)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 如果语言列表为空,保存默认值
|
// 如果语言列表为空,保存默认值
|
||||||
if (!savedTranscribeLanguages || savedTranscribeLanguages.length === 0) {
|
if (!savedTranscribeLanguages || savedTranscribeLanguages.length === 0) {
|
||||||
const defaultLanguages = ['zh']
|
const defaultLanguages = ['zh']
|
||||||
@@ -1850,6 +1857,7 @@ function SettingsPage({ onClose }: SettingsPageProps = {}) {
|
|||||||
try {
|
try {
|
||||||
await window.electronAPI.http.stop()
|
await window.electronAPI.http.stop()
|
||||||
setHttpApiRunning(false)
|
setHttpApiRunning(false)
|
||||||
|
await configService.setHttpApiEnabled(false)
|
||||||
showMessage('API 服务已停止', true)
|
showMessage('API 服务已停止', true)
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
showMessage(`操作失败: ${e}`, false)
|
showMessage(`操作失败: ${e}`, false)
|
||||||
@@ -1867,6 +1875,10 @@ function SettingsPage({ onClose }: SettingsPageProps = {}) {
|
|||||||
if (result.success) {
|
if (result.success) {
|
||||||
setHttpApiRunning(true)
|
setHttpApiRunning(true)
|
||||||
if (result.port) setHttpApiPort(result.port)
|
if (result.port) setHttpApiPort(result.port)
|
||||||
|
|
||||||
|
await configService.setHttpApiEnabled(true)
|
||||||
|
await configService.setHttpApiPort(result.port || httpApiPort)
|
||||||
|
|
||||||
showMessage(`API 服务已启动,端口 ${result.port}`, true)
|
showMessage(`API 服务已启动,端口 ${result.port}`, true)
|
||||||
} else {
|
} else {
|
||||||
showMessage(`启动失败: ${result.error}`, false)
|
showMessage(`启动失败: ${result.error}`, false)
|
||||||
@@ -1915,14 +1927,18 @@ function SettingsPage({ onClose }: SettingsPageProps = {}) {
|
|||||||
<label>服务端口</label>
|
<label>服务端口</label>
|
||||||
<span className="form-hint">API 服务监听的端口号(1024-65535)</span>
|
<span className="form-hint">API 服务监听的端口号(1024-65535)</span>
|
||||||
<input
|
<input
|
||||||
type="number"
|
type="number"
|
||||||
className="field-input"
|
className="field-input"
|
||||||
value={httpApiPort}
|
value={httpApiPort}
|
||||||
onChange={(e) => setHttpApiPort(parseInt(e.target.value, 10) || 5031)}
|
onChange={(e) => {
|
||||||
disabled={httpApiRunning}
|
const port = parseInt(e.target.value, 10) || 5031
|
||||||
style={{ width: 120 }}
|
setHttpApiPort(port)
|
||||||
min={1024}
|
scheduleConfigSave('httpApiPort', () => configService.setHttpApiPort(port))
|
||||||
max={65535}
|
}}
|
||||||
|
disabled={httpApiRunning}
|
||||||
|
style={{ width: 120 }}
|
||||||
|
min={1024}
|
||||||
|
max={65535}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -65,6 +65,8 @@ export const CONFIG_KEYS = {
|
|||||||
NOTIFICATION_FILTER_MODE: 'notificationFilterMode',
|
NOTIFICATION_FILTER_MODE: 'notificationFilterMode',
|
||||||
NOTIFICATION_FILTER_LIST: 'notificationFilterList',
|
NOTIFICATION_FILTER_LIST: 'notificationFilterList',
|
||||||
HTTP_API_TOKEN: 'httpApiToken',
|
HTTP_API_TOKEN: 'httpApiToken',
|
||||||
|
HTTP_API_ENABLED: 'httpApiEnabled',
|
||||||
|
HTTP_API_PORT: 'httpApiPort',
|
||||||
MESSAGE_PUSH_ENABLED: 'messagePushEnabled',
|
MESSAGE_PUSH_ENABLED: 'messagePushEnabled',
|
||||||
WINDOW_CLOSE_BEHAVIOR: 'windowCloseBehavior',
|
WINDOW_CLOSE_BEHAVIOR: 'windowCloseBehavior',
|
||||||
QUOTE_LAYOUT: 'quoteLayout',
|
QUOTE_LAYOUT: 'quoteLayout',
|
||||||
@@ -1484,3 +1486,26 @@ export async function getAnalyticsDenyCount(): Promise<number> {
|
|||||||
export async function setAnalyticsDenyCount(count: number): Promise<void> {
|
export async function setAnalyticsDenyCount(count: number): Promise<void> {
|
||||||
await config.set(CONFIG_KEYS.ANALYTICS_DENY_COUNT, count)
|
await config.set(CONFIG_KEYS.ANALYTICS_DENY_COUNT, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 获取 HTTP API 自动启动状态
|
||||||
|
export async function getHttpApiEnabled(): Promise<boolean> {
|
||||||
|
const value = await config.get(CONFIG_KEYS.HTTP_API_ENABLED)
|
||||||
|
return value === true
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置 HTTP API 自动启动状态
|
||||||
|
export async function setHttpApiEnabled(enabled: boolean): Promise<void> {
|
||||||
|
await config.set(CONFIG_KEYS.HTTP_API_ENABLED, enabled)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取 HTTP API 端口
|
||||||
|
export async function getHttpApiPort(): Promise<number> {
|
||||||
|
const value = await config.get(CONFIG_KEYS.HTTP_API_PORT)
|
||||||
|
return typeof value === 'number' ? value : 5031
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置 HTTP API 端口
|
||||||
|
export async function setHttpApiPort(port: number): Promise<void> {
|
||||||
|
await config.set(CONFIG_KEYS.HTTP_API_PORT, port)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user