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.
 
 
 

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