SSI ļʱ



node.js
SSI ļʱ
首页 > 网络编程 > JavaScript > node.js > node.js验证码登录

node.js(expree.js )模拟手机验证码登录功能

作者:鸥总

这篇文章主要介绍了node.js(expree.js )模拟手机验证码功能及登录功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
ƼѶơʱŻݻƷ12G99Ԫ/ꡢ24G768Ԫ/3꣬>>>9i0i.cn/qcloud

Ƽ㻹ԭ۹Ʒڰ0.8ʱ48GҵƷ2998Ԫ/3꣬>>>9i0i.cn/aliyun

dbconfig.js

const mysql = require('mysql')
module.exports = {
    // 数据库配置
    config: {
        host: 'localhost', // 连接地址
        port: '3306', //端口号
        user: 'root',  //用户名
        password: 'wei630229', //密码
        database: 'exapp2', //数据库名
    },
// 连接数据库,使用mysql的连接池连接方式
// 连接池对象
sqlConnect: function (sql, sqlArr, callBack) {
        var pool = mysql.createPool(this.config)
        pool.getConnection((err, conn) => {
            console.log('12345')
            if (err) {
                console.log('连接失败');
                return;
            }
            // 事件驱动回调
            conn.query(sql, sqlArr, callBack);
            //释放连接
            conn.release();
        })
    }
}
var dbCongif = require("../utils/dbconfig");
// 随机验证码
function rand(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}
// 声明验证码和手机号数组
ValidatePhoneCode = [];
// 判断该手机是否一定接收过该验证码
let sendCodeP = (phone) => {
  for (var item of ValidatePhoneCode) {
    console.log("item", item);
    if (phone == item.phone) {
      return true;
    }
  }
  return false;
};
//
let findCodeAndPhone = (phone, code) => {
  for (var item of ValidatePhoneCode) {
    if (phone == item.phone && code == item.code) {
      return "login";
    }
  }
  return "error";
};
// 验证码发送接口
sendCode = (req, res) => {
  // 判断该手机号是否一定接收过验证码
  let phone = req.query.phone;
  if (sendCodeP(phone)) {
    res.send({
      code: 400,
      msg: "已经发送过验证码,稍后再发",
    });
  }
  let codeMge = rand(1000, 9999);
  ValidatePhoneCode.push({
    phone: phone,
    code: codeMge,
  });
  res.send({
    code: 200,
    msg: "发送成功",
  });
  console.log("code", codeMge);
};
// 验证码登录
codePhoneLogin = (req, res) => {
  let { phone, code } = req.query;
  // 判断手机号是否发送过验证码
  if (sendCodeP(phone)) {
    // 验证码和手机号是否匹配
    let state = findCodeAndPhone(phone, code);
    if (state == "login") {
      // 登录成功
      res.send({
        code: "200",
        mgs: "登录成功",
      });
    } else if (state == "error") {
      res.send({
        code: "500",
        mgs: "登录失败",
      });
    }
  } else {
    res.send({
      code: "400",
      mgs: "未发送验证码",
    });
  }
};
module.exports = {
  sendCode, // 验证码接口
  codePhoneLogin, //  登录接口
};

测试验证码发送

测试登录

到此这篇关于node.js(expree.js )模拟手机验证码登录功能的文章就介绍到这了,更多相关node.js验证码登录内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文
SSI ļʱ
SSI ļʱ


http://www.vxiaotou.com