Tencent cloud, SMS sdk access, used in vue2

Tencent cloud, SMS sdk access, used in vue2

Tencent Cloud SMS SDK URL Enter the official website to directly search SMS SDK
insert image description here

1. Scan the QR code to log in on WeChat, enter as follows

insert image description here

2. The signature is created by yourself, and it takes one day for reviewof

3. As follows, creating a template also takes time to review

insert image description here

4. You need to pay attention here, and do the preparatory work as follows

Note that this SMS signature needs to be created by an enterprise or by creating a WeChat subscription account. The SMS signature needs to be reviewed. Select the official account, pay attention! The signature and the name of the official account must be consistent, otherwise the creation cannot pass
insert image description here
the following three pictures, it is necessary
Please add a picture description
Please add a picture description
Please add a picture description

5. Ready to start writing code

First of all, the front end only needs to request the interface

           //请求短信验证码接口
			http.$axios({
				url:'/api/code',
				method:'POST',
				data:{
			 // 传给后台,的手机号
					phone:this.userTel
				}
			}).then(res=>{
				if( res.success ){ // 如果接收的,根据后台返回的验证
				alert('验证码是' + res.data)
					this.code = res.data;
				}
			})

6. The background is really miserable, and I feel sorry for it. I use node.js to write it here (more important)

Access the SMS verification code SDK api link: download in node,
refer to the node connection sdk article
to download cnpm install qcloudsms_js -S

Use it in the express framework created by your own server, node.js, create your own route, copy and paste below, and change the three pictures in the above 4, just the information

var QcloudSms = require("qcloudsms_js");
//发送短信验证码
router.post('/api/code',function(req,res,next){
	// 前端传给后台的手机号
	let tel = req.body.phone;
	
	// 短信应用SDK AppID
	var appid = 1400187558;  // SDK AppID是1400开头
	
	// 短信应用SDK AppKey
	var appkey = "dc9dc3391896235ddc2325685047edc7";
	
	// 需要发送短信的手机号码
	var phoneNumbers = [tel];
	
	// 短信模板ID,需要在短信应用中申请
	var templateId = 285590;  // NOTE: 这里的模板ID`7839`只是一个示例,真实的模板ID需要在短信控制台中申请
	
	// 签名
	var smsSign = "三人行慕课";  // NOTE: 这里的签名只是示例,请使用真实的已申请的签名, 签名参数使用的是`签名内容`,而不是`签名ID`
	
	// 实例化QcloudSms
	var qcloudsms = QcloudSms(appid, appkey);
	
	// 设置请求回调处理, 这里只是演示,用户需要自定义相应处理回调
	function callback(err, ress, resData) {
	    if (err) {
	        console.log("err: ", err);
	    } else {
			res.send({
				code:200,
				data:{
					success:true,
					data:ress.req.body.params[0]
				}
			})
	    }
	}
	
	var ssender = qcloudsms.SmsSingleSender();
	//这个变量:params 就是往手机上,发送的短信
	var params = [  Math.floor( Math.random()*(9999-1000))+1000   ];
	ssender.sendWithParam(86, phoneNumbers[0], templateId,
	  params, smsSign, "", "", callback);  // 签名参数不能为空串
	
})

appid needs to be changed to your own SMS application SDK AppID
appkey needs to be changed to your own SMS application SDK AppKey
Please add a picture description

templateId // Need to change to the following picture, your own SMS template IDPlease add a picture description

smsSign Please use the real signed signature, the signature parameter is used 签名内容instead of签名ID
insert image description here

7. It’s over, send text messages by yourself, free users only need 100 text messages, don’t stop playing, and don’t use it in illegal ways, it’s only for learning

Guess you like

Origin blog.csdn.net/weixin_46426412/article/details/129691880