diff --git a/backend/config/config.go b/backend/config/config.go index 30134c9..f2e6d6f 100644 --- a/backend/config/config.go +++ b/backend/config/config.go @@ -18,6 +18,9 @@ type Config struct { // 其他配置 ServerPort string + + // SQLite配置(用于数据迁移) + SQLitePath string } func LoadConfig() (*Config, error) { @@ -47,6 +50,9 @@ func LoadConfig() (*Config, error) { // 其他配置 ServerPort: getEnv("PORT", "8080"), + + // SQLite路径(用于数据迁移) + SQLitePath: filepath.Join(dbDir, "aimodels.db"), } return config, nil diff --git a/backend/database/db.go b/backend/database/db.go index b30e1ea..4edcef3 100644 --- a/backend/database/db.go +++ b/backend/database/db.go @@ -6,6 +6,7 @@ import ( "log" _ "github.com/go-sql-driver/mysql" + _ "modernc.org/sqlite" "aimodels-prices/config" "aimodels-prices/models" diff --git a/backend/handlers/model_type.go b/backend/handlers/model_type.go index 511cdef..eed1008 100644 --- a/backend/handlers/model_type.go +++ b/backend/handlers/model_type.go @@ -4,29 +4,25 @@ import ( "database/sql" "github.com/gin-gonic/gin" -) -// ModelType 模型类型结构 -type ModelType struct { - Key string `json:"key"` - Label string `json:"label"` -} + "aimodels-prices/models" +) // GetModelTypes 获取所有模型类型 func GetModelTypes(c *gin.Context) { db := c.MustGet("db").(*sql.DB) - rows, err := db.Query("SELECT key, label FROM model_type") + rows, err := db.Query("SELECT type_key, type_label FROM model_type") if err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } defer rows.Close() - var types []ModelType + var types []models.ModelType for rows.Next() { - var t ModelType - if err := rows.Scan(&t.Key, &t.Label); err != nil { + var t models.ModelType + if err := rows.Scan(&t.TypeKey, &t.TypeLabel); err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } @@ -40,17 +36,17 @@ func GetModelTypes(c *gin.Context) { func CreateModelType(c *gin.Context) { db := c.MustGet("db").(*sql.DB) - var newType ModelType + var newType models.ModelType if err := c.ShouldBindJSON(&newType); err != nil { c.JSON(400, gin.H{"error": err.Error()}) return } _, err := db.Exec(` - INSERT INTO model_type (key, label) + INSERT INTO model_type (type_key, type_label) VALUES (?, ?) - ON CONFLICT(key) DO UPDATE SET label = excluded.label - `, newType.Key, newType.Label) + ON DUPLICATE KEY UPDATE type_label = VALUES(type_label) + `, newType.TypeKey, newType.TypeLabel) if err != nil { c.JSON(500, gin.H{"error": err.Error()}) diff --git a/backend/scripts/init.sql b/backend/scripts/init.sql deleted file mode 100644 index a3e9855..0000000 --- a/backend/scripts/init.sql +++ /dev/null @@ -1,10 +0,0 @@ --- 初始化模型类型数据 -INSERT INTO model_type (type_key, type_label) VALUES -('text2text', '文生文'), -('text2image', '文生图'), -('text2speech', '文生音'), -('speech2text', '音生文'), -('image2text', '图生文'), -('embedding', '向量'), -('other', '其他') -ON DUPLICATE KEY UPDATE type_label = VALUES(type_label); \ No newline at end of file diff --git a/frontend/src/views/Prices.vue b/frontend/src/views/Prices.vue index a9af2e6..7014c1e 100644 --- a/frontend/src/views/Prices.vue +++ b/frontend/src/views/Prices.vue @@ -777,7 +777,7 @@ const loadModelTypes = async () => { const types = response.data const map = {} types.forEach(type => { - map[type.key] = type.label + map[type.type_key] = type.type_label }) modelTypeMap.value = map } catch (error) { @@ -795,18 +795,18 @@ const handleModelTypeCreate = async (value) => { } // 如果输入的是英文key,直接使用 - let key = value - let label = value + let type_key = value + let type_label = value if (!/^[a-zA-Z0-9_]+$/.test(value)) { // 如果是中文描述,生成一个新的key - key = `type_${Date.now()}` - label = value + type_key = `type_${Date.now()}` + type_label = value } try { - await axios.post('/api/model-types', { key, label }) - modelTypeMap.value[key] = label - return key + await axios.post('/api/model-types', { type_key, type_label }) + modelTypeMap.value[type_key] = type_label + return type_key } catch (error) { console.error('Failed to create model type:', error) ElMessage.error('创建模型类型失败') diff --git a/scripts/start.sh b/scripts/start.sh index 0a9e0bc..f0db2a6 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -1,5 +1,9 @@ #!/bin/bash +# 执行数据库迁移 +echo "执行数据库迁移..." +./migrate + # 启动后端服务 echo "启动后端服务..." ./main &