From 7a6c647eda0282d2751188ed6864e45f5a4a8b23 Mon Sep 17 00:00:00 2001 From: chengma Date: Thu, 6 Aug 2026 17:34:56 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=B8=BA=20Admin=20=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E5=A2=9E=E5=8A=A0=E7=83=AD=E9=87=8D=E8=BD=BD?= =?UTF-8?q?=20(#13)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ admin/.air.toml | 24 ++++++++++++++ docs/admin/00-getting-started.md | 16 ++++++++-- run_admin.bat | 55 ++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 admin/.air.toml create mode 100644 run_admin.bat diff --git a/.gitignore b/.gitignore index 141afdc..68418e6 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ dist/ *.spec admin/admin.exe admin/admin +admin/admin-dev.exe +admin/tmp/ # Excel 打开时生成的锁文件 ~$*.xlsx diff --git a/admin/.air.toml b/admin/.air.toml new file mode 100644 index 0000000..c2615d2 --- /dev/null +++ b/admin/.air.toml @@ -0,0 +1,24 @@ +root = "." +tmp_dir = "tmp" + +[build] +cmd = "go build -o ./admin-dev.exe ." +bin = "admin-dev.exe" +include_ext = ["go", "html", "css", "js"] +exclude_dir = ["data", "tmp", "testdata"] +exclude_regex = ["_test\\.go"] +exclude_unchanged = true +delay = 500 +stop_on_error = true +send_interrupt = false +kill_delay = "500ms" + +[log] +time = true + +[misc] +clean_on_exit = true + +[screen] +clear_on_rebuild = false +keep_scroll = true diff --git a/docs/admin/00-getting-started.md b/docs/admin/00-getting-started.md index cfcecf0..7aba6ec 100644 --- a/docs/admin/00-getting-started.md +++ b/docs/admin/00-getting-started.md @@ -67,6 +67,20 @@ go get github.com/xuri/excelize/v2@v2.9.1 ## 3. 跑起来 +推荐在仓库根目录运行热重载脚本: + +```powershell +cd D:\chengma\cmautobuy +.\run_admin.bat +``` + +脚本第一次运行时会自动安装与 Go 1.23 兼容的 +`github.com/air-verse/air@v1.61.7`,以后会直接使用已安装的固定版本。 +保存 Go、HTML、CSS 或 JavaScript 文件后,Air 会自动重新编译并重启 Admin; +按 `Ctrl+C` 停止。 + +如果只想临时运行一次、不需要热重载,也可以: + ```powershell cd D:\chengma\cmautobuy\admin go run . @@ -76,8 +90,6 @@ go run . **这个命令是安全的**:Admin 只管理数据,不会连手机、不会下单。放心随便跑。 -改了代码要重启才生效(Go 不像 Python 那样改完就生效)。按 `Ctrl+C` 停掉再 `go run .`。 - ## 4. 你应该看到什么 左边(或顶部)是四个模块的导航: diff --git a/run_admin.bat b/run_admin.bat new file mode 100644 index 0000000..68a06d4 --- /dev/null +++ b/run_admin.bat @@ -0,0 +1,55 @@ +@echo off +setlocal + +set "AIR_VERSION=v1.61.7" + +cd /d "%~dp0admin" +if errorlevel 1 ( + echo [ERROR] Cannot enter the admin directory. + pause + exit /b 1 +) + +where go >nul 2>&1 +if errorlevel 1 ( + echo [ERROR] Go was not found. Install Go 1.23.0 and reopen the terminal. + pause + exit /b 1 +) + +for /f "usebackq delims=" %%G in (`go env GOPATH`) do set "ADMIN_GOPATH=%%G" +if not defined ADMIN_GOPATH ( + echo [ERROR] Cannot read GOPATH. + pause + exit /b 1 +) + +set "AIR_EXE=%ADMIN_GOPATH%\bin\air.exe" +set "NEED_INSTALL=0" +if not exist "%AIR_EXE%" set "NEED_INSTALL=1" + +if exist "%AIR_EXE%" ( + "%AIR_EXE%" -v 2>nul | findstr /c:"%AIR_VERSION%" >nul + if errorlevel 1 set "NEED_INSTALL=1" +) + +if "%NEED_INSTALL%"=="1" ( + echo [INFO] Installing Air %AIR_VERSION% for Go 1.23... + go install github.com/air-verse/air@%AIR_VERSION% + if errorlevel 1 ( + echo [ERROR] Air installation failed. Check the network and GOPROXY. + pause + exit /b 1 + ) +) + +echo [INFO] Starting Admin with hot reload. Press Ctrl+C to stop. +"%AIR_EXE%" -c .air.toml +set "ADMIN_EXIT_CODE=%ERRORLEVEL%" + +if not "%ADMIN_EXIT_CODE%"=="0" ( + echo [ERROR] Admin stopped with exit code %ADMIN_EXIT_CODE%. + pause +) + +endlocal & exit /b %ADMIN_EXIT_CODE%