koa2实现发送短信和短信校验

nodejs yekong 594℃

##api

// 发送短信
smsApi.post('/sendSmsKsd', async ctx => {
    console.log(ctx.request.body)
    const tel = ctx.request.body.tel;
    var data2 = await sendSmsKsd(ctx, tel);
    ctx.json(data2);
});
// 查询短信
smsApi.post('/getSmsCode', async ctx => {
    console.log(ctx.request.body)
    console.log(ctx.request.body.code)
    console.log(ctx.session.verifCode)
    var data2 = await getSmsCode(ctx);
    ctx.json(data2);
});

##lib

//  发送短信
async function sendSmsKsd(tel) {
    var data2 = null;
    var code = smsCode.getCode(tel)
    console.log(code)
    var data = qs.stringify({
        "Account": "",
        "Password": "",
        "Mobile": tel,
        "Content": "验证码" + code
    });
    var config = {
        method: 'get',
        url: apiUrl + data,
    };
    await axios(config)
        .then(function (response) {
            data2 = response.data
        })
        .catch(function (error) {
            console.log(error);
            data2 = error
        });
    if (data2.search("SUCCESS") != -1) {
        return {
            code: 1,
            msg: data2,
            data: data2
        }
    } else {
        return {
            code: 0,
            msg: data2,
            data: data2
        }
    }
    // return data2
}

// 校验短信
function getSmsCode(ctx) {
    if (!smsCode.verifyCode(ctx.request.body.tel, ctx.request.body.code)) {
        return {
            code: 0,
            msg: '验证码错误',
            data: ''
        }
    }else{
        return {
            code: 1,
            msg: '验证码正确',
            data: ''
        }
    }
}
喜欢 (1)