.net 验证码 + TP6 通过接口调用实现

.net 项目git地址 注意 .net 版本 4.6.2

项目介绍:# LazyCaptcha v2(基于SkiaSharp) 仿EasyCaptchaSimpleCaptcha,基于.Net Standard 2.0 的图形验证码模块。 v2是指版本号>=2.0.0的版本,<2.0.0则称为v1。 v1基于ImageSharp,v2基于SkiaSharp 。SkiaSharp性能更好,但发布到linux时需要安装对应NativeAssets(ImageSharp则不需要)。 v1文档地址

本地需要先启动.net 验证码服务

  1. 需要先配置.net 服务 win系统 需要下载 指定版本
  2. 安装完毕后再 cmd 中输入命令验证是否安装成功 dotnet --version 会输出对应版本号
  3. 下载验证码项目 git clone https://github.com/pojianbing/LazyCaptcha.git
  4. 进入到指定目录 LazyCaptcha\Sample.NetCore 运行 dotnet restore - 这个命令会自动下载项目所需的所有依赖包(包括 Lazy.Captcha.Core 本身)。
  5. 在同一个命令行中输入dotnet run 程序开始编译并运行。会输出类似下面的命令info: Microsoft.Hosting.Lifetime[14] Now listening on: https://localhost:5001 info: Microsoft.Hosting.Lifetime[14] Now listening on: http://localhost:5000 info: Microsoft.Hosting.Lifetime[14] Application started. Press Ctrl+C to shut down. 6.在浏览器中查看验证码 输入地址 http://localhost:5000/captcha?id=123

TP6 后端代码:

css 复制代码
路由部分
// 验证码测试
Route::group(function () {
    Route::get('getCaptcha', 'v1.CaptchaController/getCaptcha');
    Route::get('image', 'v1.CaptchaController/captchaImage');
    Route::post('verify', 'v1.CaptchaController/verify');
});

控制器

php 复制代码
<?php

namespace app\databoardapi\controller\v1;

use think\facade\Log;
use think\facade\Request;

class CaptchaController
{
    private $netCaptchaUrl = 'http://localhost:5220/captcha';
    private $netValidateUrl = 'http://localhost:5220/captcha/validate';
    public function index()
    {
        return "index";
    }
    public function getCaptcha()
    {
        $captchaId = uniqid('cap_');

        // 手动拼接 URL,确保返回字符串
        $imageUrl = 'http://192.168.1.119/databoardapi/image?id=' . urlencode($captchaId);

        return json([
            'success' => true,
            'data' => [
                'captcha_id' => $captchaId,
                'captcha_image' => $imageUrl
            ]
        ]);
    }

    public function captchaImage()
    {
        $id = request()->param('id', '', 'trim');
        if (empty($id)) {
            return abort(400, 'Missing captcha id');
        }

        $url = $this->netCaptchaUrl . '?id=' . urlencode($id);

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $imageData = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($imageData === false || $httpCode !== 200) {
            Log::error('Failed to fetch captcha: ' . $url);
            return abort(500, '验证码服务错误');
        }

        return response($imageData, 200, ['Content-Type' => 'image/png']);
    }

    public function verify(\app\Request $request)
    {
        $data = $request->getMore([
            ['captcha_id', ''],
            ['captcha_code','']
        ]);
        $captchaId = $data['captcha_id'] ?? '';
        $userInput = $data['captcha_code'] ?? '';

        if (empty($captchaId) || empty($userInput)) {
            return json(['success' => false, 'message' => '参数缺失'], 400);
        }

        $url = $this->netValidateUrl . '?id=' . urlencode($captchaId) . '&code=' . urlencode($userInput);

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $result = curl_exec($ch);
        curl_close($ch);

        if ($result === false) {
            Log::error('验证请求失败: ' . $url);
            return json(['success' => false, 'message' => '验证码服务不可用'], 500);
        }

        $isValid = trim($result) === 'True' || trim($result) === 'true';

        return json([
            'success' => $isValid,
            'message' => $isValid ? '验证码正确' : '验证码错误'
        ]);
    }
}

在postman中访问 :http://127.0.0.1/databoardapi/getCaptcha 获取下面返回值

json 复制代码
{
    "success": true,
    "data": {
        "captcha_id": "cap_68f88ff18b15b",
        "captcha_image": "http://192.168.1.119/databoardapi/image?id=cap_68f88ff18b15b"
    }
}

验证接口 post 方式请求 http://127.0.0.1/databoardapi/verify

请求值:

json 复制代码
{
    "captcha_id":"cap_68f8955224443",
    "captcha_code":"象进地但"
}

返回值:

json 复制代码
{
    "success": true,
    "message": "验证码正确"
}
相关推荐
悟能不能悟3 分钟前
springboot controller返回的是HttpServletResponse成功返回excel文件流,失败就返回失败参数
spring boot·后端·excel
神奇小汤圆3 分钟前
面试官:如何在 Kafka 中实现延迟消息?
后端
请告诉他30 分钟前
【实战经验】Dell Inspiron 7560 升级 BIOS 支持 DDR4-2666 内存,解决 Spring Cloud 多模块开发内存瓶颈
后端·spring·spring cloud
我想问问天34 分钟前
【从0到1大模型应用开发实战】02|用 LangChain 和本地大模型,完成第一次“可控对话
后端·langchain·aigc
爱吃牛肉的大老虎1 小时前
Spring WebFlux与SpringMVC 对比讲解
java·后端·spring
老华带你飞1 小时前
房屋租赁管理系统|基于java+ vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
superman超哥2 小时前
仓颉性能优化秘籍:内联函数的优化策略与深度实践
开发语言·后端·性能优化·内联函数·仓颉编程语言·仓颉·仓颉语言
IT_陈寒2 小时前
Vue3性能优化实战:7个被低估的Composition API技巧让渲染提速40%
前端·人工智能·后端
superman超哥2 小时前
仓颉编译器优化揭秘:尾递归优化的原理与实践艺术
开发语言·后端·仓颉编程语言·仓颉·仓颉语言·尾递归·仓颉编译器