Files
authentication/app/controller/SMSService.js

68 lines
2.2 KiB
JavaScript
Raw Normal View History

'use strict';
const Controller = require('../core/base_controller');
const fileName = "SMSService";
class SMSServiceController extends Controller {
// 向手机发送短信验证码
async sendSMS() {
const { ctx } = this;
try {
const { phone } = ctx.request.body;
const result = await ctx.service.sMSService.sendSMS(this.decryptRequest(phone));
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result.status == "200") {
this.setResponse(ctx, 200, '验证码已发送', "none");
} else if (result.status == "600") {
setTimeout(async () => {
await ctx.curl('http://127.0.0.1:' + ctx.app.server.address().port + '/sendSMS', {
dataType: 'json',
contentType: 'application/x-www-form-urlencoded',
method: 'POST',
timeout: 6000,
data: {
phone
},
});
}, 3000);
this.setResponse(ctx, 200, '正在发送验证码', "none");
} else {
this.setResponse(ctx, 404, '请重试', "none");
}
} catch (error) {
const blockName = fileName + "--" + "sendSMS";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 校验短信验证码
async checkCode() {
const { ctx } = this;
try {
const body = ctx.request.body;
const phone = this.decryptRequest(body.phone);
const code = this.decryptRequest(body.code);
const result = await ctx.service.sMSService.checkCode(phone, code);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 200, '验证通过', "none");
} else {
this.setResponse(ctx, 404, '验证失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "checkCode";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
}
module.exports = SMSServiceController;