zoukankan      html  css  js  c++  java
  • 网易短信接口集成 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就可以了。需要的拿走,不谢。

  • 相关阅读:
    接口默认值引发Qt调用DLL异常
    360杀毒引发的DLL调用异常
    VS2012基于QT5.1自定接口及插件并实现动态加载
    Qt调用C的DLL
    Qt动态多语言的实现(VS2012开发)
    Qt在VS2012中引用QtWidgets时报GLES2/gl2.h无法打开错误的解决办法
    php判断某字符串是否不以数字或其他特殊字符开头
    mysql的数据类型int、bigint、smallint 和 tinyint取值范围
    28个jQuery性能优化的建议
    主题:iframe高度的自适应
  • 原文地址:https://www.cnblogs.com/zeq-jone/p/9152816.html
Copyright © 2011-2022 走看看