console_migrate.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // 用于迁移检测的旧键,该文件下个版本会删除
  2. package controller
  3. import (
  4. "encoding/json"
  5. "net/http"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/model"
  8. "github.com/gin-gonic/gin"
  9. )
  10. // MigrateConsoleSetting 迁移旧的控制台相关配置到 console_setting.*
  11. func MigrateConsoleSetting(c *gin.Context) {
  12. // 读取全部 option
  13. opts, err := model.AllOption()
  14. if err != nil {
  15. common.SysError("failed to get all options: " + err.Error())
  16. c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": "获取配置失败,请稍后重试"})
  17. return
  18. }
  19. // 建立 map
  20. valMap := map[string]string{}
  21. for _, o := range opts {
  22. valMap[o.Key] = o.Value
  23. }
  24. // 处理 APIInfo
  25. if v := valMap["ApiInfo"]; v != "" {
  26. var arr []map[string]interface{}
  27. if err := json.Unmarshal([]byte(v), &arr); err == nil {
  28. if len(arr) > 50 {
  29. arr = arr[:50]
  30. }
  31. bytes, _ := json.Marshal(arr)
  32. model.UpdateOption("console_setting.api_info", string(bytes))
  33. }
  34. model.UpdateOption("ApiInfo", "")
  35. }
  36. // Announcements 直接搬
  37. if v := valMap["Announcements"]; v != "" {
  38. model.UpdateOption("console_setting.announcements", v)
  39. model.UpdateOption("Announcements", "")
  40. }
  41. // FAQ 转换
  42. if v := valMap["FAQ"]; v != "" {
  43. var arr []map[string]interface{}
  44. if err := json.Unmarshal([]byte(v), &arr); err == nil {
  45. out := []map[string]interface{}{}
  46. for _, item := range arr {
  47. q, _ := item["question"].(string)
  48. if q == "" {
  49. q, _ = item["title"].(string)
  50. }
  51. a, _ := item["answer"].(string)
  52. if a == "" {
  53. a, _ = item["content"].(string)
  54. }
  55. if q != "" && a != "" {
  56. out = append(out, map[string]interface{}{"question": q, "answer": a})
  57. }
  58. }
  59. if len(out) > 50 {
  60. out = out[:50]
  61. }
  62. bytes, _ := json.Marshal(out)
  63. model.UpdateOption("console_setting.faq", string(bytes))
  64. }
  65. model.UpdateOption("FAQ", "")
  66. }
  67. // Uptime Kuma 迁移到新的 groups 结构(console_setting.uptime_kuma_groups)
  68. url := valMap["UptimeKumaUrl"]
  69. slug := valMap["UptimeKumaSlug"]
  70. if url != "" && slug != "" {
  71. // 仅当同时存在 URL 与 Slug 时才进行迁移
  72. groups := []map[string]interface{}{
  73. {
  74. "id": 1,
  75. "categoryName": "old",
  76. "url": url,
  77. "slug": slug,
  78. "description": "",
  79. },
  80. }
  81. bytes, _ := json.Marshal(groups)
  82. model.UpdateOption("console_setting.uptime_kuma_groups", string(bytes))
  83. }
  84. // 清空旧键内容
  85. if url != "" {
  86. model.UpdateOption("UptimeKumaUrl", "")
  87. }
  88. if slug != "" {
  89. model.UpdateOption("UptimeKumaSlug", "")
  90. }
  91. // 删除旧键记录
  92. oldKeys := []string{"ApiInfo", "Announcements", "FAQ", "UptimeKumaUrl", "UptimeKumaSlug"}
  93. model.DB.Where("key IN ?", oldKeys).Delete(&model.Option{})
  94. // 重新加载 OptionMap
  95. model.InitOptionMap()
  96. common.SysLog("console setting migrated")
  97. c.JSON(http.StatusOK, gin.H{"success": true, "message": "migrated"})
  98. }