PHP code example of yzh52521 / tp-captcha-grid

1. Go to this page and download the library: Download yzh52521/tp-captcha-grid library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yzh52521 / tp-captcha-grid example snippets


return [
    //生成验证码图片配置
    'image' => [
        //验证码图片路径
        'path' => env('GRID_CAPTCHA_IMAGE_PATH', public_path('storage\gridcaptcha\image')),
        //从验证码图片路径中获取的文件后缀名
        'suffix' => env('GRID_CAPTCHA_IMAGE_SUFFIX', 'jpg'),
        //生成验证码质量
        'quality' => env('GRID_CAPTCHA_IMAGE_QUALITY', 70),
        //生产验证码宽
        'wide' => env('GRID_CAPTCHA_IMAGE_WIDE', 300),
        //生产验证码高
        'high' => env('GRID_CAPTCHA_IMAGE_HIGH', 300),
    ],
    //验证码配置
    'captcha' => [
        //生成的验证码过期时间 单位秒
        'validity' => env('GRID_CAPTCHA_IMAGE_VALIDITY', 180),
        //验证码缓存的key
        'cache_key' => env('GRID_CAPTCHA_IMAGE_CACHE_KEY', 'grid_captcha'),
        //验证码生成的key长度
        'key_length' => env('GRID_CAPTCHA_IMAGE_KEY_LENGTH', 64),
        //自定义效验验证码key字段
        'key_string' => env('GRID_CAPTCHA_IMAGE_KEY_STRING', 'captcha_key'),
        //自定义效验验证码code字段
        'code_string' => env('GRID_CAPTCHA_IMAGE_CODE_STRING', 'captcha_code'),
    ],
];





namespace app\controller;


class Test
{
    /**
     * 辅助函数生成验证码
     * @return array
     */
    public function helpers()
    {
        return grid_captcha([
            'mobile' => '100xxxxx121'
        ]);
    }

    /**
     * 门面方式生成验证码
     * @return array
     */
    public function facade()
    {
        return \yzh52521\captcha\facade\GridCaptcha::get([
            'mobile' => '100xxxxx121'
        ]);
    }

    /**
     * 对象方式生成验证码
     * @return array
     */
    public function object()
    {
        $captcha = new \yzh52521\captcha\GridCaptcha();
        return $captcha->get([
            'mobile' => '100xxxxx121'
        ]);
    }
}







namespace app\controller;


use think\Request;

class Test 
{

    /**
     * 辅助函数方式效验
     * @param Request $request
     * @return array|false|\think\response\Json
     */
    public function helpersCheck(Request $request)
    {
        /**
         * 传参效验
         */
        if ($captcha_data = grid_captcha()->check('Qh8kHYF4C....', '1540') === false) {
            return json(['message' => '验证码错误', 'code' => 401]);
        }

        /**
         * 传递 Request 对象效验
         */
        if ($captcha_data = grid_captcha()->checkRequest($request)) {
            return json(['message' => '验证码错误', 'code' => 401]);
        }

        return $captcha_data;
    }
    
    /**
     * 门面方式效验
     * @param Request $request
     * @return array|false|\think\response\Json
     */
    public function facadeCheck(Request $request)
    {
        /**
         * 传参效验
         */
        if ($captcha_data = \yzh52521\captcha\facade\GridCaptcha::check('Qh8kHYF4C....', '1540') === false) {
            return json(['message' => '验证码错误', 'code' => 401]);
        }

        /**
         * 传递 Request 对象效验
         */
        if ($captcha_data = \yzh52521\captcha\facade\GridCaptcha::checkRequest($request)) {
            return json(['message' => '验证码错误', 'code' => 401]);
        }

        return $captcha_data;
    }
    
    /**
     * 对象方式效验
     * @param Request $request
     * @return array|false|\think\response\Json
     */
    public function objectCheck(Request $request)
    {
        $captcha = new \yzh52521\captcha\GridCaptcha();
        /**
         * 传参效验
         */
        if ($captcha_data = $captcha->check('Qh8kHYF4C....', '1540') === false) {
            return json(['message' => '验证码错误', 'code' => 401]);
        }

        /**
         * 传递 Request 对象效验
         */
        if ($captcha_data = $captcha->checkRequest($request)) {
            return json(['message' => '验证码错误', 'code' => 401]);
        }

        return $captcha_data;
    }
}

    //效验完成正确后 您可以进行业务逻辑处理,比如可以获取到上方设置在验证码中的数据 如:上方设置的是手机号,您这里可以获取验证码中的手机号,当效验成功发送短信验证码等...



//一个图片目录对应一个提示
return [
 'grid-captcha' => [
    'banmaxian' => '斑马线',
    'gongjiaoche' => '公交车',
    'heiban' => '黑板',
    'honglvdeng' => '红绿灯',
    'hongzao' => '红枣',
    'houzi' => '猴子',
    'qianbi' => '铅笔',
    'shutiao' => '薯条',
    'xiaofangshuan' => '消防栓',
    'zhenglong' => '蒸笼',
    ]
];
shell
php think gridCaptcha:publish 


config/gridcaptcha.php

app/lang/zh-cn.php