sms_client.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package sms
  2. import (
  3. "eta/eta_mini_ht_api/common/component/config"
  4. "github.com/beego/beego/v2/core/logs"
  5. )
  6. var (
  7. smsClient SMSClient
  8. )
  9. const (
  10. Juhe = "juhe"
  11. Emas = "emas"
  12. )
  13. type SMSClient interface {
  14. InitSender(config *config.SMSConfig)
  15. GetExpireMinute() int
  16. SendSms(mobile string, code string, smId int) (rs string, err error)
  17. }
  18. var clients = make(map[string]Instance)
  19. type Instance func() SMSClient
  20. func getClient(config *config.SMSConfig) (client SMSClient) {
  21. name := config.GetSmsType()
  22. instanceFunc, ok := clients[name]
  23. if !ok {
  24. logs.Error("不支持当前的短信平台", name)
  25. return nil
  26. }
  27. client = instanceFunc()
  28. client.InitSender(config)
  29. return client
  30. }
  31. func GetInstance() SMSClient {
  32. return smsClient
  33. }
  34. func InitSmsSender(config *config.SMSConfig) {
  35. smsClient = getClient(config)
  36. }
  37. func Register(name string, adapter Instance) {
  38. if adapter == nil {
  39. panic("不支持当前的短信平台")
  40. }
  41. if _, ok := clients[name]; ok {
  42. panic("当前短信平台类型已注册:" + name)
  43. }
  44. clients[name] = adapter
  45. }