JS制作图形验证码实现代码

<p style="text-align: left">第一步我们来到要展示验证码的页面,当我们按下营业执照的时候让其,弹出一个弹框,弹框的上面就是验证码,如图一所示:

这里写图片描述

弹框的样式如图二所示:

这里写图片描述

我们要对验证码的值进行校验,判断验证码是否输入正确,当输入不正确的时候,我们提示错误信息,提示信息如图三所示:

这里写图片描述

如果页面了验证正确,这不会提示错误信息并且调到我们的目标页面,如图四所示:

这里写图片描述

路由层描述

rush:js;"> /** 供货商店铺-店铺简介 */ //1-在路由层进行设置,页面跳转到根目录下/buyer/vshop/info.ejs页面进行跳转,然后在回调函数中进行接口的调用 router.get('/buyer/vshop/info',function(req,res,next) { //2-调用接口获取数据 async.parallel({ verifycode :function(fn){ //3-调用接口获取本机的IP地址 var args= { userIp: tools.getClientIp(req) };
      //4-<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>接口verifyCodeHelper的createVcode<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a><a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>captcha与 csnonce的数据,他们的值都是腾讯接口verifyCodeHelper返回的数据

      verifyCodeHelper.createVcode(args,function(err,result){
        if (err) {
          console.log(err);
        }
        else{
          fn(null,{
            captcha: result.data.queryUrl,csnonce: result.csnonce || 0
          })
        }
      });
    }
  },result){
    if (err) {
      res.end(err);
    }else{
      result.getDetail= res.shopInfo;
      var fxzStatus = res.fxzStatus || {};

    //5-进行根目录下buyer/vshop/describe<a href="https://www.jb51.cc/tag/yemian/" target="_blank" class="keywords">页面</a>的渲染,传递相关参数到EJS<a href="https://www.jb51.cc/tag/yemian/" target="_blank" class="keywords">页面</a>

      res.render("buyer/vshop/describe",{  
        title: srcBizType==1 ? result.getDetail.name : "店铺简介",pageName:"vshopInfo",captcha:result.verifycode.captcha,//验证码
        isTencent: 1,csnonce:result.verifycode.csnonce
      });
    }
  });
});

EJS层描述

rush:xhtml;"> <%- include ../../header %> //1-获取路由传递的 csnonce //3-引入相关JS文件 <%- include ../../footer %>

JS层描述

rush:js;"> /** 这是一个公用文件,vshop首页&vshop全部商品共用 */ //1-引入页面所需的各个模块 define(function (require,exports,module) { var $ = require("./top-suction-cmd"),myDialog = require("lib_cmd/myDialog-cmd"),main = require("js_cmd/main-cmd"),DataLoader = require("./tool-cmd"),fxzTool = require("./fxzTool-cmd"),Vue = require("lib_cmd/vue-cmd"),widget_navBtns = require("js_cmd/navBtns-cmd"),$eles = {},eles = {};

//2-Vue实例化
var vm = new Vue({
data: {},methods: {}
});

//3- 页面初始化函数

function initPage() {
  vm.$mount('#vue_hooker');
  window.scroll(0,0);
}

//4- 验证码相关处理函数
function license(){
$('.info4 .business-license').click(function (ev) { //弹层
$('.btn_mes_text').val("");
$('.arrow_mask').show();
$('.arrow').show();
$('#nav_omit').remove();
});
$('#btn_sku_dialog_close').click(function(ev){//弹窗
$('.arrow_mask').hide();
$('.arrow').hide();
$('.btn_mes_text').val("");
});

  $('.btn_mes_text').focus(function(){ //输入框获得焦点
    $('.btn_mes_text').val("");
    if($('.verification-code').length!=0){
      $('.verification-code').remove();
    }
  });
  $('.license-submit').click(function(){
    APP.verifyPicCode();
  });

}

//5- 验证码初始化<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>() TSOCapObj是腾讯接口里面的<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>

