zhaobinglong / myBlog

https://zhaobinglong.github.io/myBlog/
MIT License
7 stars 0 forks source link

Nodejs对接腾讯云短信服务 #154

Open zhaobinglong opened 3 years ago

zhaobinglong commented 3 years ago

报错记录:signature format is incorrect or signature is not approved

// 解决方案
const params = {
    SmsSdkAppId: "1400787878",
    SignName: "xxx",  /* 短信签名, 必须写通过审核的签名,不是创建的应用名 */
    PhoneNumberSet: ["+8613711112222"],
    TemplateId: "449739",
    TemplateParamSet: ["666"], // TemplateParamSet中的内容必须是字符串,其他类型会报错
}

image

zhaobinglong commented 3 years ago

封装通用API下发短信

router.post('/send/:app', function(req, res, next) {
    const app = config.get(req.params.app)
    const SmsClient = tencentcloud.sms.v20210111.Client;
    const clientConfig = {
      credential: {
        secretId: app.sms.secretId, 
        secretKey: app.sms.secretKey // 绝密信息
      },
      region: "ap-beijing",
      profile: {
        httpProfile: {
          endpoint: "sms.tencentcloudapi.com",
        },
      },
    };

    const client = new SmsClient(clientConfig);
    const params = {
        "PhoneNumberSet": req.body.PhoneNumberSet, // 数组,元素是手机号码,前面要加86
        "SmsSdkAppId": app.sms.SmsSdkAppId,
        "SignName": app.sms.SignName,
        "TemplateId": req.body.TemplateId, // 短信模板id
        "TemplateParamSet": req.body.TemplateParamSet // 数组
    };
    client.SendSms(params).then(
      (data) => {
        res.send(data)
      },
      (err) => {
        res.send(err)
      }
    );
});