MiPHP无状态验证码生成和验证

     /**
     * http://127.0.0.1:9501/index/MakeCaptcha
     * 请求带上access_token
     * 返回base64的字符串,前端追加到img上
     * @return [type] [description]
     */
    public function actionMakeCaptcha()
    {
        $token_user_info = Token::get('userinfo');
        $captcha         = new Captcha([
            'width'      => 100,
            'height'     => 40,
            'fontFile'   => app()->ROOT_PATH . '/3rd/fonts/TimesNewRomanBold.ttf',
            'fontSize'   => 20,
            'wordNumber' => 6,
            'angleRand'  => [-20, 20],
            'xSpacing'   => 0.82,
            'yRand'      => [5, 15],
        ]);
        $captcha->generate();
        Redis::set('changePassword' . $token_user_info['uid'], $captcha->getText());
        $baseImg = 'data:image/png;base64,' . base64_encode($captcha->getContent());
        return $baseImg;
    }
    /**
     * 验验证码
     * 无状态api使用
     * 带上access_token以及content-type: 'application/x-www-form-urlencoded'
     * @return [type] [description]
     */
    public function actionCheckCaptcha()
    {
        $token_user_info = Token::get('userinfo');
        if (empty($token_user_info)) {
            return ['code' => -1, 'message' => '重新登陆!'];
        } else {
            $code      = app()->request->post('captcha');
            $data_code = Redis::get('changePassword' . $token_user_info['uid']);
            if ($code == $data_code) {
                Redis::delete('changePassword' . $token_user_info['uid']);
                return true;
            }
            return false;
        }
    }

猜你喜欢

转载自blog.csdn.net/HD2killers/article/details/82888016