package wechat_auth import ( "errors" "strings" "yunyan/lego/sys/log" "yunyan/lego/utils/mapstructure" ) type Option func(*Options) type Options struct { Debug bool //日志是否开启 Log log.ILogger AppID string AppSecret string } func SetAppID(v string) Option { return func(o *Options) { o.AppID = v } } func SetAppSecret(v string) Option { return func(o *Options) { o.AppSecret = v } } func newOptions(config map[string]interface{}, opts ...Option) Options { options := Options{} if config != nil { mapstructure.Decode(config, &options) } for _, o := range opts { o(&options) } if options.Log == nil { options.Log = log.NewTurnlog(options.Debug, log.Clone("sys.wechat_auth", 3)) } return options } func newOptionsByOption(opts ...Option) Options { options := Options{} for _, o := range opts { o(&options) } if options.Log == nil { options.Log = log.NewTurnlog(options.Debug, log.Clone("sys.wechat_auth", 3)) } return options } // newOptionsChecked 解析 map 配置并校验,不合法时返回 error 而非 panic。 // 启动(OnInit)与热重载(Reload)共用:配置缺失只应让微信登录降级 / 本次重载失败,不该打挂进程。 func newOptionsChecked(config map[string]interface{}, opts ...Option) (Options, error) { options := newOptions(config, opts...) // 后台表单粘贴时常带首尾空白,原样拼进 URL 微信会判 invalid appsecret。 options.AppID = strings.TrimSpace(options.AppID) options.AppSecret = strings.TrimSpace(options.AppSecret) if options.AppID == "" { return options, errors.New("wechat_auth 配置缺失: AppID 为空") } if options.AppSecret == "" { return options, errors.New("wechat_auth 配置缺失: AppSecret 为空") } return options, nil }