Centos7

CentOS7安装Python3的方法CentOS7搭建L2TP服务端一键脚本CentOS7使用CertBot工具获取LetSencrypt泛域名HTTPS证书

Git

GIT删除仓库的历史提交记录脚本示例GIT进行代码量统计和贡献统计

HTTP

HTTP关于HEADER的详细解释HTTP协议对URI长度POST数据长度及COOKIE长度限制说明HTTP关于Code状态码的详细解释

JavaScript

JavaScript下的RSA对称加密DEMOJavaScript解析并遍历Excel示例代码JavaScript调用语音合成Speech Synthesis API使用ElementUI配合API数据快速渲染合并表格

Linux

Linux 信号说明列表说明Linux利用CronTab定时执行ThinkPHP命令行模式

MySQL

MySQL字符串截取MySQL腾讯云数据库导出备份到本地服务器导入MySQL设计与使用规范MySQL在5.7及以上版本中的ONLY_FULL_GROUP_BY问题处理方案

Nginx

Nginx反向代理NodeJS实现WSS协议Nginx反向代理中的Host参数传递遇到的坑Nginx配置HTTPS与HTTP2.0Nginx负载均衡的lnmp环境初始化脚本Nginx图片递归代理服务器DEMO

NodeJS

NodeJS一些冷门但实用的npm包NodeJS使用Request模块实现CURLNodeJS使用express模块无法收到phpCurl的值NodeJS使用jimp和qrcode-reader识别解析二维码NodeJS实现UDP数据报套接字通讯NodeJS实现Websocket聊天室NodeJS实现串口通讯简单例程NodeJS实现微信协议登录NodeJS实现超级方便的Git自动发布WebhookNodeJS控制台简单表格打印NodeJS转码amr到mp3NWJS配置文件

Other

C#串口通信扫码枪数据读取支付宝Schema协议列表默克尔树基础算法示例博客的升级历程

PHP

PHP下RSA对称加密超长字符串分段加密DEMOPHP下phpMyAdmin数据字典美化代码PHP下的RSA对称加解密-根证书自签与签发子证书PHP使用root权限执行系统命令和切换到www用户PHP使用独占文件指针实现阻塞少量并发PHP多进程同时处理任务示例DEMO代码PHP大文件视频上传WebUploaderPHP实现Web方式发起一个超时任务的处理请求PHP实现二叉查找树的简单代码实现PHP实现微博登录简单代码PHP微信JSSDK开发DEMOPHP的坑XML转JSONPHP简单实现QQ登录代码PHP获取URL HTTP_HOST和SERVER_NAMEPHP获取上周、本周、上月、本月、本季度、上季度时间方法PHP获取真实IP之IP代理-IP伪造-真实IPPHP解密微信小程序手机号简单DEMOPHP高精度计算常用代码

Python

Python3使用Pool进程池实现多进程并发Python3使用jieba分词并生成WordCloud词云图Python3使用request进行CURL操作Python3实现WebhookPython3使用Selenium进行自动化测试手册

Wechat

微信公众号php环境无法获取POST值微信公众号网页跳转避免出现导航栏的小技巧
NodeJS转码amr到mp3
手机阅读

扫描二维码阅读文章
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;
}