<?php
declare (strict_types = 0);

namespace app\admin\controller\v1;

use think\facade\Cache;
use think\facade\Config;

/**
 * 验证码控制器
 */
class Captcha
{
    /**
     * 获取验证码
     */
    public function index()
    {
        // 生成token
        $token = md5(uniqid() . random_str(8, 'all'));

        // 生成验证码
        $captcha = \think\captcha\facade\Captcha::create();

        // 缓存验证码
        $hash = password_hash($captcha['code'], PASSWORD_BCRYPT, ['cost' => 10]);
        Cache::store('redis')->set('captcha:token.' . $token, $hash, Config::get('captcha.expire'));

        // 输出验证码
        return success('获取验证码成功', [
            'token'   => $token,
            'captcha' => $captcha["img"],
        ]);
    }
}
