为了账号安全,请及时绑定邮箱和手机立即绑定

Go使用网易云信发送短信验证码

标签:
Go

网易云信

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 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Python工程师
手记
粉丝
0
获赞与收藏
1

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消