diff --git a/src/app.go b/src/app.go index eafc0070..05f0a4e0 100644 --- a/src/app.go +++ b/src/app.go @@ -1,6 +1,7 @@ package src import ( + "embed" "fmt" "ems.agt/src/framework/config" @@ -17,6 +18,9 @@ import ( "github.com/gin-gonic/gin" ) +//go:embed assets/* +var assetsDir embed.FS + // 路由函数句柄,交给由 http.ListenAndServe(addr, router) func AppEngine() *gin.Engine { app := initAppEngine() @@ -27,12 +31,11 @@ func AppEngine() *gin.Engine { // 初始模块路由 initModulesRoute(app) + // 设置程序内全局资源访问 + config.SetAssetsDirFS(assetsDir) + // 读取服务配置 app.ForwardedByClientIP = config.Get("server.proxy").(bool) - addr := fmt.Sprintf(":%d", config.Get("server.port").(int)) - - // 启动服务 - fmt.Printf("\nopen http://localhost%s \n\n", addr) return app } @@ -45,13 +48,7 @@ func AppEngine() *gin.Engine { // } // } func RunServer() error { - app := initAppEngine() - - // 初始全局默认 - initDefeat(app) - - // 初始模块路由 - initModulesRoute(app) + app := AppEngine() // 读取服务配置 app.ForwardedByClientIP = config.Get("server.proxy").(bool) diff --git a/src/assets/template/excel/user_import_template.xlsx b/src/assets/template/excel/user_import_template.xlsx new file mode 100644 index 00000000..eea2c6ec Binary files /dev/null and b/src/assets/template/excel/user_import_template.xlsx differ diff --git a/src/framework/config/config.go b/src/framework/config/config.go index dfe60ebc..ce196ee9 100644 --- a/src/framework/config/config.go +++ b/src/framework/config/config.go @@ -147,6 +147,16 @@ func Get(key string) any { return viper.Get(key) } +// GetAssetsDirFS 访问程序内全局资源访问 +func GetAssetsDirFS() embed.FS { + return viper.Get("AssetsDir").(embed.FS) +} + +// SetAssetsDirFS 设置程序内全局资源访问 +func SetAssetsDirFS(assetsDir embed.FS) { + viper.Set("AssetsDir", assetsDir) +} + // IsAdmin 用户是否为管理员 func IsAdmin(userID string) bool { if userID == "" { diff --git a/src/modules/system/controller/sys_user.go b/src/modules/system/controller/sys_user.go index 138d4cd8..470d1644 100644 --- a/src/modules/system/controller/sys_user.go +++ b/src/modules/system/controller/sys_user.go @@ -455,7 +455,23 @@ func (s *SysUserController) Export(c *gin.Context) { func (s *SysUserController) Template(c *gin.Context) { fileName := fmt.Sprintf("user_import_template_%d.xlsx", time.Now().UnixMilli()) asserPath := "assets/template/excel/user_import_template.xlsx" - c.FileAttachment(asserPath, fileName) + + // 从 embed.FS 中读取默认配置文件内容 + assetsDir := config.GetAssetsDirFS() + + // 读取内嵌文件 + fileData, err := assetsDir.ReadFile(asserPath) + if err != nil { + c.String(500, "Failed to read file") + return + } + + // 设置响应头 + c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName)) + c.Header("Content-Type", "application/octet-stream") + + // 返回响应体 + c.Data(200, "application/octet-stream", fileData) } // 用户信息列表导入