fix(system): 修复开机自启相关功能

This commit is contained in:
DLmaster361
2025-07-12 16:08:07 +08:00
parent 6d3fda50d3
commit 4efbafc174
3 changed files with 100 additions and 37 deletions

View File

@@ -706,7 +706,7 @@ class GeneralSubConfig(LQConfig):
class AppConfig(GlobalConfig):
VERSION = "4.4.0.4"
VERSION = "4.4.0.5"
stage_refreshed = Signal()
PASSWORD_refreshed = Signal()
@@ -717,7 +717,7 @@ class AppConfig(GlobalConfig):
super().__init__()
self.app_path = Path(sys.argv[0]).resolve().parent # 获取软件根目录
self.app_path_sys = str(Path(sys.argv[0]).resolve()) # 获取软件自身的路径
self.app_path_sys = Path(sys.argv[0]).resolve() # 获取软件自身的路径
self.log_path = self.app_path / "debug/AUTO_MAA.log"
self.database_path = self.app_path / "data/data.db"

View File

@@ -31,9 +31,11 @@ import sys
import ctypes
import win32gui
import win32process
import winreg
import psutil
import subprocess
import tempfile
import getpass
from datetime import datetime
from pathlib import Path
from app.core import Config
@@ -61,44 +63,103 @@ class _SystemHandler:
# 恢复系统电源状态
ctypes.windll.kernel32.SetThreadExecutionState(self.ES_CONTINUOUS)
def set_SelfStart(self) -> bool:
def set_SelfStart(self) -> None:
"""同步开机自启"""
if Config.get(Config.start_IfSelfStart) and not self.is_startup():
# 创建任务计划
try:
# 创建任务计划
result = subprocess.run(
[
"schtasks",
"/create",
"/tn",
"AUTO_MAA_AutoStart",
"/tr",
Config.app_path_sys,
"/sc",
"onlogon",
"/rl",
"highest", # 以最高权限运行
"/f", # 强制创建(覆盖现有任务)
],
creationflags=subprocess.CREATE_NO_WINDOW,
stdin=subprocess.DEVNULL,
capture_output=True,
text=True,
)
# 获取当前用户和时间
current_user = getpass.getuser()
current_time = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
if result.returncode == 0:
logger.info(f"任务计划程序自启动已创建: {Config.app_path_sys}")
return True
else:
logger.error(f"创建任务计划失败: {result.stderr}")
return False
# XML 模板
xml_content = f"""<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>{current_time}</Date>
<Author>{current_user}</Author>
<Description>AUTO_MAA自启动服务</Description>
<URI>\\AUTO_MAA_AutoStart</URI>
</RegistrationInfo>
<Triggers>
<LogonTrigger>
<StartBoundary>{current_time}</StartBoundary>
<Enabled>true</Enabled>
</LogonTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<LogonType>InteractiveToken</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>false</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>"{Config.app_path_sys}"</Command>
</Exec>
</Actions>
</Task>"""
# 创建临时 XML 文件并执行
with tempfile.NamedTemporaryFile(
mode="w", suffix=".xml", delete=False, encoding="utf-16"
) as f:
f.write(xml_content)
xml_file = f.name
try:
result = subprocess.run(
[
"schtasks",
"/create",
"/tn",
"AUTO_MAA_AutoStart",
"/xml",
xml_file,
"/f",
],
creationflags=subprocess.CREATE_NO_WINDOW,
stdin=subprocess.DEVNULL,
capture_output=True,
text=True,
)
if result.returncode == 0:
logger.info(f"任务计划程序自启动已创建: {Config.app_path_sys}")
else:
logger.error(f"创建任务计划失败: {result.stderr}")
finally:
# 删除临时文件
try:
Path(xml_file).unlink()
except:
pass
except Exception as e:
logger.error(f"设置任务计划程序自启动失败: {e}")
return False
logger.exception(f"设置任务计划程序自启动失败: {e}")
elif not Config.get(Config.start_IfSelfStart) and self.is_startup():
@@ -114,14 +175,11 @@ class _SystemHandler:
if result.returncode == 0:
logger.info("任务计划程序自启动已删除")
return True
else:
logger.error(f"删除任务计划失败: {result.stderr}")
return False
except Exception as e:
logger.error(f"删除任务计划程序自启动失败: {e}")
return False
logger.exception(f"删除任务计划程序自启动失败: {e}")
def set_power(self, mode) -> None:

View File

@@ -1,6 +1,11 @@
{
"main_version": "4.4.0.4",
"main_version": "4.4.0.5",
"version_info": {
"4.4.0.5": {
"修复BUG": [
"修复开机自启相关功能"
]
},
"4.4.0.4": {
"新增功能": [
"添加重置管理密钥功能"