egg(99)--egg之注册 发送手机短信验证码 以及验证当前ip、手机号发送验证码的次数

注册流程

egg(99)--egg之注册 发送手机短信验证码 以及验证当前ip、手机号发送验证码的次数

model 用户注册临时表

app/model/user_temp.js
module.exports = app => {
    const mongoose = app.mongoose;
    const Schema = mongoose.Schema;
    var d = new Date();
    const UserTempSchema = new Schema({
        phone: {
            type: Number
        },
        send_count: {
            type: Number
        },
        sign: {
            type: String
        },
        ip: {
            type: String,
        },
        add_time: {
            type: Number,
            default: d.getTime()
        },
    });

    return mongoose.model('UserTemp', UserTempSchema, 'user_temp');
}

router.js

router.get('/register/registerStep1', initMiddleware, controller.default.pass.registerStep1);
    router.get('/verify', initMiddleware, controller.default.base.verify);

controller

验证码

app/controller/default/base.js
'use strict';

const Controller = require('egg').Controller;

class BaseController extends Controller {

    //验证码
    async verify() {


        var captcha = await this.service.tools.captcha(120, 50); //服务里面的方法

        this.ctx.session.identify_code = captcha.text; /*验证码的信息*/
        console.log(captcha.text)

        this.ctx.response.type = 'image/svg+xml'; /*指定返回的类型*/

        this.ctx.body = captcha.data; /*给页面返回一张图片*/

    }
}

module.exports = BaseController;

验证码验证码是否正确

验证当前ip、手机号发送验证码的次数

app/controller/default/pass.js
async sendCode() {

        var phone = this.ctx.request.query.phone;
        var identify_code = this.ctx.request.query.identify_code; //用户输入的验证码

        if (identify_code != this.ctx.session.identify_code) {

            this.ctx.body = {
                success: false,
                msg: '输入的图形验证码不正确'
            }
        } else {

            //判断手机格式是否合法
            var reg = /^[\d]{11}$/;
            if (!reg.test(phone)) {
                this.ctx.body = {
                    success: false,
                    msg: '手机号不合法'
                }
            } else {

                var add_day = await this.service.tools.getDay(); //年月日    
                var sign = await this.service.tools.md5(phone + add_day); //签名
                var ip = this.ctx.request.ip.replace(/::ffff:/, ''); //获取客户端ip         
                var phone_code = await this.service.tools.getRandomNum(); //发送短信的随机码    

                var userTempResult = await this.ctx.model.UserTemp.find({ "sign": sign, add_day: add_day });

                //1个ip 一天只能发20个手机号
                var ipCount = await this.ctx.model.UserTemp.find({ "ip": ip, add_day: add_day }).count();
                console.log(ip)

                if (userTempResult.length > 0) {
                    if (userTempResult[0].send_count < 6 && ipCount < 10) { //执行发送
                        var send_count = userTempResult[0].send_count + 1;
                        await this.ctx.model.UserTemp.updateOne({ "_id": userTempResult[0]._id }, { "send_count": send_count });

                        //发送短信
                        // this.service.sendCode.send(phone,'随机验证码')
                        this.ctx.session.phone_code = phone_code;
                        console.log('---------------------------------')
                        console.log(phone_code, ipCount);



                        this.ctx.body = {
                            success: true,
                            msg: '短信发送成功',
                            sign: sign
                        }



                    } else {
                        this.ctx.body = { "success": false, msg: '当前手机号码发送次数达到上限,明天重试' };

                    }

                } else {
                    var userTmep = new this.ctx.model.UserTemp({
                        phone,
                        add_day,
                        sign,
                        ip,
                        send_count: 1
                    });
                    userTmep.save();

                    //发送短信
                    // this.service.sendCode.send(phone,'随机验证码')  
                    this.ctx.session.phone_code = phone_code;
                    this.ctx.body = {
                        success: true,
                        msg: '短信发送成功',
                        sign: sign
                    }

                }

            }

        }



    }

service

app/service/tools.js
async getTime() {
        var d = new Date();
        return d.getTime();
    }
    
    async getDay() {
        var day = sd.format(new Date(), 'YYYYMMDD');
        return day;
    }
    
    async getRandomNum() {
        var random_str = '';
        for (var i = 0; i < 4; i++) {
            random_str += Math.floor(Math.random() * 10);
        }
        return random_str;
    }

效果

egg(99)--egg之注册 发送手机短信验证码 以及验证当前ip、手机号发送验证码的次数

egg(99)--egg之注册 发送手机短信验证码 以及验证当前ip、手机号发送验证码的次数

相关推荐