获取验证码代码记录

写好一份获取验证码逻辑, 记录已做备用


	  // 1.定义一个随机字典
    public static final char[] chars = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
    										'A', 'B', 'C', 'D', 'E','F', 'G', 'H', 'I', 'J',
    										'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 
    										'U', 'V', 'W', 'X', 'Y', 'Z' };

     //2 定义生成随机数
    private static Random random = new Random();

     //获取随机六位数
    public static String getRandomString(){

         //字符串缓冲 ,进行拼接
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i <4;i++){

            //每次获取一个随机字符串
            sb.append(chars[random.nextInt(chars.length)]);
        }

        return sb.toString();
    }

     //获取一个颜色  分别用于前景色(文字的颜色)和背景色  
    public static Color getRandomColor(){
        return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
   }

    //获取随机颜色的反色 
    public static Color getReverColor(Color c){
        return new Color(255-c.getRed(), 255-c.getGreen(), 255-c.getBlue());        
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //随机字符串
        String randomString = getRandomString();

        //随机颜色 背景色
        Color c =getRandomColor();

        //反色 用于前景色
        Color reverse =getReverColor(c);

        //创建一个彩色的图片  根据BufferedImage(长度,宽度,生成的类型) 
        BufferedImage  bi =new BufferedImage(100, 30, BufferedImage.TYPE_INT_RGB);
        //获取绘图对象,画笔  
        Graphics g = bi.getGraphics();
        //设置字体     
        g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20));
        //设置字体的颜色
        g.setColor(c);
        //绘制背景 
        g.fillRect(0, 0, 100, 30);
        //设置前景色
        g.setColor(reverse);
        //绘制随机数字 
        g.drawString(randomString, 18, 20);

        //加噪点 
        for(int i=0 ,n=random.nextInt(100);i<n;i++){
            g.drawRect(random.nextInt(100), random.nextInt(30), 1, 1);
        }

      //输出图片
        ImageIO.write(bi,"jpg",response.getOutputStream());        
    }  


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个获取验证码的微信小程序代码示例: 1. 在 wxml 文件中,添加一个按钮和一个输入框,用于获取手机号码和发送验证码: ``` <view class="input-box"> <input type="number" placeholder="请输入手机号码" bindinput="bindPhone" /> <button type="default" bindtap="getCode" disabled="{{!canGetCode}}">{{codeText}}</button> </view> ``` 2. 在 js 文件中,定义一个变量用于记录手机号码和倒计时时间,以及一个函数用于获取验证码: ``` Page({ data: { phone: '', countdown: 60, canGetCode: false, codeText: '获取验证码' }, bindPhone: function(e) { this.setData({ phone: e.detail.value }) }, getCode: function() { var that = this; // 发送验证码的逻辑 // 判断手机号码是否合法 if (!(/^1[3456789]\d{9}$/.test(that.data.phone))) { wx.showToast({ title: '手机号码格式错误', icon: 'none' }) return; } // 发送验证码请求 wx.request({ url: 'http://api.example.com/sendCode', method: 'POST', data: { phone: that.data.phone }, success: function(res) { if (res.data.code === 0) { // 发送成功,开始倒计时 that.setData({ canGetCode: true, codeText: '重新发送(' + that.data.countdown + 's)' }) var timer = setInterval(function() { that.setData({ countdown: that.data.countdown - 1, codeText: '重新发送(' + that.data.countdown + 's)' }) if (that.data.countdown <= 0) { clearInterval(timer); that.setData({ countdown: 60, canGetCode: false, codeText: '获取验证码' }) } }, 1000) } else { wx.showToast({ title: res.data.msg, icon: 'none' }) } }, fail: function(res) { wx.showToast({ title: '发送失败', icon: 'none' }) } }) } }) ``` 3. 在 app.json 文件中,添加微信登录和手机号码授权: ``` { "pages": [ "pages/index/index" ], "permission": { "scope.userLocation": { "desc": "小程序需要获取您的地理位置信息" }, "scope.userInfo": { "desc": "小程序需要获取您的用户信息" }, "scope.phoneNumber": { "desc": "小程序需要获取您的手机号码" } }, "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "获取验证码", "navigationBarTextStyle": "black" }, "tabBar": { "list": [{ "pagePath": "pages/index/index", "text": "首页" }] }, "networkTimeout": { "request": 10000, "downloadFile": 10000 }, "debug": true, "sitemapLocation": "sitemap.json" } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值