init repo
This commit is contained in:
65
pkg/sms/sms.go
Normal file
65
pkg/sms/sms.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package sms
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"gitea.com/bitwsd/document_ai/config"
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
|
||||
dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
|
||||
aliutil "github.com/alibabacloud-go/tea-utils/service"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
)
|
||||
|
||||
const (
|
||||
Signature = "北京比特智源科技"
|
||||
TemplateCode = "SMS_291510729"
|
||||
TemplateParam = `{"code":"%s"}`
|
||||
VerifyCodeLength = 6
|
||||
VerifyCodeExpire = 3 * 60 // 1 minutes
|
||||
)
|
||||
|
||||
var (
|
||||
MsgClient *dysmsapi.Client
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
func InitSmsClient() *dysmsapi.Client {
|
||||
once.Do(func() {
|
||||
key := tea.String(config.GlobalConfig.Aliyun.Sms.AccessKeyID)
|
||||
secret := tea.String(config.GlobalConfig.Aliyun.Sms.AccessKeySecret)
|
||||
config := &openapi.Config{AccessKeyId: key, AccessKeySecret: secret}
|
||||
client, err := dysmsapi.NewClient(config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
MsgClient = client
|
||||
})
|
||||
return MsgClient
|
||||
}
|
||||
|
||||
type SendSmsRequest struct {
|
||||
PhoneNumbers string
|
||||
SignName string
|
||||
TemplateCode string
|
||||
TemplateParam string
|
||||
}
|
||||
|
||||
func SendMessage(req *SendSmsRequest) (err error) {
|
||||
client := MsgClient
|
||||
request := &dysmsapi.SendSmsRequest{
|
||||
PhoneNumbers: tea.String(req.PhoneNumbers),
|
||||
SignName: tea.String(req.SignName),
|
||||
TemplateCode: tea.String(req.TemplateCode),
|
||||
TemplateParam: tea.String(req.TemplateParam),
|
||||
}
|
||||
resp, err := client.SendSms(request)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !tea.BoolValue(aliutil.EqualString(resp.Body.Code, tea.String("OK"))) {
|
||||
err = errors.New(*resp.Body.Code)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user