腾讯云短信 nodejs 接入, 通过验证码修改手机示例

腾讯云短信 nodejs 接入, 通过验证码修改手机示例

参考:
腾讯云短信文档
国内短信快速入门
qcloudsms Node.js SDK
文档中心>短信>错误码

nodejs sdk 使用示例

const QcloudSms = require('qcloudsms_js');

const cfg = {
  appid: 1400123123, // SDK AppID 以1400开头
  appkey:  '...', // 短信应用 SDK AppKey
  templateId: 402790, // 短信模板 ID,需要在短信控制台中申请
  smsSign: '我的过去公众号', // NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台申请
}

// 简单封装一下, 向指定手机下发验证码
// sendCode('18212341234', 1234) // 发送短信
function sendCode (phone, code, time = 10) {
  phone = typeof(phone) === 'object' ? phone : [phone]
  const qcloudsms = QcloudSms(cfg.appid, cfg.appkey) // 实例化 QcloudSms
  const msender = qcloudsms.SmsMultiSender()
  msender.sendWithParam(86,
    phone, // 一个数组
    cfg.templateId, // 模版 id
    [code, time], // 正文中的参数值
    cfg.smsSign, // 签名 未提供或者为空时,会使用默认签名发送短信
    '', '',
    (err, res, resData) => { // 回调函数
      err && console.log('err: ', err)
      console.log('request data: ', res.req)
      console.log('response data: ', resData)
    })
}

请求及回调

这里的请求及回调与 sdk 中的是一样的。

参考:
文档中心>短信>API 文档>短信API>指定模板群发短信

URL 示例
POST https://yun.tim.qq.com/v5/tlssmssvr/sendmultisms2?sdkappid=xxxxx&random=xxxx

请求

{
  "ext": "",
  "extend": "",
  "params": [
    "验证码",
    "1234",
    "4"
  ],
  "sig": "...",
  "sign": "腾讯云",
  "tel": [
    {
      "mobile": "13788888888",
      "nationcode": "86"
    },
    {
      "mobile": "13788888889",
      "nationcode": "86"
    }
  ],
  "time": 1457336869,
  "tpl_id": 19
}

回调

{
  "result": 0,
  "errmsg": "OK",
  "ext": "",
  "detail": [
    {
      "errmsg": "OK",
      "fee": 1,
      "mobile": "13788888888",
      "nationcode": "86",
      "result": 0,
      "sid": "xxxxxxx"
    },
    {
      "errmsg": "OK",
      "fee": 1,
      "mobile": "13788888889",
      "nationcode": "86",
      "result": 0,
      "sid": "xxxxxxx"
    }
  ]
}

使用验证码修改手机

API 发送验证码

返回验证码及有效时间的加密为字符串.

query: {phone}
response: {token}
phoneCode({
  body: {},
  query: {
    phone: '18212341234',
  }
})
async function phoneCode (ctx) {
  let {phone} = ctx.query
  let code = '1234' // 生成验证码用于测试
  if(process.env.PRO_ENV === 'pro') { // 如果是正式环境, 修改验证码为随机
    code = String(Math.random()).slice(-4)
    sms.sendCode(phone, code) // 发送验证码
  }
  let token = jwt.sign(
    { // 加密的 json 信息
      phone,
      code,
      iat: Date.now() + 1000 * 60 * 10, // 过期时间设置为 10 分钟后
    },
    config.jwt.secret, // secret 为密匙
  )
  ctx.body = {token: token} // 返回加密后的内容
}

API 修改手机号

解密加密字符串, 判断是否与用户填写的对应.

body: {
  token,
  phone,
  code,
}
response: {...}
phoneChange({
  body: {
    token: '...',
    phone: '18212341234',
    code: '1111',
  },
  query: {},
})
async function phoneChange(ctx) {
  const userModel = models.user // 数据库中的 user 表
  const { token, phone, code } = ctx.body
  let decoded = jwt.decode(token, config.secret) // 使用 secret 密匙解密 token
  let { creator_id } = ctx.state || {}
  if(!creator_id) {return console.log('非法请求')}
  const apiUser = await userModel.findOne({ where: { id: creator_id }}) // 查询当前发起请求的用户
  if (apiUser && decoded) {
    if(decoded.iat < Date.now()) {return console.log('验证码已过期')}
    if(decoded.phone !== phone) {return console.log('接收验证码的手机号与要修改的手机号不匹配')}
    if(decoded.code !== code) {return console.log('验证码错误')}
    const bindUser = await userModel.findOne({where: {bind_phone: phone}, raw: true}) // 查询是否存在已绑定
    if(bindUser) {return console.log('该手机号已有绑定用户')}

    apiUser.bind_phone = phone // 修改为新手机并保存
    return ctx.body = await apiUser.save({ new: true })
  } else {
    return console.log('该用户不存在')
  }
}
posted @ 2019-08-23 17:33  程序媛李李李李蕾  阅读(1383)  评论(0编辑  收藏  举报