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

腾讯云信息查看

登录 - 腾讯云

相关推荐
FungLeo1 小时前
Node 后端实战 · 列表查询到底怎么写?一个通用 DSL 封装,过滤分页排序一次搞定
数据库·node.js·serverless·dsl 封装·通用列表查询
_codemonster3 小时前
npm run dev 是在开发模式运行,怎么在生产环境运行
前端·npm·node.js
渔夫正在掘金8 小时前
Cordis 插件热插拔能力深度解析
后端·node.js
小新讲网安8 小时前
云安全攻防实战:AWS与阿里云渗透测试全流程指南
安全·web安全·网络安全·阿里云·自动化·云计算·aws
行业研究员11 小时前
腾讯云ADP:智能体平台封神榜
人工智能·microsoft·腾讯云·智能体·智能体平台·腾讯云adp
嘟嘟071714 小时前
NestJS 入门:从 NestFactory 入口到 Module/Controller/Service 模块化结构一次讲清
typescript·node.js·nestjs
breeze jiang15 小时前
NestJS MVC 实战:用 hello 项目 7 个文件讲清分层、装饰器、依赖注入与错误处理
node.js·mvc·js
m0_380743871 天前
从零调用 Claude 教程
开发语言·python·node.js
星野川崎2061 天前
电商多店运维:云机长期挂机频繁掉线、账号无故风控原因剖析与解决方案
大数据·运维·云计算·电商
烬羽1 天前
从 nest new 到 Hello World:一个最小 NestJS 项目,讲透工厂 + 装饰器 + 模块化
设计模式·node.js·nestjs