在实际开发中需要通过动态切换功能或发送处理事件的情况下,可以使用eventBus来处理

如短信平台切换

在后台切换当前使用的短信平台

在代码中发布短信配置变更事件

import (
    commonService "github.com/tiger1103/gfast/v3/internal/app/common/service"
)

// PublishSmsPluginConfigChangeEvent 发布短信配置变更事件
func (s *smsService) PublishSmsPluginConfigChangeEvent(ctx context.Context) (err error) {
    var smsPluginInfo *model.PluginSmsConfigInfoRes
    err = dao.PluginSmsConfig.Ctx(ctx).Where("status", 1).Scan(&smsPluginInfo)
    if err != nil {
        return
    }
    if smsPluginInfo == nil {
        err = errors.New("未获取到短信配置信息")
        return
    }
    commonService.EventBus().Publish("sms_plugin:registerService", smsPluginInfo.SmsType)
    return
}

在代码中订阅并处理短信配置变更事件


import (
    commonService "github.com/tiger1103/gfast/v3/internal/app/common/service"
)

func Register() {
    commonService.EventBus().Subscribe("sms_plugin:registerService", func(smsType string) {
        switch smsType {
        case "demo":

        case "yunxin": //网易云信

        case "aly": //阿里云短信

        case "ten": //腾讯云短信

        }
    })
}
作者:袁学飞  创建时间:2024-01-26 10:37
最后编辑:袁学飞  更新时间:2024-04-16 15:39