fix: JWT密钥同步、地图初始化时序、API中间件错误处理

- 修复安装完成后JWT密钥未同步到viper内存的问题
- 修复地图在loading状态时DOM元素不存在导致的初始化错误
- 修复AppCryptoMiddleware中数据库错误时未正确返回错误响应的问题
- 添加数据库未初始化检查
- 移除调试日志输出
This commit is contained in:
2026-05-04 02:22:23 +08:00
parent a950acb2d6
commit 9262d066bb
3 changed files with 29 additions and 14 deletions
@@ -1,7 +1,6 @@
package app
import (
"fmt"
"verification-platform-backend/internal/database"
"verification-platform-backend/internal/model"
"verification-platform-backend/pkg/crypto"
@@ -11,25 +10,26 @@ import (
func AppCryptoMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Printf("[AppCrypto] Middleware called\n")
appKey := c.Param("appKey")
fmt.Printf("[AppCrypto] AppKey from param: %s\n", appKey)
if appKey == "" {
fmt.Printf("[AppCrypto] AppKey is empty, skipping\n")
c.Next()
return
}
if database.DB == nil {
c.JSON(500, gin.H{"code": 500, "message": "数据库未初始化"})
c.Abort()
return
}
var app model.Application
if err := database.DB.Where("app_key = ?", appKey).First(&app).Error; err != nil {
fmt.Printf("[AppCrypto] Database error: %v\n", err)
c.Next()
c.JSON(404, gin.H{"code": 404, "message": "应用不存在"})
c.Abort()
return
}
fmt.Printf("[AppCrypto] App found: %+v\n", app)
var encryptType crypto.EncryptType
switch app.EncryptType {
case "aes":
@@ -42,11 +42,9 @@ func AppCryptoMiddleware() gin.HandlerFunc {
shouldEncrypt := encryptType != crypto.EncryptTypeNone
fmt.Printf("[AppCrypto] AppKey: %s, EncryptType: %s, EncryptKey: %s, ShouldEncrypt: %v\n",
appKey, app.EncryptType, app.EncryptKey, shouldEncrypt)
c.Set("should_encrypt_response", shouldEncrypt)
c.Set("crypto_manager", crypto.NewCryptoManager(encryptType, app.EncryptKey))
c.Set("app", &app)
c.Next()
}