- 随机生成 6 位数字
- 封装请求
/*
* @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))
return
}
// SendMsg 发送短信
func SendMsg(moblie, Code string) (code int) {
client, err := dysmsapi.NewClientWithAccessKey("cn-hangzhou", "你的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()
}
- 本来想附上官网链接的,突然失忆...
有疑问加站长微信联系(非本文作者)