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

Golang 网易云信(短信验证码) SDK

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

安装

go get github.com/daniuEvan/go-wysm/wysm

创建客户端

smClient := wysm.NewSmClient()

初始化配置

smClient.SmConfig.Mobile = "接收验证码的手机号"
smClient.SmConfig.AppKey = "网易云信AppKey"
smClient.SmConfig.AppSecret = "网易云信AppSecret"
smClient.SmConfig.SmTemplateCode = 19506299         // 验证码模板 默认为 19506299
smClient.SmConfig.CodeLen = 4                       // 验证码长度默认为 4

发送短信验证码

resJson, err := smClient.SendSmCode()  // 返回值resJson类型为json
  • 成功回resJson值, msg字段表示此次发送的sendid;obj字段表示此次发送的验证码。

    {
      "code": 200,   // 状态码
      "msg": "88",   
      "obj": "1908"  // 验证码
    }
    
  • 更多返回码请参考: https://doc.yunxin.163.com/docs/TM5MzM5Njk/Tk5ODIzNjk

  • 完整发送短信验证码demo

    smClient := wysm.NewSmClient()
    smClient.SmConfig.Mobile = "接收验证码的手机号"
    smClient.SmConfig.AppKey = "网易云信AppKey"
    smClient.SmConfig.AppSecret = "网易云信AppSecret"
    //获取验证码
    res, err := smClient.SendSmCode()
    if err != nil {
      log.Fatal(err.Error())
    }
    fmt.Println(res)
    

校验短信验证码

resJson, err := smClient.VerifySmCode("获取的验证码")   // 返回值resJson类型为json
  • 成功resJson返回值:

    {
      "code":200
    }
    
  • 更多返回码请参考: https://doc.yunxin.163.com/docs/TM5MzM5Njk/Tk5ODIzNjk

  • 完整校验短信验证码示例

    	smClient := wysm.NewSmClient()
    	smClient.SmConfig.Mobile = "接收验证码的手机号"
    	smClient.SmConfig.AppKey = "网易云信AppKey"
    	smClient.SmConfig.AppSecret = "网易云信AppSecret"
    	res, err := smClient.VerifySmCode("获取的验证码")
    	if err != nil {
    		log.Fatal(err.Error())
    	}
    	fmt.Println(res)
    

测试用例代码

  • 完整代码: https://github.com/daniuEvan/go-wysm/blob/master/wysm/wysm_test.go

    package wysm
    
    import (
    	"fmt"
    	"log"
    	"testing"
    )
    
    func TestSmClient_SendSmCode(t *testing.T) {
    	smClient := NewSmClient()
    	smClient.SmConfig.Mobile = "接收验证码的手机号"
    	smClient.SmConfig.AppKey = "网易云信AppKey"
    	smClient.SmConfig.AppSecret = "网易云信AppSecret"
    	//获取验证码
    	res, err := smClient.SendSmCode()
    	if err != nil {
    		log.Fatal(err.Error())
    	}
    	fmt.Println(res)
    }
    
    func TestSmClient_VerifySmCode(t *testing.T) {
    	smClient := NewSmClient()
    	smClient.SmConfig.Mobile = "接收验证码的手机号"
    	smClient.SmConfig.AppKey = "网易云信AppKey"
    	smClient.SmConfig.AppSecret = "网易云信AppSecret"
    	res, err := smClient.VerifySmCode("4128")
    	if err != nil {
    		log.Fatal(err.Error())
    	}
    	fmt.Println(res)
    }
    
    
  • 作者微信:foolish_is_me

  • 作者邮箱:big_ox@163.com

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