let request = require('request')
let CryptoJS = require("crypto-js")
let { getTimeStamp,
getNonce,
getSortASCII } = require('./utils/index')
let host = 'sms.tencentcloudapi.com'
// 接口和签名公用的对象参数
let obj = {
Action: 'SendSms',
Version: '2019-07-11',
'PhoneNumberSet.0': '',
TemplateID: '576795',
SmsSdkAppid: '1400350867',
Sign: '三天一个消息',
'TemplateParamSet.0': '',
Timestamp: getTimeStamp(),
Nonce: getNonce(100000, 999999),
SecretId: 'AKID0kvpapofMo25sDPVL7hcHjQzKOAkIA99',
}
// 获取签名字段值
function getSignature() {
// console.log(obj)
let sortObj = getSortASCII(obj)
let arr = Object.keys(sortObj)
let str = ''
for (var i = 0, len = arr.length; i < len; i++) {
arr[i] += '=' + sortObj[arr[i]]
}
str = arr.join('&')
str = 'GET' + host + '/?' + str
return CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(str, 'M8mGk7LNZgyNOK1RbaRxKr69yZoxiDmN'))
}
function getSMS(tel, code) {
if (!tel || !code) { return }
obj['PhoneNumberSet.0'] = '+86' + tel
obj['TemplateParamSet.0'] = code
obj.Signature = getSignature().trim()
console.log(obj.Signature === obj.Signature.trim())
obj.Signature = obj.Signature.trim()
request.get({
url: 'https://' + host,
qs: obj
}, function (err, response, body) {
console.log(body)
})
}
getSMS('18720072312', getNonce(100000, 999999))
utills/index文件中的工具函数
function getTimeStamp() {
return (Date.now() + '').substr(0, 10)
}
// 获取31位以内的随机数
function getNonceStr(len) {
var len = Number(len)
if (len + '' == 'NaN') {
return console.log('数据不是数字')
}
if (len >= 32) {
len = 31
} else {
len = len || 32;
}
var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
var maxPos = $chars.length;
var pwd = '';
for (i = 0; i < len; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
// 获取随机正整数 ,范围[n,m]
function getNonce(n, m) {
return parseInt(Math.random() * (m - n + 1) + n)
}
function getSortASCII(obj) {
var arr = new Array();
var num = 0;
for (var i in obj) {
arr[num] = i;
num++;
}
var sortArr = arr.sort();
var sortObj = {};
for (var i in sortArr) {
sortObj[sortArr[i]] = obj[sortArr[i]];
}
return sortObj;
}
module.exports = {
getTimeStamp,
getNonceStr,
getNonce,
getSortASCII
}
前端主要实现这几个功能:
1)点击后倒计时效果
2)判断刷新页面前,是否点击过发送验证码
3)判断发送短信次数是否超过当日限制
<button bindtap="sendSMS" disabled="{{isokSend}}">{{smsText}}</button>
data: {
isokSend: false,
smsText: '发送短信验证码'
},
time: function(wait) {
let timer = setInterval(() => {
if (wait <= 0) {
this.setData({
isokSend: false,
smsText: '发送短信验证码'
})
clearInterval(timer)
wait = 30
} else {
this.setData({
smsText: wait + '秒后重试',
isokSend: true
})
wait--
}
}, 1000)
},
onLoad: function(options) {
// 判断刷新页面前,是否点击过发送验证码
let oldSmsTimestap = wx.getStorageSync('smsTimestap')
if (oldSmsTimestap) {
let newSmsTimestap = Number((Date.now() + '').substr(0, 10))
oldSmsTimestap = Number(oldSmsTimestap)
let wait = newSmsTimestap - oldSmsTimestap
console.log(newSmsTimestap, oldSmsTimestap)
console.log(wait)
if (wait < 60) {
this.time(60 - wait)
}
}
},
sendSMS: function() {
let that = this
//判断发送短信次数是否超过当日限制(这里是2次)
let num = wx.getStorageSync('limitSms')
if (num) {
num = Number(num)
if (num >= 3) {
that.setData({
smsText: '次数超过限制,请明日再试',
isokSend: true
})
}
}
wx.request({
url: 'https://xiangzizhuye.top:3002/getSMS/sendSMS',
method: 'POST',
data: {
code: '257257',
tel: '18720072431'
},
success(res) {
if (res && res.data === '发送成功') {
wx.setStorageSync('smsTimestap', (Date.now() + '').substr(0, 10))
if (num) {
num = Number(num)
if (num >= 2) {
} else {
wx.setStorageSync('limitSms', (num + 1) + '')
}
} else {
wx.setStorageSync('limitSms', '1')
}
that.time(60)
} else {
console.log('发送验证码失败')
}
},
fail(err) {
console.log(err)
}
})
},
原生安卓和ios可以实现一键登陆,不需要发送短信