2024-08-20 06:35:14 +00:00
|
|
|
package senderservicelogic
|
|
|
|
|
|
|
|
import (
|
2024-08-20 13:08:00 +00:00
|
|
|
"app-cloudep-notification-service/internal/domain/usecase"
|
2024-08-20 06:35:14 +00:00
|
|
|
"context"
|
|
|
|
|
2024-08-20 15:53:20 +00:00
|
|
|
ers "code.30cm.net/digimon/library-go/errors"
|
|
|
|
|
2024-08-20 06:35:14 +00:00
|
|
|
"app-cloudep-notification-service/gen_result/pb/notification"
|
|
|
|
"app-cloudep-notification-service/internal/svc"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SendSmsLogic struct {
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
logx.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
|
|
|
|
return &SendSmsLogic{
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 13:08:00 +00:00
|
|
|
type sendSMSReq struct {
|
|
|
|
// RecipientAddress 收件者
|
|
|
|
RecipientAddress string `validate:"required,phone"`
|
|
|
|
// Body 內容
|
|
|
|
Body string `validate:"required"`
|
|
|
|
// RecipientName 收件者信名
|
|
|
|
RecipientName string `validate:"required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendSms 寄簡訊
|
2024-08-20 06:35:14 +00:00
|
|
|
func (l *SendSmsLogic) SendSms(in *notification.SendSMSReq) (*notification.OKResp, error) {
|
2024-08-20 13:08:00 +00:00
|
|
|
if err := l.svcCtx.Validate.ValidateAll(&sendSMSReq{
|
|
|
|
RecipientName: in.GetTo(),
|
|
|
|
Body: in.GetBody(),
|
|
|
|
RecipientAddress: in.GetTo(),
|
|
|
|
}); err != nil {
|
|
|
|
return nil, ers.InvalidFormat(err.Error())
|
|
|
|
}
|
|
|
|
|
2024-08-20 15:53:20 +00:00
|
|
|
// TODO 以後可以做換線
|
2024-08-20 13:08:00 +00:00
|
|
|
err := l.svcCtx.SMSSender.SendSMS(l.ctx, usecase.SMSReq{
|
|
|
|
RecipientAddress: in.GetTo(),
|
|
|
|
RecipientName: in.GetRecipientName(),
|
|
|
|
Body: in.GetBody(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logx.WithCallerSkip(1).WithFields(
|
|
|
|
logx.Field("func", "SMSSender.SendSMS"),
|
|
|
|
logx.Field("in", in),
|
|
|
|
logx.Field("err", err),
|
|
|
|
).Error(err.Error())
|
|
|
|
return nil, ers.ArkInternal("SMSSender.SendSMS failed to send sms")
|
|
|
|
}
|
2024-08-20 06:35:14 +00:00
|
|
|
|
|
|
|
return ¬ification.OKResp{}, nil
|
|
|
|
}
|