TP6.0验证码接口

安装验证码功能组件(如果是官网下载的完整版框架,无需安装)

composer require topthink/think-captcha

创建验证码接口

   /**
     * 获取验证码
     * @return \think\response\Json|void
     */
    public function getcaptcha(){
        //验证码标识
        $uniqid = uniqid((string)mt_rand(100000, 999999));
        //返回数据 验证码图片路径、验证码标识
        $data = [
            'src' => 'http://adminapi.pyg.com'.captcha_src(),
            'uniqid' => $uniqid
        ];
        return success('success',200,$data);
    }

可修改组件源代码如下:
vendor/topthink/think-captcha/src/Captcha.php


/**
     * 创建验证码
     * @return array
     * @throws Exception
     */
    protected function generate(): array
    {
        $bag = '';

        if ($this->math) {
            $this->useZh  = false;
            $this->length = 5;

            $x   = random_int(10, 30);
            $y   = random_int(1, 9);
            $bag = "{$x} + {$y} = ";
            $key = $x + $y;
            $key .= '';
        } else {
            if ($this->useZh) {
                $characters = preg_split('/(?<!^)(?!$)/u', $this->zhSet);
            } else {
                $characters = str_split($this->codeSet);
            }

            for ($i = 0; $i < $this->length; $i++) {
                $bag .= $characters[rand(0, count($characters) - 1)];
            }

            $key = mb_strtolower($bag, 'UTF-8');
        }

        $hash = password_hash($key, PASSWORD_BCRYPT, ['cost' => 10]);

        $this->session->set('captcha', [
            'key' => $hash,
        ]);
        cache('captcha', [
            'key' => $hash,
        ]); //加上这行代码,便于后续校验验证码
        return [
            'value' => $bag,
            'key'   => $hash,
        ];
    }

    /**
     * 验证验证码是否正确
     * @access public
     * @param string $code 用户验证码
     * @return bool 用户验证码是否正确
     */
    public function check(string $code): bool
    {
        if (!cache('captcha')) {
            return false;
        }

        $key = cache('captcha')['key'];

        $code = mb_strtolower($code, 'UTF-8');

        $res = password_verify($code, $key);

        if ($res) {
            cache('captcha');
        }

        return $res;
    }
TP6.0中,事件机制是通过事件管理器(Event)来实现的。事件管理器是一个全局单例对象,可以在应用程序的任何地方使用。事件管理器主要负责事件的注册、触发和监听。 以下是TP6.0中事件执行的流程: 1. 注册事件 在应用程序的任何地方,都可以通过事件管理器注册事件。注册事件时,需要指定事件名称和事件处理函数。事件名称可以是任何字符串,事件处理函数可以是闭包、方法或者类静态方法。 例如: ```php // 注册事件 \think\facade\Event::listen('user_login', function ($user) { // 处理用户登录事件 ... }); ``` 2. 触发事件 当应用程序执行到某个位置时,可以通过事件管理器触发事件。触发事件时,需要指定事件名称和事件参数。事件参数可以是任何类型的数据。 例如: ```php // 触发事件 \think\facade\Event::trigger('user_login', $user); ``` 3. 执行事件处理函数 当事件被触发时,事件管理器会自动执行注册的事件处理函数。事件处理函数会按照注册的顺序执行,直到所有的事件处理函数都执行完毕。 例如: ```php // 执行事件处理函数 function handleUserLogin($user) { // 处理用户登录事件 ... } ``` 4. 返回事件结果 事件处理函数可以返回任何类型的数据,这些数据会被收集到一个数组中,作为最终的事件结果返回。事件结果可以被其他的事件处理函数使用。 例如: ```php // 返回事件结果 function handleUserLogin($user) { // 处理用户登录事件 ... return ['user' => $user, 'time' => time()]; } ``` 以上就是TP6.0中事件执行的流程。通过事件机制,我们可以实现应用程序的解耦和扩展,让应用程序更加灵活和可维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值