使用node.js生成后端可以拖动的拼图验证码

使用npm包canvas在后端绘制一张背景图片,然后在这张背景图片中截取一部分作为拼图,让用户移动拼图到正确的位置,同时将此拼图所截取的区域用一个空白的区域覆盖。

import express from 'express';

const Canvas = require('canvas');
const path = require('path');

const router = express.Router();

// 背景图片的宽可以传参设置,默认值是320 * 180,小拼图默认是60 * 45
router.get('/drag_captcha', (req, res) => {
  const { bgWidth: width } = req.query;
  const bgWidth = parseInt(width) || 320;
  const bgHeight = (width && parseInt(width * 180 / 320)) || 180;
  const dragPicWidth = 60;
  const dragPicHeight = 45;
  const index = Math.floor(Math.random() * 13);
  const positionX = Math.floor(Math.random() * (bgWidth - dragPicWidth - 10) + 11);  // 空白拼图的定位X
  const positionY = Math.floor(Math.random() * (bgHeight - dragPicHeight - 10) + 11);
  const Image = Canvas.Image;
  const bgCanvas = new Canvas(bgWidth, bgHeight);
  const dragCanvas = new Canvas(dragPicWidth, dragPicHeight);
  const background = bgCanvas.getContext('2d');
  const dragPic = dragCanvas.getContext('2d');

  const image = new Image();
  image.onload = () => {
    background.drawImage(image, 0, 0, 320, 180, 0, 0, bgWidth, bgHeight);
    dragPic.drawImage(bgCanvas, positionX, positionY, dragPicWidth, dragPicHeight, 0, 0,
    dragPicWidth, dragPicHeight);
    background.clearRect(positionX, positionY, dragPicWidth, dragPicHeight);
  };
  image.src = path.join(__dirname, `../app/assets/images/validate/bg-${index}.png`);
  if (req.session) {
    req.session.dragCaptcha = {
      positionX,
      positionY
    };
  }

  res.send({ bgCanvas: bgCanvas.toDataURL(), dragCanvas: dragCanvas.toDataURL() });
});

router.post('/check_captcha_position', (req, res) => {
  const { offsetLeft, offsetTop } = req.body;
  const { dragCaptcha = {} } = req.session;
  const range = 5;  // 误差范围
  if (!offsetLeft || !offsetTop) return;
  console.log(dragCaptcha.positionX, dragCaptcha.positionY);

  const d
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Node.js 编写后端接口一般需要以下步骤: 1. 安装 Node.js 和 npm Node.js 官网提供了下载链接和安装指南,npm 是 Node.js 自带的包管理工具,也可以使用 yarn 等替代。 2. 创建项目目录 在终端中进入需要创建项目的目录,使用 `mkdir` 命令创建项目文件夹,然后使用 `cd` 命令进入该文件夹。例如: ``` mkdir my-project cd my-project ``` 3. 初始化项目 在项目目录中使用 `npm init` 命令初始化项目,会生成一个 `package.json` 文件,其中包含了项目的基本信息和依赖。 ``` npm init ``` 4. 安装依赖 在项目目录中使用 `npm install` 命令安装需要的依赖,例如 Express、body-parser 等。 ``` npm install express body-parser --save ``` 5. 创建入口文件 在项目目录中创建入口文件 `index.js`,使用 require 导入需要的模块,创建 Express 应用程序,并添加路由和中间件。 ```javascript const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.json()); app.get('/', function (req, res) { res.send('Hello World!'); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); }); ``` 6. 启动应用程序 在终端中使用 `node` 命令启动应用程序,例如: ``` node index.js ``` 这样就可以在浏览器中访问 `http://localhost:3000`,看到输出的 `Hello World!`。 以上是一个简单的示例,实际应用中需要根据需求进行路由和中间件的编写,使用数据库等其他技术实现更复杂的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值