网易云信
DEMO
1. 发送短信
// 发送验证码
const (
SendSmBaseUrl = "https://api.netease.im/sms/sendcode.action"
AppSecret = "AppSecret"
APPKey = "APPKey"
SMTemplateCode = 19505317
CodeLen = 4
)
// VerifySmBaseUrl 校验验证码
const (
VerifySmBaseUrl = "https://api.netease.im/sms/verifycode.action"
)
//
// BuildCheckSum
// @Description: 构建 BuildCheckSum
//
func BuildCheckSum(curTime, nonce string) (checkSum string, err error) {
resStr := AppSecret + nonce + curTime
t := sha1.New()
_, err = io.WriteString(t, resStr)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", t.Sum(nil)), nil
}
//
// SendSM
// @Description: 发送短信
// @param mobile:
// @return resObj: resObj
//
func SendSM(mobile int) (resObj string, err error) {
rand.Seed(time.Now().UnixNano())
nonce := strconv.FormatInt(int64(rand.Intn(5)), 10)
curTime := strconv.FormatInt(time.Now().Unix(), 10)
checkSum, err := BuildCheckSum(curTime, nonce)
if err != nil {
return "", err
}
client := http.Client{}
body := strings.NewReader(fmt.Sprintf(`mobile=%d`, mobile))
request, err := http.NewRequest("POST", SendSmBaseUrl, body)
// 增加 head 选项
request.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
request.Header.Add("AppKey", APPKey)
request.Header.Add("CurTime", curTime)
request.Header.Add("Nonce", nonce)
request.Header.Add("CheckSum", checkSum)
response, err := client.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
resMsg, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
fmt.Println(string(resMsg))
return string(resMsg), err
}
2. 验证短信
//
// VeritySMCode
// @Description: 校验验证码
// @param msg:
// @param obj:
// @return err:
//
func VeritySMCode(mobile, code int) (resObj string, err error) {
rand.Seed(time.Now().UnixNano())
nonce := strconv.FormatInt(int64(rand.Intn(5)), 10)
curTime := strconv.FormatInt(time.Now().Unix(), 10)
checkSum, err := BuildCheckSum(curTime, nonce)
if err != nil {
return "", err
}
client := http.Client{}
body := strings.NewReader(fmt.Sprintf(`mobile=%d&code=%d`, mobile, code))
request, err := http.NewRequest("POST", VerifySmBaseUrl, body)
// 增加 head 选项
request.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
request.Header.Add("AppKey", APPKey)
request.Header.Add("CurTime", curTime)
request.Header.Add("Nonce", nonce)
request.Header.Add("CheckSum", checkSum)
response, err := client.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
resMsg, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
fmt.Println(string(resMsg))
return string(resMsg), err
}
func main() {
SendSM(17660626526)
//VeritySMCode(17660626526, 2458)
}
3. 完整示例
/**
* @date: 2022/2/17
* @desc: ...
*/
package main
import (
"crypto/sha1"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
)
// 发送验证码
const (
SendSmBaseUrl = "https://api.netease.im/sms/sendcode.action"
AppSecret = "AppSecret"
APPKey = "APPKey"
SMTemplateCode = 19505317
CodeLen = 4
)
// VerifySmBaseUrl 校验验证码
const (
VerifySmBaseUrl = "https://api.netease.im/sms/verifycode.action"
)
//
// BuildCheckSum
// @Description: 构建 BuildCheckSum
//
func BuildCheckSum(curTime, nonce string) (checkSum string, err error) {
resStr := AppSecret + nonce + curTime
t := sha1.New()
_, err = io.WriteString(t, resStr)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", t.Sum(nil)), nil
}
//
// SendSM
// @Description: 发送短信
// @param mobile:
// @return resObj: resObj
//
func SendSM(mobile int) (resObj string, err error) {
rand.Seed(time.Now().UnixNano())
nonce := strconv.FormatInt(int64(rand.Intn(5)), 10)
curTime := strconv.FormatInt(time.Now().Unix(), 10)
checkSum, err := BuildCheckSum(curTime, nonce)
if err != nil {
return "", err
}
client := http.Client{}
body := strings.NewReader(fmt.Sprintf(`mobile=%d`, mobile))
request, err := http.NewRequest("POST", SendSmBaseUrl, body)
// 增加 head 选项
request.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
request.Header.Add("AppKey", APPKey)
request.Header.Add("CurTime", curTime)
request.Header.Add("Nonce", nonce)
request.Header.Add("CheckSum", checkSum)
response, err := client.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
resMsg, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
fmt.Println(string(resMsg))
return string(resMsg), err
}
//
// VeritySMCode
// @Description: 校验验证码
// @param msg:
// @param obj:
// @return err:
//
func VeritySMCode(mobile, code int) (resObj string, err error) {
rand.Seed(time.Now().UnixNano())
nonce := strconv.FormatInt(int64(rand.Intn(5)), 10)
curTime := strconv.FormatInt(time.Now().Unix(), 10)
checkSum, err := BuildCheckSum(curTime, nonce)
if err != nil {
return "", err
}
client := http.Client{}
body := strings.NewReader(fmt.Sprintf(`mobile=%d&code=%d`, mobile, code))
request, err := http.NewRequest("POST", VerifySmBaseUrl, body)
// 增加 head 选项
request.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
request.Header.Add("AppKey", APPKey)
request.Header.Add("CurTime", curTime)
request.Header.Add("Nonce", nonce)
request.Header.Add("CheckSum", checkSum)
response, err := client.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
resMsg, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
fmt.Println(string(resMsg))
return string(resMsg), err
}
func main() {
SendSM(17660626526)
//VeritySMCode(17660626526, 2458)
}
作者微信:foolish_is_me
作者邮箱:big_ox@163.com
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