12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- const express = require('express')
- const router = express.Router()
- const SMSClient = require('@alicloud/sms-sdk')
- const config = require('./config')
- // ACCESS_KEY_ID/ACCESS_KEY_SECRET 根据实际申请的账号信息进行替换
- const accessKeyId = config.accessKeyId
- const secretAccessKey = config.secretAccessKey
- const smsClient = new SMSClient({accessKeyId, secretAccessKey})
- const main = async (number, code) => {
- let data = await smsClient.sendSMS({
- PhoneNumbers: number,
- SignName: '阿里云短信测试专用',
- TemplateCode: 'SMS_133180335',
- TemplateParam: '{"code":"' + code + '"}'
- })
- return data
- }
- router.post('/', async (req, res) => {
- console.log(req.body.number)
- let code = Math.floor(Math.random() * 99999)
- req.session.code = code
- console.log(req.session.code)
- if (/^1[3|4|5|8][0-9]\d{4,8}$/.test(req.body.number)) {
- try{
- let data = await main(req.body.number, code)
- if (data.Code === 'OK') {
- res.status(201).send({
- code: 'ok'
- })
- } else {
- res.status(202).send({
- err: '服务器繁忙,请重新再试'
- })
- }
- } catch (e) {
- if (e.code == "isv.BUSINESS_LIMIT_CONTROL") {
- res.status(202).send({
- err: '你号码已经限流,请稍后再试'
- })
- }
- }
- } else {
- res.status(202).send({
- err: '请检查手机格式是否正确'
- })
- }
- })
- module.exports = router
|