25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

550 lines
23 KiB

  1. package model
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/setting"
  8. "github.com/QuantumNous/new-api/setting/config"
  9. "github.com/QuantumNous/new-api/setting/operation_setting"
  10. "github.com/QuantumNous/new-api/setting/performance_setting"
  11. "github.com/QuantumNous/new-api/setting/ratio_setting"
  12. "github.com/QuantumNous/new-api/setting/system_setting"
  13. )
  14. type Option struct {
  15. Key string `json:"key" gorm:"primaryKey"`
  16. Value string `json:"value"`
  17. }
  18. func AllOption() ([]*Option, error) {
  19. var options []*Option
  20. var err error
  21. err = DB.Find(&options).Error
  22. return options, err
  23. }
  24. func InitOptionMap() {
  25. common.OptionMapRWMutex.Lock()
  26. common.OptionMap = make(map[string]string)
  27. // 添加原有的系统配置
  28. common.OptionMap["FileUploadPermission"] = strconv.Itoa(common.FileUploadPermission)
  29. common.OptionMap["FileDownloadPermission"] = strconv.Itoa(common.FileDownloadPermission)
  30. common.OptionMap["ImageUploadPermission"] = strconv.Itoa(common.ImageUploadPermission)
  31. common.OptionMap["ImageDownloadPermission"] = strconv.Itoa(common.ImageDownloadPermission)
  32. common.OptionMap["PasswordLoginEnabled"] = strconv.FormatBool(common.PasswordLoginEnabled)
  33. common.OptionMap["PasswordRegisterEnabled"] = strconv.FormatBool(common.PasswordRegisterEnabled)
  34. common.OptionMap["EmailVerificationEnabled"] = strconv.FormatBool(common.EmailVerificationEnabled)
  35. common.OptionMap["GitHubOAuthEnabled"] = strconv.FormatBool(common.GitHubOAuthEnabled)
  36. common.OptionMap["LinuxDOOAuthEnabled"] = strconv.FormatBool(common.LinuxDOOAuthEnabled)
  37. common.OptionMap["TelegramOAuthEnabled"] = strconv.FormatBool(common.TelegramOAuthEnabled)
  38. common.OptionMap["WeChatAuthEnabled"] = strconv.FormatBool(common.WeChatAuthEnabled)
  39. common.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(common.TurnstileCheckEnabled)
  40. common.OptionMap["CaptchaEnabled"] = strconv.FormatBool(common.CaptchaEnabled)
  41. common.OptionMap["RegisterEnabled"] = strconv.FormatBool(common.RegisterEnabled)
  42. common.OptionMap["AutomaticDisableChannelEnabled"] = strconv.FormatBool(common.AutomaticDisableChannelEnabled)
  43. common.OptionMap["AutomaticEnableChannelEnabled"] = strconv.FormatBool(common.AutomaticEnableChannelEnabled)
  44. common.OptionMap["LogConsumeEnabled"] = strconv.FormatBool(common.LogConsumeEnabled)
  45. common.OptionMap["DisplayInCurrencyEnabled"] = strconv.FormatBool(common.DisplayInCurrencyEnabled)
  46. common.OptionMap["DisplayTokenStatEnabled"] = strconv.FormatBool(common.DisplayTokenStatEnabled)
  47. common.OptionMap["DrawingEnabled"] = strconv.FormatBool(common.DrawingEnabled)
  48. common.OptionMap["TaskEnabled"] = strconv.FormatBool(common.TaskEnabled)
  49. common.OptionMap["DataExportEnabled"] = strconv.FormatBool(common.DataExportEnabled)
  50. common.OptionMap["ChannelDisableThreshold"] = strconv.FormatFloat(common.ChannelDisableThreshold, 'f', -1, 64)
  51. common.OptionMap["EmailDomainRestrictionEnabled"] = strconv.FormatBool(common.EmailDomainRestrictionEnabled)
  52. common.OptionMap["EmailAliasRestrictionEnabled"] = strconv.FormatBool(common.EmailAliasRestrictionEnabled)
  53. common.OptionMap["EmailDomainWhitelist"] = strings.Join(common.EmailDomainWhitelist, ",")
  54. common.OptionMap["SMTPServer"] = ""
  55. common.OptionMap["SMTPFrom"] = ""
  56. common.OptionMap["SMTPPort"] = strconv.Itoa(common.SMTPPort)
  57. common.OptionMap["SMTPAccount"] = ""
  58. common.OptionMap["SMTPToken"] = ""
  59. common.OptionMap["SMTPSSLEnabled"] = strconv.FormatBool(common.SMTPSSLEnabled)
  60. common.OptionMap["Notice"] = ""
  61. common.OptionMap["About"] = ""
  62. common.OptionMap["HomePageContent"] = ""
  63. common.OptionMap["Footer"] = common.Footer
  64. common.OptionMap["SystemName"] = common.SystemName
  65. common.OptionMap["Logo"] = common.Logo
  66. common.OptionMap["ServerAddress"] = ""
  67. common.OptionMap["WorkerUrl"] = system_setting.WorkerUrl
  68. common.OptionMap["WorkerValidKey"] = system_setting.WorkerValidKey
  69. common.OptionMap["WorkerAllowHttpImageRequestEnabled"] = strconv.FormatBool(system_setting.WorkerAllowHttpImageRequestEnabled)
  70. common.OptionMap["PayAddress"] = ""
  71. common.OptionMap["CustomCallbackAddress"] = ""
  72. common.OptionMap["EpayId"] = ""
  73. common.OptionMap["EpayKey"] = ""
  74. common.OptionMap["Price"] = strconv.FormatFloat(operation_setting.Price, 'f', -1, 64)
  75. common.OptionMap["USDExchangeRate"] = strconv.FormatFloat(operation_setting.USDExchangeRate, 'f', -1, 64)
  76. common.OptionMap["MinTopUp"] = strconv.Itoa(operation_setting.MinTopUp)
  77. common.OptionMap["StripeMinTopUp"] = strconv.Itoa(setting.StripeMinTopUp)
  78. common.OptionMap["StripeApiSecret"] = setting.StripeApiSecret
  79. common.OptionMap["StripeWebhookSecret"] = setting.StripeWebhookSecret
  80. common.OptionMap["StripePriceId"] = setting.StripePriceId
  81. common.OptionMap["StripeUnitPrice"] = strconv.FormatFloat(setting.StripeUnitPrice, 'f', -1, 64)
  82. common.OptionMap["StripePromotionCodesEnabled"] = strconv.FormatBool(setting.StripePromotionCodesEnabled)
  83. common.OptionMap["CreemApiKey"] = setting.CreemApiKey
  84. common.OptionMap["CreemProducts"] = setting.CreemProducts
  85. common.OptionMap["CreemTestMode"] = strconv.FormatBool(setting.CreemTestMode)
  86. common.OptionMap["CreemWebhookSecret"] = setting.CreemWebhookSecret
  87. common.OptionMap["WechatPayAppID"] = setting.WechatPayAppID
  88. common.OptionMap["WechatPayMchID"] = setting.WechatPayMchID
  89. common.OptionMap["WechatPayAPIv3Key"] = setting.WechatPayAPIv3Key
  90. common.OptionMap["WechatPaySerialNo"] = setting.WechatPaySerialNo
  91. common.OptionMap["WechatPayPubKeyID"] = setting.WechatPayPubKeyID
  92. common.OptionMap["WechatPayNotifyURL"] = setting.WechatPayNotifyURL
  93. common.OptionMap["WechatPayKeyPath"] = setting.WechatPayKeyPath
  94. common.OptionMap["WechatPayPubKeyPath"] = setting.WechatPayPubKeyPath
  95. common.OptionMap["WechatPayKeyB64"] = setting.WechatPayKeyB64
  96. common.OptionMap["WechatPayPubKeyB64"] = setting.WechatPayPubKeyB64
  97. common.OptionMap["WechatPayMinTopUp"] = strconv.Itoa(setting.WechatPayMinTopUp)
  98. common.OptionMap["WechatPayUnitPrice"] = strconv.FormatFloat(setting.WechatPayUnitPrice, 'f', -1, 64)
  99. common.OptionMap["TopupGroupRatio"] = common.TopupGroupRatio2JSONString()
  100. common.OptionMap["Chats"] = setting.Chats2JsonString()
  101. common.OptionMap["AutoGroups"] = setting.AutoGroups2JsonString()
  102. common.OptionMap["DefaultUseAutoGroup"] = strconv.FormatBool(setting.DefaultUseAutoGroup)
  103. common.OptionMap["PayMethods"] = operation_setting.PayMethods2JsonString()
  104. common.OptionMap["GitHubClientId"] = ""
  105. common.OptionMap["GitHubClientSecret"] = ""
  106. common.OptionMap["TelegramBotToken"] = ""
  107. common.OptionMap["TelegramBotName"] = ""
  108. common.OptionMap["WeChatServerAddress"] = ""
  109. common.OptionMap["WeChatServerToken"] = ""
  110. common.OptionMap["WeChatAccountQRCodeImageURL"] = ""
  111. common.OptionMap["TurnstileSiteKey"] = ""
  112. common.OptionMap["TurnstileSecretKey"] = ""
  113. common.OptionMap["QuotaForNewUser"] = strconv.Itoa(common.QuotaForNewUser)
  114. common.OptionMap["QuotaForInviter"] = strconv.Itoa(common.QuotaForInviter)
  115. common.OptionMap["QuotaForInvitee"] = strconv.Itoa(common.QuotaForInvitee)
  116. common.OptionMap["QuotaRemindThreshold"] = strconv.Itoa(common.QuotaRemindThreshold)
  117. common.OptionMap["PreConsumedQuota"] = strconv.Itoa(common.PreConsumedQuota)
  118. common.OptionMap["ModelRequestRateLimitCount"] = strconv.Itoa(setting.ModelRequestRateLimitCount)
  119. common.OptionMap["ModelRequestRateLimitDurationMinutes"] = strconv.Itoa(setting.ModelRequestRateLimitDurationMinutes)
  120. common.OptionMap["ModelRequestRateLimitSuccessCount"] = strconv.Itoa(setting.ModelRequestRateLimitSuccessCount)
  121. common.OptionMap["ModelRequestRateLimitGroup"] = setting.ModelRequestRateLimitGroup2JSONString()
  122. common.OptionMap["ModelRatio"] = ratio_setting.ModelRatio2JSONString()
  123. common.OptionMap["ModelPrice"] = ratio_setting.ModelPrice2JSONString()
  124. common.OptionMap["CacheRatio"] = ratio_setting.CacheRatio2JSONString()
  125. common.OptionMap["CreateCacheRatio"] = ratio_setting.CreateCacheRatio2JSONString()
  126. common.OptionMap["GroupRatio"] = ratio_setting.GroupRatio2JSONString()
  127. common.OptionMap["GroupGroupRatio"] = ratio_setting.GroupGroupRatio2JSONString()
  128. common.OptionMap["UserUsableGroups"] = setting.UserUsableGroups2JSONString()
  129. common.OptionMap["CompletionRatio"] = ratio_setting.CompletionRatio2JSONString()
  130. common.OptionMap["ImageRatio"] = ratio_setting.ImageRatio2JSONString()
  131. common.OptionMap["AudioRatio"] = ratio_setting.AudioRatio2JSONString()
  132. common.OptionMap["AudioCompletionRatio"] = ratio_setting.AudioCompletionRatio2JSONString()
  133. common.OptionMap["TopUpLink"] = common.TopUpLink
  134. //common.OptionMap["ChatLink"] = common.ChatLink
  135. //common.OptionMap["ChatLink2"] = common.ChatLink2
  136. common.OptionMap["QuotaPerUnit"] = strconv.FormatFloat(common.QuotaPerUnit, 'f', -1, 64)
  137. common.OptionMap["RetryTimes"] = strconv.Itoa(common.RetryTimes)
  138. common.OptionMap["DataExportInterval"] = strconv.Itoa(common.DataExportInterval)
  139. common.OptionMap["DataExportDefaultTime"] = common.DataExportDefaultTime
  140. common.OptionMap["DefaultCollapseSidebar"] = strconv.FormatBool(common.DefaultCollapseSidebar)
  141. common.OptionMap["MjNotifyEnabled"] = strconv.FormatBool(setting.MjNotifyEnabled)
  142. common.OptionMap["MjAccountFilterEnabled"] = strconv.FormatBool(setting.MjAccountFilterEnabled)
  143. common.OptionMap["MjModeClearEnabled"] = strconv.FormatBool(setting.MjModeClearEnabled)
  144. common.OptionMap["MjForwardUrlEnabled"] = strconv.FormatBool(setting.MjForwardUrlEnabled)
  145. common.OptionMap["MjActionCheckSuccessEnabled"] = strconv.FormatBool(setting.MjActionCheckSuccessEnabled)
  146. common.OptionMap["CheckSensitiveEnabled"] = strconv.FormatBool(setting.CheckSensitiveEnabled)
  147. common.OptionMap["DemoSiteEnabled"] = strconv.FormatBool(operation_setting.DemoSiteEnabled)
  148. common.OptionMap["SelfUseModeEnabled"] = strconv.FormatBool(operation_setting.SelfUseModeEnabled)
  149. common.OptionMap["ModelRequestRateLimitEnabled"] = strconv.FormatBool(setting.ModelRequestRateLimitEnabled)
  150. common.OptionMap["CheckSensitiveOnPromptEnabled"] = strconv.FormatBool(setting.CheckSensitiveOnPromptEnabled)
  151. common.OptionMap["StopOnSensitiveEnabled"] = strconv.FormatBool(setting.StopOnSensitiveEnabled)
  152. common.OptionMap["SensitiveWords"] = setting.SensitiveWordsToString()
  153. common.OptionMap["StreamCacheQueueLength"] = strconv.Itoa(setting.StreamCacheQueueLength)
  154. common.OptionMap["AutomaticDisableKeywords"] = operation_setting.AutomaticDisableKeywordsToString()
  155. common.OptionMap["AutomaticDisableStatusCodes"] = operation_setting.AutomaticDisableStatusCodesToString()
  156. common.OptionMap["AutomaticRetryStatusCodes"] = operation_setting.AutomaticRetryStatusCodesToString()
  157. common.OptionMap["ExposeRatioEnabled"] = strconv.FormatBool(ratio_setting.IsExposeRatioEnabled())
  158. // 自动添加所有注册的模型配置
  159. modelConfigs := config.GlobalConfig.ExportAllConfigs()
  160. for k, v := range modelConfigs {
  161. common.OptionMap[k] = v
  162. }
  163. common.OptionMapRWMutex.Unlock()
  164. loadOptionsFromDatabase()
  165. }
  166. // triggerWechatPayReset 安全触发微信支付客户端重置
  167. func triggerWechatPayReset() {
  168. if setting.OnWechatPayConfigChanged != nil {
  169. setting.OnWechatPayConfigChanged()
  170. }
  171. }
  172. func loadOptionsFromDatabase() {
  173. options, _ := AllOption()
  174. for _, option := range options {
  175. err := updateOptionMap(option.Key, option.Value)
  176. if err != nil {
  177. common.SysLog("failed to update option map: " + err.Error())
  178. }
  179. }
  180. }
  181. func SyncOptions(frequency int) {
  182. for {
  183. time.Sleep(time.Duration(frequency) * time.Second)
  184. common.SysLog("syncing options from database")
  185. loadOptionsFromDatabase()
  186. }
  187. }
  188. func UpdateOption(key string, value string) error {
  189. // Save to database first
  190. option := Option{
  191. Key: key,
  192. }
  193. // https://gorm.io/docs/update.html#Save-All-Fields
  194. DB.FirstOrCreate(&option, Option{Key: key})
  195. option.Value = value
  196. // Save is a combination function.
  197. // If save value does not contain primary key, it will execute Create,
  198. // otherwise it will execute Update (with all fields).
  199. DB.Save(&option)
  200. // Update OptionMap
  201. return updateOptionMap(key, value)
  202. }
  203. func updateOptionMap(key string, value string) (err error) {
  204. common.OptionMapRWMutex.Lock()
  205. defer common.OptionMapRWMutex.Unlock()
  206. common.OptionMap[key] = value
  207. // 检查是否是模型配置 - 使用更规范的方式处理
  208. if handleConfigUpdate(key, value) {
  209. return nil // 已由配置系统处理
  210. }
  211. // 处理传统配置项...
  212. if strings.HasSuffix(key, "Permission") {
  213. intValue, _ := strconv.Atoi(value)
  214. switch key {
  215. case "FileUploadPermission":
  216. common.FileUploadPermission = intValue
  217. case "FileDownloadPermission":
  218. common.FileDownloadPermission = intValue
  219. case "ImageUploadPermission":
  220. common.ImageUploadPermission = intValue
  221. case "ImageDownloadPermission":
  222. common.ImageDownloadPermission = intValue
  223. }
  224. }
  225. if strings.HasSuffix(key, "Enabled") || key == "DefaultCollapseSidebar" || key == "DefaultUseAutoGroup" {
  226. boolValue := value == "true"
  227. switch key {
  228. case "PasswordRegisterEnabled":
  229. common.PasswordRegisterEnabled = boolValue
  230. case "PasswordLoginEnabled":
  231. common.PasswordLoginEnabled = boolValue
  232. case "EmailVerificationEnabled":
  233. common.EmailVerificationEnabled = boolValue
  234. case "GitHubOAuthEnabled":
  235. common.GitHubOAuthEnabled = boolValue
  236. case "LinuxDOOAuthEnabled":
  237. common.LinuxDOOAuthEnabled = boolValue
  238. case "WeChatAuthEnabled":
  239. common.WeChatAuthEnabled = boolValue
  240. case "TelegramOAuthEnabled":
  241. common.TelegramOAuthEnabled = boolValue
  242. case "TurnstileCheckEnabled":
  243. common.TurnstileCheckEnabled = boolValue
  244. case "CaptchaEnabled":
  245. common.CaptchaEnabled = boolValue
  246. case "RegisterEnabled":
  247. common.RegisterEnabled = boolValue
  248. case "EmailDomainRestrictionEnabled":
  249. common.EmailDomainRestrictionEnabled = boolValue
  250. case "EmailAliasRestrictionEnabled":
  251. common.EmailAliasRestrictionEnabled = boolValue
  252. case "AutomaticDisableChannelEnabled":
  253. common.AutomaticDisableChannelEnabled = boolValue
  254. case "AutomaticEnableChannelEnabled":
  255. common.AutomaticEnableChannelEnabled = boolValue
  256. case "LogConsumeEnabled":
  257. common.LogConsumeEnabled = boolValue
  258. case "DisplayInCurrencyEnabled":
  259. // 兼容旧字段:同步到新配置 general_setting.quota_display_type(运行时生效)
  260. // true -> USD, false -> TOKENS
  261. newVal := "USD"
  262. if !boolValue {
  263. newVal = "TOKENS"
  264. }
  265. if cfg := config.GlobalConfig.Get("general_setting"); cfg != nil {
  266. _ = config.UpdateConfigFromMap(cfg, map[string]string{"quota_display_type": newVal})
  267. }
  268. case "DisplayTokenStatEnabled":
  269. common.DisplayTokenStatEnabled = boolValue
  270. case "DrawingEnabled":
  271. common.DrawingEnabled = boolValue
  272. case "TaskEnabled":
  273. common.TaskEnabled = boolValue
  274. case "DataExportEnabled":
  275. common.DataExportEnabled = boolValue
  276. case "DefaultCollapseSidebar":
  277. common.DefaultCollapseSidebar = boolValue
  278. case "MjNotifyEnabled":
  279. setting.MjNotifyEnabled = boolValue
  280. case "MjAccountFilterEnabled":
  281. setting.MjAccountFilterEnabled = boolValue
  282. case "MjModeClearEnabled":
  283. setting.MjModeClearEnabled = boolValue
  284. case "MjForwardUrlEnabled":
  285. setting.MjForwardUrlEnabled = boolValue
  286. case "MjActionCheckSuccessEnabled":
  287. setting.MjActionCheckSuccessEnabled = boolValue
  288. case "CheckSensitiveEnabled":
  289. setting.CheckSensitiveEnabled = boolValue
  290. case "DemoSiteEnabled":
  291. operation_setting.DemoSiteEnabled = boolValue
  292. case "SelfUseModeEnabled":
  293. operation_setting.SelfUseModeEnabled = boolValue
  294. case "CheckSensitiveOnPromptEnabled":
  295. setting.CheckSensitiveOnPromptEnabled = boolValue
  296. case "ModelRequestRateLimitEnabled":
  297. setting.ModelRequestRateLimitEnabled = boolValue
  298. case "StopOnSensitiveEnabled":
  299. setting.StopOnSensitiveEnabled = boolValue
  300. case "SMTPSSLEnabled":
  301. common.SMTPSSLEnabled = boolValue
  302. case "WorkerAllowHttpImageRequestEnabled":
  303. system_setting.WorkerAllowHttpImageRequestEnabled = boolValue
  304. case "DefaultUseAutoGroup":
  305. setting.DefaultUseAutoGroup = boolValue
  306. case "ExposeRatioEnabled":
  307. ratio_setting.SetExposeRatioEnabled(boolValue)
  308. }
  309. }
  310. switch key {
  311. case "EmailDomainWhitelist":
  312. common.EmailDomainWhitelist = strings.Split(value, ",")
  313. case "SMTPServer":
  314. common.SMTPServer = value
  315. case "SMTPPort":
  316. intValue, _ := strconv.Atoi(value)
  317. common.SMTPPort = intValue
  318. case "SMTPAccount":
  319. common.SMTPAccount = value
  320. case "SMTPFrom":
  321. common.SMTPFrom = value
  322. case "SMTPToken":
  323. common.SMTPToken = value
  324. case "ServerAddress":
  325. system_setting.ServerAddress = value
  326. case "WorkerUrl":
  327. system_setting.WorkerUrl = value
  328. case "WorkerValidKey":
  329. system_setting.WorkerValidKey = value
  330. case "PayAddress":
  331. operation_setting.PayAddress = value
  332. case "Chats":
  333. err = setting.UpdateChatsByJsonString(value)
  334. case "AutoGroups":
  335. err = setting.UpdateAutoGroupsByJsonString(value)
  336. case "CustomCallbackAddress":
  337. operation_setting.CustomCallbackAddress = value
  338. case "EpayId":
  339. operation_setting.EpayId = value
  340. case "EpayKey":
  341. operation_setting.EpayKey = value
  342. case "Price":
  343. operation_setting.Price, _ = strconv.ParseFloat(value, 64)
  344. case "USDExchangeRate":
  345. operation_setting.USDExchangeRate, _ = strconv.ParseFloat(value, 64)
  346. case "MinTopUp":
  347. operation_setting.MinTopUp, _ = strconv.Atoi(value)
  348. case "StripeApiSecret":
  349. setting.StripeApiSecret = value
  350. case "StripeWebhookSecret":
  351. setting.StripeWebhookSecret = value
  352. case "StripePriceId":
  353. setting.StripePriceId = value
  354. case "StripeUnitPrice":
  355. setting.StripeUnitPrice, _ = strconv.ParseFloat(value, 64)
  356. case "StripeMinTopUp":
  357. setting.StripeMinTopUp, _ = strconv.Atoi(value)
  358. case "StripePromotionCodesEnabled":
  359. setting.StripePromotionCodesEnabled = value == "true"
  360. case "CreemApiKey":
  361. setting.CreemApiKey = value
  362. case "CreemProducts":
  363. setting.CreemProducts = value
  364. case "CreemTestMode":
  365. setting.CreemTestMode = value == "true"
  366. case "CreemWebhookSecret":
  367. setting.CreemWebhookSecret = value
  368. case "WechatPayAppID":
  369. setting.WechatPayAppID = value
  370. triggerWechatPayReset()
  371. case "WechatPayMchID":
  372. setting.WechatPayMchID = value
  373. triggerWechatPayReset()
  374. case "WechatPayAPIv3Key":
  375. setting.WechatPayAPIv3Key = value
  376. triggerWechatPayReset()
  377. case "WechatPaySerialNo":
  378. setting.WechatPaySerialNo = value
  379. triggerWechatPayReset()
  380. case "WechatPayPubKeyID":
  381. setting.WechatPayPubKeyID = value
  382. triggerWechatPayReset()
  383. case "WechatPayNotifyURL":
  384. setting.WechatPayNotifyURL = value
  385. case "WechatPayKeyPath":
  386. setting.WechatPayKeyPath = value
  387. triggerWechatPayReset()
  388. case "WechatPayPubKeyPath":
  389. setting.WechatPayPubKeyPath = value
  390. triggerWechatPayReset()
  391. case "WechatPayKeyB64":
  392. setting.WechatPayKeyB64 = value
  393. triggerWechatPayReset()
  394. case "WechatPayPubKeyB64":
  395. setting.WechatPayPubKeyB64 = value
  396. triggerWechatPayReset()
  397. case "WechatPayMinTopUp":
  398. setting.WechatPayMinTopUp, _ = strconv.Atoi(value)
  399. case "WechatPayUnitPrice":
  400. setting.WechatPayUnitPrice, _ = strconv.ParseFloat(value, 64)
  401. case "TopupGroupRatio":
  402. err = common.UpdateTopupGroupRatioByJSONString(value)
  403. case "GitHubClientId":
  404. common.GitHubClientId = value
  405. case "GitHubClientSecret":
  406. common.GitHubClientSecret = value
  407. case "LinuxDOClientId":
  408. common.LinuxDOClientId = value
  409. case "LinuxDOClientSecret":
  410. common.LinuxDOClientSecret = value
  411. case "LinuxDOMinimumTrustLevel":
  412. common.LinuxDOMinimumTrustLevel, _ = strconv.Atoi(value)
  413. case "Footer":
  414. common.Footer = value
  415. case "SystemName":
  416. common.SystemName = value
  417. case "Logo":
  418. common.Logo = value
  419. case "WeChatServerAddress":
  420. common.WeChatServerAddress = value
  421. case "WeChatServerToken":
  422. common.WeChatServerToken = value
  423. case "WeChatAccountQRCodeImageURL":
  424. common.WeChatAccountQRCodeImageURL = value
  425. case "TelegramBotToken":
  426. common.TelegramBotToken = value
  427. case "TelegramBotName":
  428. common.TelegramBotName = value
  429. case "TurnstileSiteKey":
  430. common.TurnstileSiteKey = value
  431. case "TurnstileSecretKey":
  432. common.TurnstileSecretKey = value
  433. case "QuotaForNewUser":
  434. common.QuotaForNewUser, _ = strconv.Atoi(value)
  435. case "QuotaForInviter":
  436. common.QuotaForInviter, _ = strconv.Atoi(value)
  437. case "QuotaForInvitee":
  438. common.QuotaForInvitee, _ = strconv.Atoi(value)
  439. case "QuotaRemindThreshold":
  440. common.QuotaRemindThreshold, _ = strconv.Atoi(value)
  441. case "PreConsumedQuota":
  442. common.PreConsumedQuota, _ = strconv.Atoi(value)
  443. case "ModelRequestRateLimitCount":
  444. setting.ModelRequestRateLimitCount, _ = strconv.Atoi(value)
  445. case "ModelRequestRateLimitDurationMinutes":
  446. setting.ModelRequestRateLimitDurationMinutes, _ = strconv.Atoi(value)
  447. case "ModelRequestRateLimitSuccessCount":
  448. setting.ModelRequestRateLimitSuccessCount, _ = strconv.Atoi(value)
  449. case "ModelRequestRateLimitGroup":
  450. err = setting.UpdateModelRequestRateLimitGroupByJSONString(value)
  451. case "RetryTimes":
  452. common.RetryTimes, _ = strconv.Atoi(value)
  453. case "DataExportInterval":
  454. common.DataExportInterval, _ = strconv.Atoi(value)
  455. case "DataExportDefaultTime":
  456. common.DataExportDefaultTime = value
  457. case "ModelRatio":
  458. err = ratio_setting.UpdateModelRatioByJSONString(value)
  459. case "GroupRatio":
  460. err = ratio_setting.UpdateGroupRatioByJSONString(value)
  461. case "GroupGroupRatio":
  462. err = ratio_setting.UpdateGroupGroupRatioByJSONString(value)
  463. case "UserUsableGroups":
  464. err = setting.UpdateUserUsableGroupsByJSONString(value)
  465. case "CompletionRatio":
  466. err = ratio_setting.UpdateCompletionRatioByJSONString(value)
  467. case "ModelPrice":
  468. err = ratio_setting.UpdateModelPriceByJSONString(value)
  469. case "CacheRatio":
  470. err = ratio_setting.UpdateCacheRatioByJSONString(value)
  471. case "CreateCacheRatio":
  472. err = ratio_setting.UpdateCreateCacheRatioByJSONString(value)
  473. case "ImageRatio":
  474. err = ratio_setting.UpdateImageRatioByJSONString(value)
  475. case "AudioRatio":
  476. err = ratio_setting.UpdateAudioRatioByJSONString(value)
  477. case "AudioCompletionRatio":
  478. err = ratio_setting.UpdateAudioCompletionRatioByJSONString(value)
  479. case "TopUpLink":
  480. common.TopUpLink = value
  481. //case "ChatLink":
  482. // common.ChatLink = value
  483. //case "ChatLink2":
  484. // common.ChatLink2 = value
  485. case "ChannelDisableThreshold":
  486. common.ChannelDisableThreshold, _ = strconv.ParseFloat(value, 64)
  487. case "QuotaPerUnit":
  488. common.QuotaPerUnit, _ = strconv.ParseFloat(value, 64)
  489. case "SensitiveWords":
  490. setting.SensitiveWordsFromString(value)
  491. case "AutomaticDisableKeywords":
  492. operation_setting.AutomaticDisableKeywordsFromString(value)
  493. case "AutomaticDisableStatusCodes":
  494. err = operation_setting.AutomaticDisableStatusCodesFromString(value)
  495. case "AutomaticRetryStatusCodes":
  496. err = operation_setting.AutomaticRetryStatusCodesFromString(value)
  497. case "StreamCacheQueueLength":
  498. setting.StreamCacheQueueLength, _ = strconv.Atoi(value)
  499. case "PayMethods":
  500. err = operation_setting.UpdatePayMethodsByJsonString(value)
  501. }
  502. return err
  503. }
  504. // handleConfigUpdate 处理分层配置更新,返回是否已处理
  505. func handleConfigUpdate(key, value string) bool {
  506. parts := strings.SplitN(key, ".", 2)
  507. if len(parts) != 2 {
  508. return false // 不是分层配置
  509. }
  510. configName := parts[0]
  511. configKey := parts[1]
  512. // 获取配置对象
  513. cfg := config.GlobalConfig.Get(configName)
  514. if cfg == nil {
  515. return false // 未注册的配置
  516. }
  517. // 更新配置
  518. configMap := map[string]string{
  519. configKey: value,
  520. }
  521. config.UpdateConfigFromMap(cfg, configMap)
  522. // 特定配置的后处理
  523. if configName == "performance_setting" {
  524. // 同步磁盘缓存配置到 common 包
  525. performance_setting.UpdateAndSync()
  526. }
  527. return true // 已处理
  528. }