2025-10-02 16:16:33 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"backend/pkg/notification/config"
|
|
|
|
|
"backend/pkg/notification/domain"
|
|
|
|
|
"backend/pkg/notification/domain/repository"
|
|
|
|
|
"context"
|
2025-10-22 13:40:31 +00:00
|
|
|
"fmt"
|
2025-10-02 16:16:33 +00:00
|
|
|
|
2025-10-02 16:36:34 +00:00
|
|
|
"backend/pkg/library/errs"
|
2025-10-02 16:16:33 +00:00
|
|
|
"backend/pkg/library/errs/code"
|
|
|
|
|
|
|
|
|
|
"github.com/minchao/go-mitake"
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// MitakeSMSDeliveryParam 三竹傳送參數配置
|
|
|
|
|
type MitakeSMSDeliveryParam struct {
|
|
|
|
|
Conf *config.MitakeSMSSender
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MitakeSMSDeliveryRepository struct {
|
|
|
|
|
Client *mitake.Client
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-22 13:40:31 +00:00
|
|
|
func (repo *MitakeSMSDeliveryRepository) SendSMS(ctx context.Context, req repository.SMSMessageRequest) error {
|
|
|
|
|
// 檢查 context 是否已取消
|
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
|
return ctx.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 構建簡訊訊息
|
|
|
|
|
message := mitake.Message{
|
|
|
|
|
Dstaddr: req.PhoneNumber,
|
|
|
|
|
Destname: req.RecipientName,
|
|
|
|
|
Smbody: req.MessageContent,
|
|
|
|
|
}
|
2025-10-02 16:16:33 +00:00
|
|
|
|
2025-10-22 13:40:31 +00:00
|
|
|
// 直接發送,不使用 goroutine pool
|
|
|
|
|
// 讓 delivery usecase 統一管理重試和超時
|
|
|
|
|
_, err := repo.Client.Send(message)
|
2025-10-02 16:16:33 +00:00
|
|
|
if err != nil {
|
2025-10-22 13:40:31 +00:00
|
|
|
return errs.ThirdPartyErrorL(
|
2025-10-02 16:16:33 +00:00
|
|
|
code.CloudEPNotification,
|
|
|
|
|
domain.FailedToSendSMSErrorCode,
|
|
|
|
|
logx.WithContext(ctx),
|
|
|
|
|
[]logx.LogField{
|
|
|
|
|
{Key: "req", Value: req},
|
2025-10-22 13:40:31 +00:00
|
|
|
{Key: "func", Value: "MitakeSMSDeliveryRepository.Send"},
|
2025-10-02 16:16:33 +00:00
|
|
|
{Key: "err", Value: err.Error()},
|
|
|
|
|
},
|
2025-10-22 13:40:31 +00:00
|
|
|
fmt.Sprintf("failed to send sms by mitake: %v", err)).Wrap(err)
|
2025-10-02 16:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-22 13:40:31 +00:00
|
|
|
logx.WithContext(ctx).Infof("SMS sent successfully via Mitake to %s", req.PhoneNumber)
|
2025-10-02 16:16:33 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func MustMitakeRepository(param MitakeSMSDeliveryParam) repository.SMSClientRepository {
|
|
|
|
|
return &MitakeSMSDeliveryRepository{
|
|
|
|
|
Client: mitake.NewClient(param.Conf.User, param.Conf.Password, nil),
|
|
|
|
|
}
|
|
|
|
|
}
|