**阿里具有php和nodejs的短信发送方法,在php的laravel当中无比简单,但木有go语言的,然后搞了一下,各位可以参考一下:**
`步骤1:首先需要在beego的app.conf中定义配置如下所示`
```go
appname = store
httpport = 8080
runmode = dev
sms_AppKey=你的ID
sms_AppSecret=你的密钥
sms_free_sign_name=你的阿里签名
sms_template_code=你的阿里模板代码
```
`步骤2:首先需要在beego的项目中新建utils目录,实现代码函数的封装如下:`
```go
package utils
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"sort"
"strings"
"time"
)
// SendSmsReply 发送短信返回
type SendSmsReply struct {
Code string `json:"Code,omitempty"`
Message string `json:"Message,omitempty"`
}
func replace(in string) string {
rep := strings.NewReplacer("+", "%20", "*", "%2A", "%7E", "~")
return rep.Replace(url.QueryEscape(in))
}
// SendSms 发送短信
func SendSms(accessKeyID, accessSecret, phoneNumbers, signName, templateParam, templateCode string) error {
paras := map[string]string{
"SignatureMethod": "HMAC-SHA1",
"SignatureNonce": fmt.Sprintf("%d", rand.Int63()),
"AccessKeyId": accessKeyID,
"SignatureVersion": "1.0",
"Timestamp": time.Now().UTC().Format("2006-01-02T15:04:05Z"),
"Format": "JSON",
"Action": "SendSms",
"Version": "2017-05-25",
"RegionId": "cn-hangzhou",
"PhoneNumbers": phoneNumbers,
"SignName": signName,
"TemplateParam": templateParam,
"TemplateCode": templateCode,
}
var keys []string
for k := range paras {
keys = append(keys, k)
}
sort.Strings(keys)
var sortQueryString string
for _, v := range keys {
sortQueryString = fmt.Sprintf("%s&%s=%s", sortQueryString, replace(v), replace(paras[v]))
}
stringToSign := fmt.Sprintf("GET&%s&%s", replace("/"), replace(sortQueryString[1:]))
mac := hmac.New(sha1.New, []byte(fmt.Sprintf("%s&", accessSecret)))
mac.Write([]byte(stringToSign))
sign := replace(base64.StdEncoding.EncodeToString(mac.Sum(nil)))
str := fmt.Sprintf("http://dysmsapi.aliyuncs.com/?Signature=%s%s", sign, sortQueryString)
resp, err := http.Get(str)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
ssr := &SendSmsReply{}
if err := json.Unmarshal(body, ssr); err != nil {
return err
}
if ssr.Code == "SignatureNonceUsed" {
return SendSms(accessKeyID, accessSecret, phoneNumbers, signName, templateParam, templateCode)
} else if ssr.Code != "OK" {
return errors.New(ssr.Code)
}
return nil
}
```
`步骤3:在控制器调用如下:`
```go
package controllers
import (
"fmt"
"github.com/astaxie/beego"
"store/utils"
)
type SmsController struct {
beego.Controller
}
func (this *SmsController) Get() {
sms_free_sign_name := beego.AppConfig.String("sms_free_sign_name")
sms_template_code := beego.AppConfig.String("sms_template_code")
sms_AppKey := beego.AppConfig.String("sms_AppKey")
sms_AppSecret := beego.AppConfig.String("sms_AppSecret")
if err := utils.SendSms(sms_AppKey, sms_AppSecret, "你的手机号码", sms_free_sign_name, "{code:'8558'}", sms_template_code); err != nil {
this.Ctx.WriteString( fmt.Sprintf("%v",err) )
}
this.Ctx.WriteString("ok.......")
}
```
有疑问加站长微信联系(非本文作者))
![](https://static.golangjob.cn/static/img/footer.png?imageView2/2/w/280)