/*
* @Description:
* @Author: Casso-Wong
*/
package alimsg
import (
"fmt"
"math/rand"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
)
// Code 生成6位数随机码-- int
func Code() (res string) {
rand.Seed(time.Now().Unix())
rnd := rand.New(rand.NewSource(time.Now().Unix()))
res = fmt.Sprintf("%06v", rnd.Intn(1000000))
// 可能大家都已经发现了,如果随机生成的Code以0开头就会被阿里去掉,所以要稍微解决一下
if res[0] == '0'{
res = Code()
}
return
}
// SendMsg 发送短信
func SendMsg(moblie, Code string) (code int) {
client, err := dysmsapi.NewClientWithAccessKey("********", "你的AccessKey ID", "你的AccessKey Secret
")
request := dysmsapi.CreateSendSmsRequest()
request.Scheme = "https"
request.PhoneNumbers = moblie
request.SignName = "短信开头"
request.TemplateCode = "短信模板代码"
request.TemplateParam = fmt.Sprintf("{code:%s}", Code)
response, err := client.SendSms(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("ali sendmsg response code is %#v\n", response.BaseResponse.GetHttpStatus())
return response.BaseResponse.GetHttpStatus()
}