cool框架 node.js 实现 腾讯云短信

1.现在想实现node 后端 实现腾讯云验证码的功能

去腾讯云上下载了node.js sdk 下载了以后 发现不能使用 问了问客服 说是 sdk有问题

2.qcloudsms_js 插件的使用 腾讯短信

(1). 安装

复制代码
npm i qcloudsms_js -save

(2)主题代码使用

复制代码
import { Config, Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import * as request from 'request';
const QCloudSms = require('qcloudsms_js');
/**
 * 抖音接口封装
 */
@Provide()
export class SendSesService extends BaseService {
  @Config('module.business')
  businessConfig;

  //发送验证码
  sendVerificationCode(query) {
    // 1323418546;
    const config = {
      appid: 'xxxxx', //sdk app 应用ID
      appkey: 'xxxx', // sdk 
    };
    const smsSign = 'xxx'; // 短信签名
    const templateId = 'xxxx'; // 短信模板ID
    const phoneNumber = [query.phone]; // 手机号数组
    var qcloudsms = QCloudSms(config.appid, config.appkey);

    const params = [];

    params[0] = Math.floor(Math.random() * 900000 + 100000).toString(); // 生成4位随机验证码
    // params[1]=

    // const paramsQuery = `您的动态验证码为:${params},为保障帐户安全,请勿向任何人提供此验证码。`;
    const ssender = qcloudsms.SmsSingleSender();
    return new Promise(function (resolve, reject) {
      // // 签名参数未提供或者为空时,会使用默认签名发送短信
      ssender.sendWithParam(
        '86',
        phoneNumber,
        templateId,
        params,
        smsSign,
        '',
        '',
        function (err, res, resData) {
          if (err) {
            reject(err); //发送失败
          } else {
            //所有短信全部认定发送成功
            // console.log('request data: ', res.req);
            console.log('response data: ', resData);
            //这里再把信息响应给客户端,方便逻辑判断
            resolve(resData);
          }
        }
      );
    });

    // return smsProvider.multiSend(
    //   params,
    //   phoneNumber,
    //   templateId,
    //   '',
    //   '',
    //   smsSign
    // );
  }
}

大概就是这个代码了

  1. 扩展

如果 不太清楚这个插件

qcloudsms_js - npm

腾讯云信息查看

登录 - 腾讯云

相关推荐
代码or搬砖1 天前
文件上传阿里云OSS以及本地图片服务器搭建
服务器·阿里云·云计算
许久'1 天前
环境搭建node.js gnvm
node.js
细节控菜鸡1 天前
Webpack 核心知识点详解:proxy、热更新、Loader与Plugin全解析
前端·webpack·node.js
2401_865854881 天前
腾讯云的游戏盾怎么样
游戏·云计算·腾讯云
spmcor1 天前
Nest.js 文件分片上传:当大文件来敲门,别慌,我们有“分尸”妙计!
node.js
熙客1 天前
阿里云负载均衡SLB的使用
网络·阿里云·云原生·云计算·负载均衡
武汉唯众智创1 天前
对接世界职业院校技能大赛标准,唯众打造高质量云计算实训室
云计算·云计算实训室·云计算实验室·世界职业院校技能大赛·世赛
悠闲蜗牛�1 天前
云智融合:人工智能与云计算融合实践指南
人工智能·云计算
搜移IT科技1 天前
2025上海国际数据中心及云计算产业展览会影响力如何?有啥亮点?
云计算
星空下的曙光2 天前
Node.js crypto模块所有 API 详解 + 常用 API + 使用场景
算法·node.js·哈希算法