11package config
22
33import (
4+ "fmt"
45 "strings"
56
67 "github.com/spf13/viper"
78
9+ "github.com/chaitin/MonkeyCode/backend/consts"
810 "github.com/chaitin/MonkeyCode/backend/pkg/logger"
911)
1012
@@ -318,6 +320,10 @@ func Init(dir string) (*Config, error) {
318320 v .SetConfigName ("config" )
319321 v .ReadInConfig ()
320322
323+ if err := normalizeWechatMPTemplates (v ); err != nil {
324+ return nil , err
325+ }
326+
321327 c := Config {}
322328 if err := v .Unmarshal (& c ); err != nil {
323329 return nil , err
@@ -326,6 +332,47 @@ func Init(dir string) (*Config, error) {
326332 return & c , nil
327333}
328334
335+ func normalizeWechatMPTemplates (v * viper.Viper ) error {
336+ raw := v .GetStringMap ("wechat.mp.templates" )
337+ if len (raw ) == 0 {
338+ return nil
339+ }
340+
341+ flat := make (map [string ]string , len (raw ))
342+ for key , value := range raw {
343+ valueStr , ok := value .(string )
344+ if ! ok {
345+ return fmt .Errorf ("invalid wechat.mp.templates value type at %s: %T" , key , value )
346+ }
347+
348+ normalizedKey := normalizeWechatMPTemplateKey (key )
349+ if _ , exists := flat [normalizedKey ]; exists && normalizedKey != key {
350+ continue
351+ }
352+ flat [normalizedKey ] = valueStr
353+ }
354+
355+ v .Set ("wechat.mp.templates" , flat )
356+ return nil
357+ }
358+
359+ func normalizeWechatMPTemplateKey (key string ) string {
360+ switch key {
361+ case "vm_expiring_soon" :
362+ return string (consts .NotifyEventVMExpiringSoon )
363+ case "quota_refreshed" :
364+ return string (consts .NotifyEventQuotaRefreshed )
365+ case "quota_basic_exhausted" :
366+ return string (consts .NotifyEventQuotaBasicExhausted )
367+ case "quota_pro_exhausted" :
368+ return string (consts .NotifyEventQuotaProExhausted )
369+ case "quota_ultra_exhausted" :
370+ return string (consts .NotifyEventQuotaUltraExhausted )
371+ default :
372+ return key
373+ }
374+ }
375+
329376// GithubConfig GitHub 配置
330377type GithubConfig struct {
331378 Token string `mapstructure:"token"`
0 commit comments