You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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