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.
114 lines
2.9 KiB
114 lines
2.9 KiB
6 months ago
|
package biz
|
||
|
|
||
|
import (
|
||
|
"bkb-notify/internal/conf"
|
||
|
"bkb-notify/internal/utils"
|
||
|
"fmt"
|
||
|
"strconv"
|
||
|
|
||
|
"errors"
|
||
|
|
||
|
"github.com/go-kratos/kratos/v2/log"
|
||
|
)
|
||
|
|
||
|
// ErrUserNotFound is user not found.
|
||
|
|
||
|
// GreeterRepo is a Greater repo.
|
||
|
type SmsRepo interface {
|
||
|
// setcode
|
||
|
CreateSms(phone, code string) error
|
||
|
CheckCode(phone, code string) error
|
||
|
}
|
||
|
|
||
|
// EmailUsecase is a Greeter usecase.
|
||
|
type SmsUsecase struct {
|
||
|
repo SmsRepo
|
||
|
log *log.Helper
|
||
|
c *conf.App_Dysmsapi
|
||
|
}
|
||
|
|
||
|
// NewGreeterUsecase new a Greeter usecase.
|
||
|
func NewSmsUsecase(repo SmsRepo, logger log.Logger, c *conf.App) *SmsUsecase {
|
||
|
return &SmsUsecase{repo: repo, log: log.NewHelper(logger), c: c.Dysmsapi}
|
||
|
}
|
||
|
|
||
|
func (uc *SmsUsecase) SendSms(phone string, smsType int, isGlobal bool) (err error) {
|
||
|
var (
|
||
|
code, paramStr string
|
||
|
signName, templateCode string
|
||
|
)
|
||
|
switch smsType {
|
||
|
case 1:
|
||
|
codeNum := utils.GenerateRandNumByCount(6)
|
||
|
code = strconv.Itoa(codeNum)
|
||
|
paramStr = "{\"code\":\"" + code + "\"}"
|
||
|
signName = uc.c.SignName
|
||
|
templateCode = uc.c.TemplateCode1
|
||
|
if isGlobal {
|
||
|
signName = uc.c.SignNameGlobal
|
||
|
templateCode = uc.c.TemplateCodeGlobal1
|
||
|
}
|
||
|
// signName = "风芒"
|
||
|
// templateCode = "SMS_217921050"
|
||
|
case 2:
|
||
|
codeNum := utils.GenerateRandNumByCount(6)
|
||
|
code = strconv.Itoa(codeNum)
|
||
|
paramStr = "{\"code\":\"" + code + "\"}"
|
||
|
signName = uc.c.SignName
|
||
|
templateCode = uc.c.TemplateCode2
|
||
|
if isGlobal {
|
||
|
signName = uc.c.SignNameGlobal
|
||
|
templateCode = uc.c.TemplateCodeGlobal2
|
||
|
}
|
||
|
|
||
|
case 3:
|
||
|
codeNum := utils.GenerateRandNumByCount(6)
|
||
|
code = strconv.Itoa(codeNum)
|
||
|
paramStr = "{\"code\":\"" + code + "\"}"
|
||
|
signName = uc.c.SignName
|
||
|
templateCode = uc.c.TemplateCode3
|
||
|
if isGlobal {
|
||
|
signName = uc.c.SignNameGlobal
|
||
|
templateCode = uc.c.TemplateCodeGlobal3
|
||
|
}
|
||
|
case 4:
|
||
|
codeNum := utils.GenerateRandNumByCount(6)
|
||
|
code = strconv.Itoa(codeNum)
|
||
|
paramStr = "{\"code\":\"" + code + "\"}"
|
||
|
signName = uc.c.SignName
|
||
|
templateCode = uc.c.TemplateCode4
|
||
|
if isGlobal {
|
||
|
signName = uc.c.SignNameGlobal
|
||
|
templateCode = uc.c.TemplateCodeGlobal4
|
||
|
}
|
||
|
default:
|
||
|
codeNum := utils.GenerateRandNumByCount(6)
|
||
|
code = strconv.Itoa(codeNum)
|
||
|
paramStr = "{\"code\":\"" + code + "\"}"
|
||
|
// signName = "一个地球"
|
||
|
// templateCode = "SMS_257662876"
|
||
|
signName = uc.c.SignName
|
||
|
templateCode = uc.c.TemplateCode1
|
||
|
}
|
||
|
//todo 测试通过后在develop环境关闭验证码
|
||
|
if err = utils.SendSms(phone, paramStr, signName, templateCode, 1, uc.c); err != nil {
|
||
|
fmt.Println(err.Error())
|
||
|
if err.Error() == "isv.BUSINESS_LIMIT_CONTROL" {
|
||
|
err = errors.New("短信发送次数过多,请稍后再试")
|
||
|
} else {
|
||
|
err = errors.New("验证码发送失败,请稍后重试")
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
uc.repo.CreateSms(phone, code)
|
||
|
// _ = service.RedisSet("msg_code:"+phone, code, 5*time.Minute)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (uc *SmsUsecase) CheckCode(phone string, code string) (err error) {
|
||
|
|
||
|
err = uc.repo.CheckCode(phone, code)
|
||
|
// _ = service.RedisSet("msg_code:"+phone, code, 5*time.Minute)
|
||
|
return err
|
||
|
}
|