package service
import (
"encoding/json"
"fmt"
"ginStudy/tool"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
"math/rand"
"time"
)
type MemberService struct {
}
func (ms *MemberService) SendSms(phone string) bool {
//1.产生一个验证码
code := fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000))
//2.调用全局配置参数
config := tool.GetConfig().Sms
configSdk := sdk.NewConfig()
credential := credentials.NewAccessKeyCredential(config.AppKey, config.AppSecret)
client, err := dysmsapi.NewClientWithOptions(config.RegionId, configSdk, credential)
if err != nil {
panic(err)
}
//3.构造请求参数
request := dysmsapi.CreateSendSmsRequest()
request.Scheme = "https"
request.SignName = config.SignName
request.TemplateCode = config.TemplateCode
request.PhoneNumbers = phone
par, err := json.Marshal(map[string]interface{}{
"code": code,
})
request.TemplateParam = string(par)
//接收响应
response, err := client.SendSms(request)
if err != nil {
fmt.Print(err.Error())
return false
}
fmt.Printf("response is %#v\n", response)
//5.判断返回
if response.Code == "OK" {
return true
}
return false
}