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

nodejs yekong 170℃

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(ctx, tel) {
    let txt = createCode(4);
    var data2 = null;
    var data = qs.stringify({
        "Account": "账号",
        "Password": "密码",
        "Mobile": tel,
        "Content": "验证码" + txt + "【智慧杆】"
    });
    var config = {
        method: 'get',
        url: 'http://api.ksd106.com:8088/SMSSend.aspx?' + data,
    };
    await axios(config)
        .then(function (response) {
            console.log(JSON.stringify(response.data));
            // return response
            data2 = response.data
        })
        .catch(function (error) {
            console.log(error);
            data2 = error
        });
    if (data2.search("SUCCESS") != -1) {
        ctx.session.verifCode = txt;
        return {
            code: 1,
            msg: data2,
            data: data2
        }
    } else {
        return {
            code: 0,
            msg: data2,
            data: data2
        }
    }
    // return data2
}
// 校验短信
function getSmsCode(ctx) {
    if (ctx.request.body.code !== ctx.session.verifCode) {
        return {
            code: 0,
            msg: '验证码错误',
            data: ''
        }
    } else {
        return {
            code: 1,
            msg: '验证码正确',
            data: ''
        }
    }


}
喜欢 (0)