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()
}
@@ -16,6 +16,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/glebarez/sqlite"
"github.com/spf13/viper"
"golang.org/x/crypto/bcrypt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
@@ -232,6 +233,16 @@ func handleSetup(c *gin.Context) {
log.Printf("Warning: failed to save config file: %v", err)
}
viper.Set("app.jwt_secret", jwtSecret)
viper.Set("database.type", req.DbType)
if req.DbType == "mysql" {
viper.Set("database.host", req.DbHost)
viper.Set("database.port", req.DbPort)
viper.Set("database.name", req.DbName)
viper.Set("database.username", req.DbUsername)
viper.Set("database.password", req.DbPassword)
}
database.DB = newDB
response.Success(c, gin.H{
+9 -3
View File
@@ -703,10 +703,16 @@ function initActivityChart() {
onMounted(async () => {
fetchCurrentUser()
await fetchDashboard()
await nextTick()
})
initMapChart()
initActivityChart()
watch(loading, async (newVal) => {
if (!newVal) {
await nextTick()
setTimeout(() => {
initMapChart()
initActivityChart()
}, 100)
}
})
watch(isDark, () => {