官方接口: [容联云短信接口
](https://doc.yuntongxun.com/p/5a533de33b8496dd00dce07c)
- 统一请求包头
- HTTP标准包头字段
- 请求包体
- 响应
statusCode/smsMessageSid/dateCreated三个字段说明:- 当请求失败时,响应中只有statusCode和statusMsg
- 当请求成功时,响应中有statusCode和smsMessageSid, dateCreated
注:对响应解析后,statusCode为"000000"表示请求发送成功。statusCode不是"000000",表示请求发送失败。
call API
const fetch = require('node-fetch');
const CryptoJS = require('crypto-js');
const moment = require('moment');
const BASE_URL = 'https://app.cloopen.com:8883';
const ACCOUNT_SID = '';//查询平台自己的key值
const AUTH_TOKEN = '';//查询平台自己的key值
const APP_ID = '';//查询平台自己的key值
const TEMPLATE_ID = '';//查询平台自己的key值
let timeStamp;
const getTimeStamp = () => moment().format('YYYYMMDDhhmmss');
const getSigParameter = () => {
timeStamp = getTimeStamp();
const message = `${ACCOUNT_SID}${AUTH_TOKEN}${timeStamp}`;
const encodedMessgae = CryptoJS.MD5(message).toString();
return encodedMessgae.toUpperCase();
};
const getAuthorization = () => {
const str = `${ACCOUNT_SID}:${timeStamp}`;
const wordArray = CryptoJS.enc.Utf8.parse(str);
const encodedMessgae = CryptoJS.enc.Base64.stringify(wordArray);
return encodedMessgae;
};
const getUrl = () => {
const SigParameter = getSigParameter();
return `${BASE_URL}/2013-12-26/Accounts/${ACCOUNT_SID}/SMS/TemplateSMS?sig=${SigParameter}`;
};
const parseJSON = (response) => {
return response.json ? response.json() : response;
};
const checkStatus = (response) => {
if (response.status >= 200 && response.status < 300) {
return response;
}
return parseJSON(response).then((responseFormatted) => {
return {
error: true,
message: responseFormatted.message || response.statusText || 'Fail.',
};
});
};
exports.submitPhoneNumberVerification = (phoneNumber, code) => {
const url = getUrl();
const authorization = getAuthorization();
return fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json;charset=utf-8;',
'Content-Length': 256,
Authorization: authorization,
},
body: JSON.stringify({
to: phoneNumber.toString(),
appId: APP_ID,
templateId: TEMPLATE_ID,
datas: [code],
}),
})
.then(checkStatus)
.then(parseJSON);
};