• 网易短信接口集成 nodejs 版


    /*
    name:网易短信服务集成nodejs版;
    author:zeq
    time:20180607
    test:
    // checkValidCode('157****6954','284561').then(res=>console.log(res))
    // .catch(err=>console.log(err));
    // sendMsg(3992798,['157****6954'],['zeq']).then(res=>{
    //     console.log(JSON.stringify(res));
    // }).catch(e=>{
    //     console.log(JSON.stringify(e));
    // });
    // sendValidCode('15726656954').then(res=>{
    //     console.log(res);
    // }).catch(e=>{
    //     console.log(e);
    // });
    */
    
    var http =require('http');
    var qs = require('querystring');
    var crypto = require('crypto');
    var util = require('./util');
    
    
    const appKey = '';
    const appSecret = '';
    const noticeMsgUrl = 'https://api.netease.im/sms/sendtemplate.action';
    const validCodeUrl = 'https://api.netease.im/sms/sendcode.action';
    const checkValidCodeUrl = 'https://api.netease.im/sms/verifycode.action';
    
    
    module.exports = {
        sendValidCode,
        sendMsg,
        checkValidCode
    };
    
    function checkValidCode(mobile,code){
        return new Promise((resolve,reject)=>{
            let postData = qs.stringify({
                mobile,
                code
            });
            let options = packageHeaders('/sms/verifycode.action',postData);
            let req = http.request(options,res=>{
                var data = '';
                res.on('data',d=> data += d);
                res.on('end',()=>resolve(data));
                res.on('error',(err)=>reject(err));
            });
            req.on('error',(err)=>reject(err));
            req.write(postData);
            req.end();
        });
    }
    
    function sendMsg(templateid,mobiles,params){
        return new Promise((resolve,reject) => {
            mobiles = JSON.stringify(mobiles);
            params = JSON.stringify(params);
            let postData = `templateid=${templateid}&mobiles=${mobiles}&params=${params}`;
            let options = packageHeaders('/sms/sendtemplate.action',postData);
            let req = http.request(options,(res)=>{
                res.on('error',(res)=>{
                    reject(res);
                });
                let resData = '';
                res.on('data',d=>{
                    resData += d;
                });
                res.on('end',()=>{
                    resolve(resData);
                });
            });
            req.on('error',(err)=>{
                reject(err);
            });
            req.on('warning',(err)=>{
                reject(err);
            });
            req.write(postData);
            req.end();
        });
    }
    function sendValidCode(tel){
        return new Promise((resolve,reject) => {
            const data = qs.stringify({
                mobile:tel,
                codeLen:6
            });
            let options = packageHeaders('/sms/sendcode.action',data);
            let req = http.request(options,(res)=>{
                var resData = '';
                res.on('data',d=>{
                    resData += d;
                });
                res.on('end',()=>{
                    resolve(resData);
                });
            });
    
            req.on('error',(err)=>{
                console.log(err);
                reject(err);
            });
            req.write(data);
            req.end();
        })
    }
    /*sha1 加密 其实是在util里的*/
    function sha1(str){
        var sha1 = crypto.createHash('sha1');
        sha1.update(str);
        var secret = sha1.digest('hex');
        return secret;
    }
    
    /*请求header里的参数*/
    function packageHeaders(urlPath,postData){
        var Nonce = util.createRandomNum(10);
        var CurTime = Date.now();
        var CheckSum = sha1([appSecret,Nonce,CurTime].join(''));
        return {
            hostname:'api.netease.im',
            port:80,
            path:urlPath,
            method:'POST',
            headers:{
                appKey,
                Nonce,
                CurTime,
                CheckSum,
                'content-type': contentType,
                'content-length':Buffer.byteLength(postData)
            }
        }
    }

    网易短信网关nodejs项目集成,已经调通。如果有网易的IM帐号,就可以直接用,只需要修改appId,appSecret就可以了。需要的拿走,不谢。

  • 相关阅读:
    你真的了解HTML吗?–雅虎面试题
    移动端meta 解释
    PLSQL导入导出表的正确步骤
    Linux命令学习之xargs命令
    python搭建简易Web Server
    python小项目练习之转换像素图片为字符图
    向量空间搜索引擎基本理论
    Elasticsearch 常用基本查询
    Elasticsearch配置参数介绍
    JavaWeb温习之防止表单重复提交
  • 原文地址:https://www.cnblogs.com/zeq-jone/p/9152816.html
Copyright © 2020-2023  润新知