You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
1.8 KiB
86 lines
1.8 KiB
package sms
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"matchmaking-system/internal/pkg/flags"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
// ALiYunCase
|
|
// @Description:
|
|
type ALiYunCase struct {
|
|
aly ALiYunRepo
|
|
|
|
log *log.Helper
|
|
}
|
|
|
|
// ALiYunRepo
|
|
// @Description:
|
|
type ALiYunRepo interface {
|
|
SendVerificationCode(ctx context.Context, phoneNumber string) (string, error)
|
|
CheckVerificationCode(ctx context.Context, phoneNumber, verificationCode string) error
|
|
CheckVerificationCodeNew(ctx context.Context, phoneNumber string) (string, error)
|
|
}
|
|
|
|
// NewMsgSend
|
|
//
|
|
// @Description:
|
|
// @param ur
|
|
// @param logger
|
|
// @return *ALiYunCase
|
|
func NewMsgSend(ur ALiYunRepo, logger log.Logger) *ALiYunCase {
|
|
return &ALiYunCase{aly: ur, log: log.NewHelper(logger)}
|
|
}
|
|
|
|
// RunSendSms
|
|
//
|
|
// @Description:
|
|
// @receiver al
|
|
// @param ctx
|
|
// @param phoneNumber
|
|
// @return string
|
|
// @return error
|
|
func (al *ALiYunCase) RunSendSms(ctx context.Context, phoneNumber string) (string, error) {
|
|
code, err := al.aly.CheckVerificationCodeNew(ctx, phoneNumber)
|
|
if err != nil {
|
|
return err.Error(), err
|
|
}
|
|
|
|
if len(code) > 0 {
|
|
return code, err
|
|
}
|
|
|
|
// Request verification code
|
|
codeMsg, err := al.aly.SendVerificationCode(ctx, phoneNumber)
|
|
if err != nil {
|
|
return flags.SetNull, err
|
|
}
|
|
|
|
return codeMsg, nil
|
|
}
|
|
|
|
// CreateRandCode
|
|
//
|
|
// @Description: 创建6位随机数
|
|
// @param ctx
|
|
// @return string
|
|
func CreateRandCode(ctx context.Context) string {
|
|
return fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
|
|
}
|
|
|
|
// CreateRandCodeFrom
|
|
//
|
|
// @Description: 创建一个10位随机数作为发送方
|
|
// @param ctx
|
|
// @param n
|
|
// @return string
|
|
func CreateRandCodeFrom(ctx context.Context, n int) string {
|
|
code := flags.SetNull
|
|
for i := 0; i < n; i++ {
|
|
code = fmt.Sprintf("%s%d", code, rand.Intn(10))
|
|
}
|
|
return code
|
|
}
|
|
|