Service implement api or sdk of 3rd party.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.4 KiB

'use strict';
/**
* Express router of aliyun/
*/
const express = require('express');
const {default: common} = require('./common.js');
const {default: aliyunSmsClient} = require('./aliyunSmsClient');
const {default: defaultConfig, getCustomConfigs: getCustomConfigs} = require('./conf/config');
const router = express.Router();
let customConfig = null;
//发送注册验证码短信接口
/**
* @phoneNumber: 手机号码
* @codeNumber: 4位数字的验证码
* @action: ['register', 'login']
* @sign: 签名
*/
router.post('/sendverifycode', async (req, res) => {
let phoneNumber = req.body.phoneNumber;
let codeNumber = req.body.codeNumber;
let action = req.body.action ? req.body.action : 'register';
let sign = req.body.sign;
let data = {code: 0, message: ''};
6 months ago
//使用默认配置
let myConfig = defaultConfig;
//加载自定义配置
if (!customConfig) {
customConfig = await getCustomConfigs('./conf/custom_config.json');
}
//使用自定义配置
myConfig = customConfig;
if (!phoneNumber || !codeNumber || !sign) {
data.message = '参数不能为空';
}else if (common.isPhoneNumber(phoneNumber) == false) {
data.message = '手机号码格式错误';
}else if (common.isVerifyCode(codeNumber) == false) {
data.message = '验证码格式错误,必须是4位数的数字';
}else {
let paramsCheck = {};
for (const key in req.body) {
if (key != 'sign') {
paramsCheck[key] = req.body[key];
}
}
let mySign = common.sign(paramsCheck, myConfig.secret);
if (mySign.toLowerCase() != sign.toLowerCase()) {
data.message = `签名 ${sign} 不匹配,请确保密钥正确及签名方法跟文档一致`;
}
}
if (!data.message) {
let signName = 'Ta荐',
templateCode = action == 'register' ? 'SMS_465895580' : 'SMS_465915662',
templateParam = `{"code":"${codeNumber}"}`;
let sended = await aliyunSmsClient.send(myConfig, signName, templateCode, templateParam, phoneNumber);
if (sended) {
data.code = 1;
data.message = '验证码发送成功';
}else {
data.message = '验证码发送失败,请稍后重试';
}
}
return res.status(200).json(data);
});
exports.default = router;