欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

thinkcmf php 前后端分离验证码实现流程

程序员文章站 2024-03-20 21:05:34
...

需要考虑尽可能在原始代码上修改,但又不影响原先的代码逻辑。

已知,原始代码的验证码储存与 session,vue是无法带上session进行请求,也就无法验证验证码。

那么,我们需要把验证码数据储存与缓存。

vendor\topthink\think-captcha\src\Captcha.php

public function entry($id = '')
//改为
public function entry($id = '',$cache=false)

//把原先的Session::set($key . $id, $secode, '');加多一个储存方式
        if ($cache) {
            cache($key . $id, $secode,3600);
        }else{
            Session::set($key . $id, $secode, '');
        }

验证码修改文件

\vendor\thinkcmf\cmf\src\controller\CaptchaController.php

在$response = hook_one('captcha_image', $config);上边加上代码,用于判断是否要储存与缓存。

$cache = $request->param('cache', null);
        if (!empty($cache)) {
            $cache = true;
        }else{
            $cache = false;
        }

$response = $captcha->entry($id,$cache);//与$response = $captcha->entry($id);

//源代码
@ob_clean();// 清除输出缓存
return $response;

//改为
        @ob_clean();// 清除输出缓存
        if ($cache) {
            $id = time() . mt_rand(100, 999);
            $data['imgData'] = 'data:image/png;base64,'.base64_encode($response);
            $data['captchaId'] = $id;
            return json(['code' => 1,'msg'=> 'success','data' => $data]);

        }
        return $response;

获取/new_captcha.html?height=40&width=200&font_size=15&length=4&cache=1返回结果

{
    "code": 1,
    "msg": "success",
    "data": {
        "imgData": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAAoBAMAAADHxCMWAAAAG1BMVEX///9FEy7n4eS5prCiiZaKa3zQxMpcMEhzTmJHlm1SAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABZUlEQVRIie2SsU+DQBTG8aDQ0c+q15GYYB2h0uiINcaOVI12hMlVjNqOYhz6Z/fB3VGMUJTFxPQb4OWR373vfZymbfX3cuY3Rc3837EG0Fe1nvL1h6MfwFMArihHVKq2+YCDRlYHTxDkpUXsZ/nMoAm2EFoFzMeq3SEWT01wDNvCUMJuqf0xwG4T7CHypEGCz+eBcm13myd74GRwT8ATqJRS2PGmnZl/aYuUgEMBo4h7igV4VA9f0BhbM+npqMlYAoKIs3Pq/5UuJzoI5XZWf0x2BWyWXFSIfswE3Kb3MBUx67RFIoEOescb4B0al1GZwX3Zcx1VenCN9bX9phM8F3CQd9g1le/i6xSviUiiShQXnwGZbbypRdYZZbeTdqqWkQeSmWSzW9kzE/QjWZMfHtQN7oL7CRbul6Z+WsxiV3eRVqcOpTFovn/VYsDLGXrtYO2xuJVtRHEjbAuz0fK+LbvVVv9HK+OPMQdZs9zmAAAAAElFTkSuQmCC",
        "captchaId": "1616053804248"
    }
}

 

验证

文件vendor\topthink\think-captcha\src\Captcha.php找到check改为

public function check($code, $id = '',$cache=false)
    {
        $key = $this->authcode($this->seKey) . $id;
        // 验证码不能为空
        if ($cache) {
            $secode = cache($key);
        }else{
            $secode = Session::get($key, '');
        }

        //halt($secode);
        if (empty($code) || empty($secode)) {
            return false;
        }
        // session 过期
        if (time() - $secode['verify_time'] > $this->expire) {
            if ($cache) {
                cache($key, NULL);
            }else{
                Session::delete($key, '');
            }
            return false;
        }

        if ($this->authcode(strtoupper($code)) == $secode['verify_code']) {
            if ($cache) {
                cache($key, NULL);
            }else{
                $this->reset && Session::delete($key, '');
            }
            return true;

        }

        return false;
    }

 

文件\vendor\thinkcmf\cmf\src\common.php找到cmf_captcha_check

改为

function cmf_captcha_check($value, $id = "", $reset = true,$ceche=false)
{
    $captcha        = new \think\captcha\Captcha();
    $captcha->reset = $reset;
    //halt($id);

    return $captcha->check($value, $id,$ceche);
}

文件vendor\thinkcmf\cmf-app\src\user\controller\LoginController.php找到doLogin

改为

$data = $this->request->post();

            $ValidateData = [
                'captcha' => 'require',
                'username' => 'require',
                'password' => 'require|min:6|max:32',
            ];
            $ValidateMsg = [
                'username.require' => '用户名不能为空',
                'password.require' => '密码不能为空',
                'password.max' => '密码不能超过32个字符',
                'password.min' => '密码不能小于6个字符',
                'captcha.require' => '验证码不能为空',
            ];
            $captchaId = '';
            $ceche = false;
            if (isset($data['captchaId'])) {
                $ceche = true;
                $captchaId = $data['captchaId'];
                $Validate['captchaId'] = 'require';
                $ValidateMsg['captchaId.require'] = '验证码ID不能为空';
            }

            $validate = new \think\Validate($ValidateData);

            $validate->message($ValidateMsg);


            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            if (!cmf_captcha_check($data['captcha'],$captchaId,true,$ceche)) {
                $this->error(lang('CAPTCHA_NOT_RIGHT'));
            }

 

 

相关标签: thinkphp5 PHP