238 lines
6.6 KiB
Go
238 lines
6.6 KiB
Go
//go:build windows
|
||||
|
|
|
|||
|
|
package main
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"embed"
|
|||
|
|
"errors"
|
|||
|
|
"flag"
|
|||
|
|
"fmt"
|
|||
|
|
"io/fs"
|
|||
|
|
"log/slog"
|
|||
|
|
"net/http"
|
|||
|
|
"os"
|
|||
|
|
"os/exec"
|
|||
|
|
"os/signal"
|
|||
|
|
"path/filepath"
|
|||
|
|
"syscall"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"bj_power_mes/internal/handler"
|
|||
|
|
"bj_power_mes/internal/middleware"
|
|||
|
|
|
|||
|
|
xhttp "bj_power_mes/common/httpx"
|
|||
|
|
xlog "bj_power_mes/common/logx"
|
|||
|
|
"bj_power_mes/internal/config"
|
|||
|
|
"bj_power_mes/internal/svc"
|
|||
|
|
|
|||
|
|
"github.com/dromara/carbon/v2"
|
|||
|
|
"github.com/getlantern/systray"
|
|||
|
|
"github.com/pkg/browser"
|
|||
|
|
"github.com/zeromicro/go-zero/core/conf"
|
|||
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|||
|
|
"github.com/zeromicro/go-zero/rest"
|
|||
|
|
"github.com/zeromicro/go-zero/rest/httpx"
|
|||
|
|
"golang.org/x/sys/windows"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
const AppName = "bj_power_mes"
|
|||
|
|
|
|||
|
|
var configFile = flag.String("f", "etc/bj_power_mes-api.yaml", "the config file")
|
|||
|
|
|
|||
|
|
var AppPath, _ = filepath.Abs(os.Args[0])
|
|||
|
|
|
|||
|
|
var baseUrl string
|
|||
|
|
|
|||
|
|
//go:embed public
|
|||
|
|
var public embed.FS
|
|||
|
|
|
|||
|
|
//go:embed app.ico
|
|||
|
|
var icon []byte
|
|||
|
|
|
|||
|
|
func main() {
|
|||
|
|
flag.Parse()
|
|||
|
|
|
|||
|
|
// 业务影�:所有时间显示使用北京时� carbon.SetDefault(carbon.Default{
|
|||
|
|
Layout: carbon.DateTimeLayout,
|
|||
|
|
Timezone: carbon.PRC,
|
|||
|
|
Locale: "zh-CN",
|
|||
|
|
WeekStartsAt: carbon.Monday,
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
var c config.Config
|
|||
|
|
conf.MustLoad(*configFile, &c)
|
|||
|
|
baseUrl = fmt.Sprintf("http://127.0.0.1:%d", c.Port)
|
|||
|
|
|
|||
|
|
logger := xlog.NewLogger(c.Logger)
|
|||
|
|
xlog.SetDefault(logger)
|
|||
|
|
logx.SetWriter(logger)
|
|||
|
|
|
|||
|
|
// 业务影å“�:防æ¢å�Œæ—¶å�¯åŠ¨å¤šä¸ªå®žä¾‹ï¼Œé�¿å…�端å�£å†²çª�å’?PLC 连接冲çª�
|
|||
|
|
mutex, err := createMutex(AppName)
|
|||
|
|
if err != nil {
|
|||
|
|
if errors.Is(err, ErrAlreadyExists) {
|
|||
|
|
_ = browser.OpenURL(baseUrl)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
MessageBox("错误", err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
defer procCloseHandle.Call(mutex)
|
|||
|
|
|
|||
|
|
// 业务影�:首次�行时自动创建桌�和开始��快�方� createShortcuts()
|
|||
|
|
|
|||
|
|
// 业务影å“�:自动写入注册表开机å�¯åŠ¨ï¼Œæ— éœ€ç”¨æˆ·æ‰‹åŠ¨å‹¾é€? if err := setAutoStart(AppName, AppPath); err != nil {
|
|||
|
|
slog.Warn("开机�动设置失� " + err.Error())
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 业务影�:�始化数�库连接�PLC 连接
|
|||
|
|
svcCtx := svc.NewServiceContext(c)
|
|||
|
|
|
|||
|
|
spaHandler := xhttp.NewNotFoundHandler(http.FS(mustSub(public, "public")))
|
|||
|
|
|
|||
|
|
server := rest.MustNewServer(
|
|||
|
|
c.RestConf,
|
|||
|
|
rest.WithCors("*"),
|
|||
|
|
rest.WithNotFoundHandler(spaHandler),
|
|||
|
|
)
|
|||
|
|
logx.DisableStat()
|
|||
|
|
logx.SetWriter(logger)
|
|||
|
|
defer server.Stop()
|
|||
|
|
|
|||
|
|
handler.RegisterHandlers(server, svcCtx)
|
|||
|
|
|
|||
|
|
// 写æ“�作审计ä¸é—´ä»¶ï¼ˆå…œåº•覆盖系统管ç�†ç±» CRUD ç‰æ‰€æœ‰å†™æŽ¥å�£ï¼? server.Use(middleware.WriteAudit(svcCtx))
|
|||
|
|
|
|||
|
|
// 业务影å“�:统一错误å“�åº”æ ¼å¼�,å‰�端ä¸�用处ç�†å¤šç§�é”™è¯¯æ ¼å¼? httpx.SetValidator(xhttp.NewValidator())
|
|||
|
|
httpx.SetOkHandler(xhttp.OkJsonCtx)
|
|||
|
|
httpx.SetErrorHandlerCtx(xhttp.ErrorCtx)
|
|||
|
|
|
|||
|
|
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
|||
|
|
slog.Info(fmt.Sprintf("Starting server at %s:%d...", c.Host, c.Port))
|
|||
|
|
|
|||
|
|
go server.Start()
|
|||
|
|
|
|||
|
|
// ç‰å¾…æœ�务器å�¯åŠ¨å®Œæˆ�å�Žå†�打开æµ�览器,é�¿å…� 404
|
|||
|
|
go func() {
|
|||
|
|
for i := 0; i < 50; i++ {
|
|||
|
|
resp, err := http.Get(baseUrl)
|
|||
|
|
if err == nil {
|
|||
|
|
resp.Body.Close()
|
|||
|
|
_ = browser.OpenURL(baseUrl)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
time.Sleep(200 * time.Millisecond)
|
|||
|
|
}
|
|||
|
|
slog.Warn("server �动超时,跳过自动打开�览�)
|
|||
|
|
}()
|
|||
|
|
|
|||
|
|
// 业务影å“�:Windows å�Žå�°è¿�è¡Œï¼Œé€šè¿‡æ‰˜ç›˜å›¾æ ‡æŽ§åˆ¶é€€å‡?打开网页
|
|||
|
|
systray.Run(onReady, func() {
|
|||
|
|
slog.Info("onExit")
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func mustSub(fsFS embed.FS, dir string) fs.FS {
|
|||
|
|
sub, err := fs.Sub(fsFS, dir)
|
|||
|
|
if err != nil {
|
|||
|
|
panic(err)
|
|||
|
|
}
|
|||
|
|
return sub
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// onReady 系统托盘:用户�打开网页�切�开机自�(需管�员��)�退出程�func onReady() {
|
|||
|
|
systray.SetIcon(icon)
|
|||
|
|
systray.SetTemplateIcon(icon, icon)
|
|||
|
|
systray.SetTitle("北京电力管控系统")
|
|||
|
|
systray.SetTooltip("北京电力管控系统")
|
|||
|
|
|
|||
|
|
mOpen := systray.AddMenuItem("打开管�页�", "")
|
|||
|
|
systray.AddSeparator()
|
|||
|
|
mAutoStart := systray.AddMenuItem("开机��, "")
|
|||
|
|
mQuit := systray.AddMenuItem("退�, "退出��)
|
|||
|
|
|
|||
|
|
// 业务影�:读�注册表��上次的开机�动设置状� if isAutoStartEnabled(AppName) {
|
|||
|
|
mAutoStart.Check()
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
signalChan := make(chan os.Signal, 1)
|
|||
|
|
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
|
|||
|
|
|
|||
|
|
go func() {
|
|||
|
|
for {
|
|||
|
|
select {
|
|||
|
|
case s := <-signalChan:
|
|||
|
|
slog.Info("receive signal: " + s.String())
|
|||
|
|
systray.Quit()
|
|||
|
|
return
|
|||
|
|
case <-mQuit.ClickedCh:
|
|||
|
|
systray.Quit()
|
|||
|
|
return
|
|||
|
|
case <-mOpen.ClickedCh:
|
|||
|
|
err := browser.OpenURL(baseUrl)
|
|||
|
|
if err != nil {
|
|||
|
|
logx.Error(err.Error())
|
|||
|
|
}
|
|||
|
|
case <-mAutoStart.ClickedCh:
|
|||
|
|
if mAutoStart.Checked() {
|
|||
|
|
if err := removeAutoStart(AppName); err != nil {
|
|||
|
|
if errors.Is(err, windows.ERROR_ACCESS_DENIED) {
|
|||
|
|
MessageBox("错误", "请用管ç�†å‘˜æ�ƒé™�è¿�行æ¤ç¨‹åº�ã€?)
|
|||
|
|
} else {
|
|||
|
|
MessageBox("错误", err.Error())
|
|||
|
|
}
|
|||
|
|
slog.Error("设置失败�" + err.Error())
|
|||
|
|
} else {
|
|||
|
|
mAutoStart.Uncheck()
|
|||
|
|
slog.Info("已��功�消自动�行�)
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
if err := setAutoStart(AppName, AppPath); err != nil {
|
|||
|
|
if errors.Is(err, windows.ERROR_ACCESS_DENIED) {
|
|||
|
|
MessageBox("错误", "请用管ç�†å‘˜æ�ƒé™�è¿�行æ¤ç¨‹åº�ã€?)
|
|||
|
|
} else {
|
|||
|
|
MessageBox("错误", err.Error())
|
|||
|
|
}
|
|||
|
|
slog.Error("设置失败�" + err.Error())
|
|||
|
|
} else {
|
|||
|
|
mAutoStart.Check()
|
|||
|
|
slog.Info("已��功设置为自动�行�)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}()
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// createShortcuts 首次è¿�行时自动创建桌é�¢å’Œå¼€å§‹è�œå�•å¿«æ�·æ–¹å¼�ã€?// 如果快æ�·æ–¹å¼�å·²å˜åœ¨åˆ™è·³è¿‡ï¼Œåˆ›å»ºå¤±è´¥ä¸�阻塞程åº�å�¯åЍã€?func createShortcuts() {
|
|||
|
|
shortcutName := "北京电力管控系统"
|
|||
|
|
|
|||
|
|
exePath, err := os.Executable()
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
exeDir := filepath.Dir(exePath)
|
|||
|
|
iconPath := filepath.Join(exeDir, "app.ico")
|
|||
|
|
|
|||
|
|
// 桌�快�方�
|
|||
|
|
desktopDir, _ := os.UserHomeDir()
|
|||
|
|
desktopPath := filepath.Join(desktopDir, "Desktop", shortcutName+".lnk")
|
|||
|
|
makeShortcut(desktopPath, exePath, exeDir, iconPath)
|
|||
|
|
|
|||
|
|
// 开始��快�方� startMenuPath := filepath.Join(os.Getenv("APPDATA"),
|
|||
|
|
"Microsoft", "Windows", "Start Menu", "Programs", shortcutName+".lnk")
|
|||
|
|
makeShortcut(startMenuPath, exePath, exeDir, iconPath)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// makeShortcut 通过 PowerShell 创建 .lnk å¿«æ�·æ–¹å¼�ã€?// 如果文件已å˜åœ¨åˆ™è·³è¿‡ï¼Œåˆ›å»ºå¤±è´¥ä¸�阻塞ã€?func makeShortcut(shortcutPath, targetPath, workDir, iconPath string) {
|
|||
|
|
if _, err := os.Stat(shortcutPath); err == nil {
|
|||
|
|
return // å·²å˜åœ¨ï¼Œè·³è¿‡
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
script := fmt.Sprintf(
|
|||
|
|
`$s=(New-Object -ComObject WScript.Shell).CreateShortcut('%s');$s.TargetPath='%s';$s.WorkingDirectory='%s';$s.IconLocation='%s';$s.Save()`,
|
|||
|
|
shortcutPath, targetPath, workDir, iconPath,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
cmd := exec.Command("powershell", "-ExecutionPolicy", "Bypass", "-NoProfile", "-Command", script)
|
|||
|
|
_ = cmd.Run() // 创建失败�阻塞��}
|