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

  • 相关阅读:
    多种方式安装GitLabRunner
    rpm,docker,k8s三种方式安装部署GitLab服务
    利用curl命令访问Kubernetes API server
    在客户端电脑使用 kubectl 远程管理 Kubernetes
    利用 Nginx 反向代理搭建本地 yum 服务器
    Jenkins和Gitlab CI/CD自动更新k8s中pod使用的镜像说明
    1.在 Kubernetes 在快速安装 Harbor
    4.Gitlab CI 与 Kubernetes 的结合
    3.在 Kubernetes 上安装 Gitlab CI Runner
    2. 在 Kubernetes 上安装 Gitlab
  • 原文地址:https://www.cnblogs.com/zeq-jone/p/9152816.html
Copyright © 2020-2023  润新知