var http = require('http');
var https = require("https"); //https
var crypto = require('crypto'); //加密
var fs = require('fs'); //文件操作
var express = require('express');
var iconv = require("iconv-lite");
var bodyParser = require("body-parser"); //post解析json
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
/**
* 微信语音转码并回发
**/
app.post('/download/media', function (request, response) {
if (request.body.access_token && request.body.media) {
downLoadMedia(request.body.access_token, request.body.media, function (data) {
if (data.length < 100) {
debug(data);
response.end(data);
} else {
var fileName = sha1(getTime() + Math.random());
//var fileName = "./demo.amr";
fs.writeFileSync("./voice/" + fileName + ".amr", data, "binary");
var ffmpeg = require('fluent-ffmpeg');
var command = ffmpeg("./voice/" + fileName + ".amr")
.save("./voice/" + fileName + ".mp3")
.on('error', function (err) {
debug("Convert Error!");
})
.on('end', function () {
debug("Converted...");
response.end("https://node.hamm.cn/voice/?id=" + fileName);
var url = "https://www.xxx.com/wechat/sendmsg/text?id=" + request.body.openid + "&msg=" + encodeURIComponent("https://node.hamm.cn/voice/?id=" + fileName);
https.get(url, function (ress) {
debug(ress.text);
}).on('error', function (e) {
debug(e.message);
});
});
debug("Converting...");
}
});
} else {
var msg = "Params Missing!";
debug(msg);
res.end(msg);
}
});
/**
*
**/
app.post('/download/url', function (req, res) {
if (req.body.fileurl) {
downLoadMediaWithUrl(req.body.fileurl, function (data) {
if (data.length === 0) {
var fileName = sha1(getTime() + Math.random());
//var fileName = "./demo.amr";
fs.writeFileSync("./voice/" + fileName + ".amr", data, "binary");
var ffmpeg = require('fluent-ffmpeg');
var command = ffmpeg("./voice/" + fileName + ".amr")
.save("./voice/" + fileName + ".mp3")
.on('error', function (err) {
debug("Convert Error!");
})
.on('end', function () {
debug("Converted...");
res.end("https://node.hamm.cn/voice/?id=" + fileName);
});
debug("Converting...");
}
});
} else {
var msg = "Params Missing!";
debug(msg);
res.end(msg);
}
});
app.get('/voice/', function (req, res) {
var param = req.query.id;
fs.readFile('./voice/' + param + ".mp3", function (err, data) {
if (err) {
debug("File not found!");
res.end("File not found!");
} else {
debug("File Got!");
res.end(data);
}
});
});
app.get('/*', function (req, res) {
var params = req.originalUrl;
res.send("<center><h1>808 Not Found</h1><hr>Nginx&Node</center>");
});
var server = app.listen(10001, function () {
//var host = server.address().address
//var port = server.address().port
debug("Server running...");
});
function downLoadMediaWithUrl(url, callback) {
https.get(url, function (res) {
var datas = "";
res.setEncoding("binary"); //一定要设置response的编码为binary
res.on('data', function (data) {
datas += data;
});
res.on("end", function () {
//var buff = Buffer.concat(datas, size);
//var result = iconv.decode(buff, "utf8"); //转码
//var result = buff.toString(); //不需要转编码,直接tostring
callback(datas);
});
}).on("error", function (err) {
debug("Request Error!");
});
}
function downLoadMedia(access_token, media, callback) {
var url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=" + access_token + "&media_id=" + media;
https.get(url, function (res) {
var datas = "";
res.setEncoding("binary"); //一定要设置response的编码为binary
res.on('data', function (data) {
datas += data;
});
res.on("end", function () {
//var buff = Buffer.concat(datas, size);
//var result = iconv.decode(buff, "utf8"); //转码
//var result = buff.toString(); //不需要转编码,直接tostring
callback(datas);
});
}).on("error", function (err) {
debug("Request Error!");
});
}
function debug(msg) {
console.log("Console\t" + getTime() + "\t" + msg);
}
function sha1(str) {
var sha1 = crypto.createHash("sha1"); //定义加密方式:md5不可逆,此处的md5可以换成任意hash加密的方法名称;
sha1.update(str);
var res = sha1.digest("hex"); //加密后的值d
return res;
}
function getTime() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
return hours + ":" + minutes + ":" + seconds;
}