NodeJS DEMO
会员通知示例
const http = require('http');
const crypto = require('crypto');
const querystring = require('querystring');
//参数
let url = "http://www.lokapi.cn/smsUTF8.aspx"
let rece = "json"
let username = "17712345678"
let password = "******"
let token = "cae46b2e"
let param = "17712345678|张三|2547|2020-06-22"
let templateid = "951A7DBD"
let passwd = md5(password);
let timestamp = Date.now();
let body = "action=sendtemplate&username=" + username + "&password=" + passwd + "&token=" + token + "×tamp=" + timestamp;
let sign = md5(body);
// 用于请求的选项
let contents = querystring.stringify({
"action": "sendtemplate",
"username": username,
"password": passwd,
"token": token,
"timestamp": timestamp,
"sign": sign,
"rece": rece,
"templateid": templateid,
"param": param
});
let options = {
host: 'www.lokapi.cn',
path: '/smsUTF8.aspx',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': contents.length
}
}
let req = http.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (data) {
console.log("data:", data); //一段html代码
});
});
req.write(contents);
req.end;
function md5(data) {
// 以md5的格式创建一个哈希值
let hash = crypto.createHash('md5');
return hash.update(data).digest('hex').toUpperCase();
}
彩信示例
const http = require('http');
const crypto = require('crypto');
const querystring = require('querystring');
const fs = require('fs');
const encodingConvert = require('encoding');
//参数
let url = "http://www.lokapi.cn/smsUTF8.aspx"
let rece = "json"
let username = "******"
let password = "123456"
let token = "333293sd"
let passwd = md5(password);
let timestamp = Date.now();
let body = "action=sendimagetext&username=" + username + "&password=" + passwd + "&token=" + token + "×tamp=" + timestamp;
let sign = md5(body);
//发送帧
//文字
let content = '祝你生日快乐';
let buff = Buffer.from(content, 'utf8');
let resultBuffer = encodingConvert.convert(buff, "GBK", "UTF8");
let base64Content = resultBuffer.toString('base64');
//图片
let path = 'D:\\我的文档\\Pictures\\d2e10cb3ac49dc63d013cb63ab6ca7cd.jpg';
let ext = 'jpg';//图片后缀
let base64str = base64_encode(path);
let message = 'txt|' + base64Content + ',' + ext + '|' + base64str + ';';
message = message.replace('%', '%25');
message = message.replace('&', '%26');
message = message.replace('+', '%2B');
// 用于请求的选项
let contents = querystring.stringify({
"action": "sendimagetext",
"username": username,
"password": passwd,
"token": token,
"timestamp": timestamp,
"sign": sign,
"rece": rece,
"mobile": '17712345678',
"title": '祝福短信',
'message': message
});
let options = {
host: 'localhost',
port: 59414,
path: '/smsUTF8.aspx',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': contents.length
}
}
let req = http.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (data) {
console.log("data:", data); //一段html代码
});
});
req.write(contents);
req.end;
function md5(data) {
// 以md5的格式创建一个哈希值
let hash = crypto.createHash('md5');
return hash.update(data).digest('hex').toUpperCase();
}
function base64_encode(file) {
var bitmap = fs.readFileSync(file);
return Buffer.from(bitmap, 'utf8').toString('base64');
}