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

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