當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
容联云通讯短信平台JS调用
生活随笔
收集整理的這篇文章主要介紹了
容联云通讯短信平台JS调用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
容聯云通訊官網
1.注冊
注冊成功之后,會贈送8元進行測試
記錄賬號信息用于代碼中配置
添加測試號碼用于測試
2.接口配置(js)
var md5 = require('blueimp-md5') var moment = require('moment') var Base64 = require('js-base64').Base64; var request = require('request');/*生成指定長度的隨機數*/ function randomCode(length) {var chars = ['0','1','2','3','4','5','6','7','8','9'];var result = ""; //統一改名: alt + shift + Rfor(var i = 0; i < length ; i ++) {var index = Math.ceil(Math.random()*9);result += chars[index];}return result; } // console.log(randomCode(6)); exports.randomCode = randomCode;/* 向指定號碼發送指定驗證碼*/ function sendCode(phone, code, callback) {//填寫自己的信息var ACCOUNT_SID = '';var AUTH_TOKEN = '';var Rest_URL = 'https://app.cloopen.com:8883';var AppID = '';//1. 準備請求url/*1.使用MD5加密(賬戶Id + 賬戶授權令牌 + 時間戳)。其中賬戶Id和賬戶授權令牌根據url的驗證級別對應主賬戶。時間戳是當前系統時間,格式"yyyyMMddHHmmss"。時間戳有效時間為24小時,如:201404161420302.SigParameter參數需要大寫,如不能寫成sig=abcdefg而應該寫成sig=ABCDEFG*/var sigParameter = '';var time = moment().format('YYYYMMDDHHmmss');sigParameter = md5(ACCOUNT_SID+AUTH_TOKEN+time);var url = Rest_URL+'/2013-12-26/Accounts/'+ACCOUNT_SID+'/SMS/TemplateSMS?sig='+sigParameter;//2. 準備請求體var body = {to : phone,appId : AppID,templateId : '1',"datas":[code,"1"]}//body = JSON.stringify(body);//3. 準備請求頭/*1.使用Base64編碼(賬戶Id + 冒號 + 時間戳)其中賬戶Id根據url的驗證級別對應主賬戶2.冒號為英文冒號3.時間戳是當前系統時間,格式"yyyyMMddHHmmss",需與SigParameter中時間戳相同。*/var authorization = ACCOUNT_SID + ':' + time;authorization = Base64.encode(authorization);var headers = {'Accept' :'application/json','Content-Type' :'application/json;charset=utf-8','Content-Length': JSON.stringify(body).length+'','Authorization' : authorization}//4. 發送請求, 并得到返回的結果, 調用callback// callback(true);request({method : 'POST',url : url,headers : headers,body : body,json : true}, function (error, response, body) {console.log(error, response, body);callback(body.statusCode==='000000');// callback(true);}); } exports.sendCode = sendCode;3.node調用短信驗證碼接口
/* 發送驗證碼短信 */ router.get('/sendcode', function (req, res, next) {//1. 獲取請求參數數據var phone = req.query.phone;//2. 處理數據//生成驗證碼(6位隨機數)var code = sms_util.randomCode(6);//發送給指定的手機號console.log(`向${phone}發送驗證碼短信: ${code}`);sms_util.sendCode(phone, code, function (success) {//success表示是否成功if (success) {users[phone] = codeconsole.log('保存驗證碼: ', phone, code)res.send({"code": 0})} else {//3. 返回響應數據res.send({"code": 1, msg: '短信驗證碼發送失敗'})}}) })總結
以上是生活随笔為你收集整理的容联云通讯短信平台JS调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高德地图地理码和经纬度转化(latlng
- 下一篇: 产品经理课程学习笔记