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

腾讯云信息查看

登录 - 腾讯云

相关推荐
AKAMAI5 小时前
跳过复杂环节:Akamai应用平台让Kubernetes生产就绪——现已正式发布
人工智能·云原生·云计算
天翼云开发者社区7 小时前
Rust 中的 Tokio 线程同步机制
云计算
天翼云开发者社区9 小时前
亮相2025年服贸会,天翼云打造高质量算力服务新生态!
云计算·天翼云
汤姆Tom10 小时前
Node.js 版本管理、NPM 命令、与 NVM 完全指南
前端·npm·node.js
RoyLin1 天前
TypeScript设计模式:原型模式
前端·后端·node.js
RoyLin2 天前
TypeScript设计模式:单例模式
前端·后端·node.js
RoyLin2 天前
TypeScript设计模式:工厂方法模式
前端·后端·node.js
RoyLin2 天前
TypeScript设计模式:模板方法模式
前端·后端·node.js
RoyLin2 天前
TypeScript设计模式:适配器模式
前端·后端·node.js
容器魔方2 天前
Bloomberg 正式加入 Karmada 用户组!
云原生·容器·云计算