Commit 5c22fbdd authored by 任国军's avatar 任国军

add sendVerifyCode

parent b621cdd5
Pipeline #21860 passed with stage
in 21 seconds
...@@ -24,7 +24,7 @@ class UserController extends Controller { ...@@ -24,7 +24,7 @@ class UserController extends Controller {
const { ctx, service } = this; const { ctx, service } = this;
const queryParams = ctx.request.body; const queryParams = ctx.request.body;
if (ctx.isEmpty(queryParams.phone) || isNaN(queryParams.phone) || queryParams.phone.length !== 11) { if (ctx.isEmpty(queryParams.phone) || !ctx.helper.isPhoneNumber(queryParams.phone)) {
ctx.failed('手机号错误'); ctx.failed('手机号错误');
} }
......
...@@ -11,10 +11,53 @@ class UserService extends Service { ...@@ -11,10 +11,53 @@ class UserService extends Service {
async sendVerificationCode(input) { async sendVerificationCode(input) {
const { ctx } = this; const { ctx } = this;
const phone = input.phone; const phone = input.phone;
const APP_ID = '1C1wN39F4s61w62';
const APP_KEY = '6643BBA3-2663-4A69-BA4E-2711D6B0AED5';
// to do const redisCode = await ctx.app.memcache.get(`course_verify_code_${phone}`);
if (!ctx.isEmpty(redisCode)) {
ctx.failed('请勿频繁操作');
}
if (!ctx.helper.isPhoneNumber(phone)) {
ctx.failed('手机号不正确');
}
const code = await this.getRandomNumber(5);
const now = String(new Date().getTime() - 1000);
const ret = await ctx.helper.send_request('http://b.jianbing.com/messages/index', {
app_id: APP_ID,
timestamp: now,
sign: ctx.helper.md5(`${now}&${APP_KEY}`),
params: {
phone,
content: `您的验证码是:${code},您正在登录趣选课,验证码5分内有效,请勿泄露给他人。`,
},
}, {
method: 'POST',
dataType: 'json',
contentType: 'json',
timeout: [ 5000, 60000 ],
});
if (ret.status === 200) {
await ctx.app.memcache.set(`course_verify_code_${phone}`, code, 300);
} else {
ctx.logger.info('course_send_verify_code_error: ' + JSON.stringify(ret.data));
ctx.failed('发送验证码失败');
}
return { result: true };
}
// 生成随机位数的数字串
async getRandomNumber(length) {
let ret = '';
for (let i = 0; i < length; i++) {
ret += Math.floor(Math.random() * 10);
}
const ret = { result: true };
return ret; return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment