Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

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