function verify_initPage(){
  $('#J_BtnVerifyPicCodeTencent').on('click',function () {
    TSOCapObj.refresh();
  });
  //初始化,参数传入<a href="https://www.jb51.cc/tag/xianshi/" target="_blank" class="keywords">显示</a>验证码的元素imgid
  TSOCapObj.init("J_BtnVerifyPicCodeTencent");
  //刷新拉取验证码<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>
  TSOCapObj.refresh();

  APP.verifyPicCode= function(fn) {
    //<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>输入
    var ans = $(".btn_mes_text").val();
    //验证答案,验证完成后会回调第二个参数传入的<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>
    TSOCapObj.verify(ans,function(ret_json){
      if (ret_json.errorCode!=0) {
        $(".btn_mes_text").val("");
        TSOCapObj.refresh();
        //tip("图形验证码输入<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>!");
        if($('.verification-code').length==0){
          $('.btn_mes').eq(0).append(" <div class='verification-code'>请输入正确的验证码</div>");
        }
        $('.btn_mes_text').blur(function(){
          $('.btn_mes_text').text("");
        });
      } else {
        window.location="/buyer/vshop/license?aid=" + APP.aid;
        fn(ret_json.ticket);
      }
    });
  }
}

 //6- <a href="https://www.jb51.cc/tag/yemian/" target="_blank" class="keywords">页面</a>初始化<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a>的执行
$(function () {
  initPage();
  license();
  verify_initPage();
});

});

CCS层描述

rush:css;"> @import url(common.css);

.div-section-info .info4{padding: 0}
.div-section-info .info4 li{
padding: 11px 14px;
line-height: 20px;
border-bottom: 1px solid #e6e6e6;

}
.div-section-info .info4 li a{
color: #333;

<a href="https://www.jb51.cc/tag/dis/" target="_blank" class="keywords">dis</a>play: block;

}
.div-section-info .info4 li a.tel{
background: url(/imgs/vshop/callAndMsg.png) no-repeat;
background-size: 20px 60px;
background-position: right 0;
}
.div-section-info .info4 li a.webim{
background: url(/imgs/vshop/callAndMsg.png) no-repeat;
background-size: 20px 60px;
background-position: right -30px;
}
.div-section-info .info4 li a label{
display: block;
float: right;
}

.div-section-info .info4 li a label span{
display: block;
width: 12px;
height: 12px;
border-right:1px solid #666;
border-bottom:1px solid #666;
-webkit-transform:rotate(-45deg);
float: right;
margin-top: 4px;

}
.arrow_mask {
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
z-index: 101;
display: none;
background: black;
opacity: 0.7;
}
.arrow{
width: 250px;
height: 226px;
background-color: #FFFFFF;
position: fixed;
top: 50%;
left: 50%;
z-index: 102;
-webkit-transform: translate(-50%,-50%);
-moz-transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
-o-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
border-radius: 10px;
display: none;
}
.arrow-mess li:nth-of-type(1){
text-align: center;
margin-bottom: 8px;
font-size: 14px;
color: #333333;
}
.arrow-mess li:nth-of-type(2){
text-align: center;
margin-bottom: 10px;
}
.arrow-mess li:nth-of-type(3) input{
width: 100%;
height: 30px;
border: 1px solid #979797;
text-align: center;
border-radius: 3px;
font-family: PingFangSC-Regular;
font-size: 16px;
color: #333333;
letter-spacing: 0px;
}
.btn_mes{
margin-bottom: 15px;
width: 100%;
padding-left: 48px;
padding-right: 48px;
position: relative;
}
.btn_mes2{
width: 100%;
padding-left: 70px;
padding-right: 70px;
/ position: relative; /
position: absolute;
bottom: 0px;
margin-bottom: 10px;
}

.license-submit{
width: 100%;
height: 44px;
background: #FF534C;
border: 1px solid #FF534C;
font-size: 14px;
border-radius: 4px;
color: #FFFFFF;
}

.sku-close {
background: url(/imgs/vshop/verification-code-del.png) no-repeat right;
-webkit-background-size: 25px auto;
display: inline-block;
width: 26px;
height: 26px;
position: absolute;
right: 0px;
top: 0px;
margin-right: -13px;
margin-top: -13px;
}
.verification-code{
z-index: 102;
color: #FFAA00;
position: absolute;
font-size: 12px;
top: 4px;
}
.verification-code-back{
width: 100%;
position: absolute;
height: 46px;
}
.arrow div:nth-of-type(1){
position: relative;
}
.arrow-mess{
margin-top: 52px;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...