Files
authentication/app/service/SMSService.js

108 lines
4.2 KiB
JavaScript

'use strict';
const Service = require('../core/base_service');
const fileName = "SMSService";
const Dysmsapi20170525 = require('@alicloud/dysmsapi20170525');
const OpenApi = require('@alicloud/openapi-client');
const Util = require('@alicloud/tea-util');
class Client {
static createClient() {
let config = new OpenApi.Config({
// 凭证类型。
// type: 'access_key',
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
accessKeyId: "LTAI5tM3QC6kY4r23sWnsvuY",
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
accessKeySecret: "Lj9QTWVMJSTAffscjykhaKZt7RN0Xh",
// 访问的地域
// regionId: 'cn-beijing'
});
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
// 访问的地址
config.endpoint = `dysmsapi.aliyuncs.com`;
return new Dysmsapi20170525.default(config);
}
}
class SMSServiceService extends Service {
// 向手机发送短信验证码
async sendSMS(phone) {
const { ctx } = this;
try {
const code = this.generateCodeString();
const mark = await ctx.app.redis.get('db3').get(phone);
if(mark == null) await ctx.app.redis.get('db3').set(phone, JSON.stringify({ status: 0 }), 'EX', 600);
let client = Client.createClient();
// 创建请求对象并设置入参
let sendSmsRequest = new Dysmsapi20170525.SendSmsRequest({
phoneNumbers: phone,
// signName: "身份验证",
// templateCode: "SMS_480180061",
// signName: "登录验证",
// templateCode: "SMS_487315107",
signName: "华云智联",
templateCode: "SMS_487315108",
// signName: "北京华云智联",
// templateCode: "SMS_487350086",
// signName: "北京华云智联科技",
// templateCode: "SMS_487410093",
templateParam: `{\"code\":\"${code}\"}`,
});
// 创建运行时配置对象
let runtime = new Util.RuntimeOptions({});
runtime.readTimeout = 5000;
runtime.connectTimeout = 5000;
// 发起请求
const response = await client.sendSmsWithOptions(sendSmsRequest, runtime);
if(response.body.code == "OK") {
await ctx.app.redis.get('db3').set(code, phone.toString(), 'EX', 600);
return { status: 200, msg: "验证码发送成功" };
};
} catch (error) {
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
// console.log('1===========================================')
// console.log(error.message);
// 若捕捉到 ConnectTimeout 错误,则尝试重发
if(error.message.includes("ConnectTimeout")) return { status: 600, msg: "尝试重新核验" };
// console.log('2===========================================')
// 诊断地址
console.log(error);
// console.log('3===========================================')
// console.log(error.data);
// console.log('4===========================================')
// console.log(error.data["Recommend"]);
// Util.default.assertAsString(error.message);
const blockName = fileName + "--" + "sendSMS";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 校验短信验证码
async checkCode(phone, code) {
const { ctx } = this;
try {
const phoneResult = await ctx.app.redis.get('db3').get(phone);
if (phoneResult == null) await ctx.app.redis.get('db3').set(phone, JSON.stringify({ status: 0 }), 'EX', 600);
else if (JSON.parse(phoneResult).status == '1') return true;
const check = await ctx.app.redis.get('db3').get(code);
if (check == null || check != phone) return false;
await ctx.app.redis.get('db3').del(code);
let remainder = await ctx.app.redis.get('db3').ttl(phone);
if (remainder < 60) remainder = 600;
await ctx.app.redis.get('db3').set(phone, JSON.stringify({ status: 1 }), 'EX', remainder);
return true;
} catch (error) {
const blockName = fileName + "--" + "checkCode";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
}
module.exports = SMSServiceService;