| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- // ============================================================================
- //go:build !service
- // power_shutdown.go — 延时关机(工具页)
- //
- // 用户在工具页输入「多少分钟后关机」,调用 ShutdownDelay 下发系统关机倒计时。
- // 复用 pccontrol.go 的系统命令构造(shutdown /s /t N /f),交给 Windows 系统计时,
- // 即使本程序退出,倒计时仍由系统执行。返回计划执行时间供前端展示。
- // ============================================================================
- package main
- import (
- "fmt"
- "strings"
- "time"
- )
- // ShutdownDelay 在指定分钟后执行关机。
- // minutes 必须 > 0;返回计划执行时间(HH:MM:SS)与提示信息。
- func (s *SettingsService) ShutdownDelay(minutes int) (string, error) {
- if minutes <= 0 {
- return "", fmt.Errorf("关机分钟数必须大于 0")
- }
- if minutes > 24*60 {
- return "", fmt.Errorf("关机分钟数过大(最多 24 小时)")
- }
- delaySec := minutes * 60
- cmd := pcSystemCommand(pcPoweroff, delaySec)
- if cmd == nil {
- return "", fmt.Errorf("当前系统不支持 shutdown 命令")
- }
- // 标记为待执行(与 API 路由共用状态,便于「主机」页取消)
- pcMu.Lock()
- if pcPending {
- remaining := time.Until(pcExecuteAt).Round(time.Second)
- pcMu.Unlock()
- return "", fmt.Errorf("已有待执行的电源命令,剩余 %v", remaining)
- }
- pcPending = true
- pcPendingCmd = pcPoweroff
- pcExecuteAt = time.Now().Add(time.Duration(delaySec) * time.Second)
- pcSystemTimer = true
- pcLastCmd = pcPoweroff
- pcLastTime = time.Now()
- pcRequested = true
- pcMu.Unlock()
- if output, err := cmd.CombinedOutput(); err != nil {
- pcMu.Lock()
- pcPending = false
- pcPendingCmd = ""
- pcExecuteAt = time.Time{}
- pcSystemTimer = false
- pcMu.Unlock()
- return "", fmt.Errorf("关机命令下发失败: %v | %s", err, strings.TrimSpace(string(output)))
- }
- execAt := pcExecuteAt.Format("15:04:05")
- fmt.Fprintf(apiLogRing, "[SYS] 工具页延时关机 | %d 分钟后(%s)\n", minutes, execAt)
- return execAt, nil
- }
- // PoweroffNow 立即关闭计算机(托盘菜单「关机」确认后调用)。
- // 与工具页延时关机共用 pccontrol 的 pending 状态,避免与倒计时冲突。
- func (s *SettingsService) PoweroffNow() error {
- cmd := pcSystemCommand(pcPoweroff, 0)
- if cmd == nil {
- return fmt.Errorf("当前系统不支持 shutdown 命令")
- }
- pcMu.Lock()
- if pcPending {
- remaining := time.Until(pcExecuteAt).Round(time.Second)
- pcMu.Unlock()
- return fmt.Errorf("已有待执行的电源命令,剩余 %v", remaining)
- }
- // 立即关机无需系统计时,但记录状态供「主机」页展示与取消(取消仅影响系统倒计时,这里直接执行)
- pcPending = true
- pcPendingCmd = pcPoweroff
- pcExecuteAt = time.Now()
- pcSystemTimer = false
- pcLastCmd = pcPoweroff
- pcLastTime = time.Now()
- pcRequested = true
- pcMu.Unlock()
- if output, err := cmd.CombinedOutput(); err != nil {
- pcMu.Lock()
- pcPending = false
- pcPendingCmd = ""
- pcExecuteAt = time.Time{}
- pcMu.Unlock()
- return fmt.Errorf("关机命令下发失败: %v | %s", err, strings.TrimSpace(string(output)))
- }
- fmt.Fprintln(apiLogRing, "[SYS] 托盘菜单立即关机")
- return nil
- }
- // SleepNow 立即休眠(一键功能按钮调用)。
- // 复用 pccontrol 的立即执行路径(rundll32 SetSuspendState),异步执行避免阻塞绑定调用。
- func (s *SettingsService) SleepNow() (string, error) {
- pcMu.Lock()
- pcLastCmd = pcSleep
- pcLastTime = time.Now()
- pcRequested = true
- pcMu.Unlock()
- go pcExecuteNow(pcSleep)
- return "已触发休眠", nil
- }
- // ToggleDesktopIcons 隐藏/显示桌面图标(一键功能按钮调用)。
- // hide=true 隐藏,hide=false 显示;返回操作结果描述。
- func (s *SettingsService) ToggleDesktopIcons(hide bool) (string, error) {
- return setDesktopIconsVisible(hide)
- }
- // ToggleDesktopIconOnly 只隐藏/显示桌面图标(壁纸保留;一键功能「隐藏图标」按钮调用)。
- // hide=true 只隐藏图标(壁纸不动),hide=false 恢复显示;返回操作结果描述。
- func (s *SettingsService) ToggleDesktopIconOnly(hide bool) (string, error) {
- return setDesktopIconOnlyVisible(hide)
- }
- // CancelShutdown 取消延时关机(工具页「取消」按钮调用)。
- func (s *SettingsService) CancelShutdown() error {
- pcMu.Lock()
- if !pcPending || pcPendingCmd != pcPoweroff {
- pcMu.Unlock()
- return fmt.Errorf("当前没有待执行的关机命令")
- }
- pcPending = false
- pcPendingCmd = ""
- pcExecuteAt = time.Time{}
- pcSystemTimer = false
- pcMu.Unlock()
- pcAbortShutdown()
- fmt.Fprintln(apiLogRing, "[SYS] 工具页取消延时关机")
- return nil
- }
|