fix: JWT密钥同步、地图初始化时序、API中间件错误处理
- 修复安装完成后JWT密钥未同步到viper内存的问题 - 修复地图在loading状态时DOM元素不存在导致的初始化错误 - 修复AppCryptoMiddleware中数据库错误时未正确返回错误响应的问题 - 添加数据库未初始化检查 - 移除调试日志输出
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"verification-platform-backend/internal/database"
|
"verification-platform-backend/internal/database"
|
||||||
"verification-platform-backend/internal/model"
|
"verification-platform-backend/internal/model"
|
||||||
"verification-platform-backend/pkg/crypto"
|
"verification-platform-backend/pkg/crypto"
|
||||||
@@ -11,25 +10,26 @@ import (
|
|||||||
|
|
||||||
func AppCryptoMiddleware() gin.HandlerFunc {
|
func AppCryptoMiddleware() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
fmt.Printf("[AppCrypto] Middleware called\n")
|
|
||||||
appKey := c.Param("appKey")
|
appKey := c.Param("appKey")
|
||||||
fmt.Printf("[AppCrypto] AppKey from param: %s\n", appKey)
|
|
||||||
|
|
||||||
if appKey == "" {
|
if appKey == "" {
|
||||||
fmt.Printf("[AppCrypto] AppKey is empty, skipping\n")
|
|
||||||
c.Next()
|
c.Next()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if database.DB == nil {
|
||||||
|
c.JSON(500, gin.H{"code": 500, "message": "数据库未初始化"})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var app model.Application
|
var app model.Application
|
||||||
if err := database.DB.Where("app_key = ?", appKey).First(&app).Error; err != nil {
|
if err := database.DB.Where("app_key = ?", appKey).First(&app).Error; err != nil {
|
||||||
fmt.Printf("[AppCrypto] Database error: %v\n", err)
|
c.JSON(404, gin.H{"code": 404, "message": "应用不存在"})
|
||||||
c.Next()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("[AppCrypto] App found: %+v\n", app)
|
|
||||||
|
|
||||||
var encryptType crypto.EncryptType
|
var encryptType crypto.EncryptType
|
||||||
switch app.EncryptType {
|
switch app.EncryptType {
|
||||||
case "aes":
|
case "aes":
|
||||||
@@ -42,11 +42,9 @@ func AppCryptoMiddleware() gin.HandlerFunc {
|
|||||||
|
|
||||||
shouldEncrypt := encryptType != crypto.EncryptTypeNone
|
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("should_encrypt_response", shouldEncrypt)
|
||||||
c.Set("crypto_manager", crypto.NewCryptoManager(encryptType, app.EncryptKey))
|
c.Set("crypto_manager", crypto.NewCryptoManager(encryptType, app.EncryptKey))
|
||||||
|
c.Set("app", &app)
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/glebarez/sqlite"
|
"github.com/glebarez/sqlite"
|
||||||
|
"github.com/spf13/viper"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -232,6 +233,16 @@ func handleSetup(c *gin.Context) {
|
|||||||
log.Printf("Warning: failed to save config file: %v", err)
|
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
|
database.DB = newDB
|
||||||
|
|
||||||
response.Success(c, gin.H{
|
response.Success(c, gin.H{
|
||||||
|
|||||||
@@ -703,10 +703,16 @@ function initActivityChart() {
|
|||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
fetchCurrentUser()
|
fetchCurrentUser()
|
||||||
await fetchDashboard()
|
await fetchDashboard()
|
||||||
await nextTick()
|
})
|
||||||
|
|
||||||
initMapChart()
|
watch(loading, async (newVal) => {
|
||||||
initActivityChart()
|
if (!newVal) {
|
||||||
|
await nextTick()
|
||||||
|
setTimeout(() => {
|
||||||
|
initMapChart()
|
||||||
|
initActivityChart()
|
||||||
|
}, 100)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
watch(isDark, () => {
|
watch(isDark, () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user