继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

刘沙河
关注TA
已关注
手记 4
粉丝 0
获赞 1

网易云信

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

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP