Nodjoy

只做了一点微小的工作


  • 首页

  • 技术

  • 笔记

  • 杂记

  • 分享

  • 归档

  • 关于

  • 站点监控

  • 服务监控

  • 搜索

  • GitHub

  • Instagram

  • Weibo

  • Email

  • Bilibili

  • Bento

  • 抖音

  • 打赏

  • 在线笔记

  • 在线文档

  • 域名证书管理

  • 云盘

  • Download Hub

  • DockerHub Mirror

  • Harbor

  • Quick Reference

  • Linux 命令查询

  • Mock 模拟数据

  • 文档预览服务
友链
  • milu杰克
  • 小菜
close

Node-Koa2之axios请求云之讯短信发送接口和soap请求WebService

时间: 2021-03-10   |   分类: 技术     |   阅读: 396 字 ~1分钟

axios请求云之讯短信接口实现短信发送

安装axios

    npm install axios --save

方法实现

static async sendMessage(ctx){
    let param = Math.random().toString().substr(2, 4);  //生成四位随机数
    let phone = '157********';  
    const url = 'https://open.ucpaas.com/ol/sms/sendsms';
    const axios = require('axios');
    let sendRe = await axios({
        method: 'post',
        url: url,
        data: {
                sid: "4rtw******", 
                token: "5fjg******", 
                appid: "8aj5******", 
                templateid: '2780*', 
                param: param, 
                mobile: phone, 
                //uid: config.uid 
        },
        headers: {
            "Content-Type": "application/json;charset=utf-8",
            "Accept": "application/json"
        }
    });
    if(sendRe.data.code == '000000'){
        ctx.json({
            code:11,
            message:"发送成功"
        });
    }else{
        ctx.json({
            code:22,
            message:"短信验证码发送失败,"+sendRe.data.code
        })
    }
}

soap请求WebService服务器数据

安装soap

    npm install soap --save

发起soap请求

async function GetProjectInfo () {
    var url = 'http://digitcode.yesno.com.cn/CCNOutService/OutDigitCodeService.asmx?wsdl';
    var args = { userID :'b6cf5d3*******1b3e',
      userPwd:'50e3*******066',
      ip:'101.*****',
      acCode:"20210310****",
      language:'1',
      channel:'X'
      };
    return new Promise(function (results) {
        const soap = require('soap');
        soap.createClient(url, function(err, client) {
          client.Get_AcCodeInfoInterface(args, function(err, result) {
            if (err) {
              results(err)
            }else {
              results(result)
            }  
          });
        });
      });
}
module.exports = {
 GetProjectInfo
};

获取到WebService返回的Xml数据

let resultProductInfo = await GetProjectInfo();
let xmlReply = resultProductInfo.reply;

解析Xml数据

安装xmlreader

    npm install xmlreader --save

xml数据转json对象实现

async function XmlToJson (xmlString) {
    return new Promise(function (result){
        var xmlreader = require("xmlreader");
        xmlreader.read(xmlString, function(errors, response){
            if(null !== errors ){
                result(errors);
            }
        let datas={};
        datas.reply = response.messageinfo.item.reply.text();
        datas.p_code = response.messageinfo.item.zcode.text();
        datas.cuanhuocode = response.messageinfo.item.cuanhuocode.text();
        datas.productcode = response.messageinfo.item.productcode.text();
        result(datas);
      });
    });
}
 
module.exports = {
    XmlToJson
};

喜欢这篇文章的话 打赏一下吧!

Wechat Alipay

#api# #axios# #soap# #node.js# #koa2#
Js数组操作
基于Koa框架的Lincms常用操作
  • 文章目录
  • 站点概览
Nodjoy

Nodjoy

Developer & Acceleration Engineer⏩

GitHub Instagram Weibo Email Bilibili Bento 抖音 打赏
52 日志
4 分类
64 标签
工具站
在线笔记 在线文档 域名证书管理 云盘 Download Hub DockerHub Mirror Harbor Quick Reference Linux 命令查询 Mock 模拟数据 文档预览服务
友情链接
  • milu杰克
  • 小菜
  • 安装axios
  • 方法实现
  • 安装soap
  • 发起soap请求
  • 获取到WebService返回的Xml数据
  • 解析Xml数据
    • 安装xmlreader
    • xml数据转json对象实现
京ICP备17004223号 © 2020 - NODJOY.
Powered by - HUGO
Theme by - NexT
访客量 -
0%