public function code()
{
$wordId = input('wordId');
if (!$wordId) {
$this->error('数据异常');
}
$word = Db::name('word');
$word_data = $word->where('word_id', $wordId)->find();
$path = ROOT_PATH . 'Public/' . $word_data['code_path'];
if ($word_data['code_path'] && file_exists($path)) {
$img = $word_data['code_path'];
} else {
$rr = request()->domain();
$rr = $rr . url('student/home/code', ['wordId' => $wordId]);
$name = $word_data['grade_id'] . '_' . $word_data['word_name'];
$img = $this->qrcode($rr, $name);
$word->where('word_id', $wordId)->update(['code_path' => $img]);
}
$this->assign('img', $img);
return $this->fetch();
}
public function qrcode($url, $name)
{
ob_clean();
vendor("phpqrcode.phpqrcode");
$level = 3;
$size = 4;
$errorCorrectionLevel = intval($level);//容错级别
$matrixPointSize = intval($size);//生成图片大小
$fileName = 'qrcode/' . $name . '.png';
// 生成的文件名
$path = ROOT_PATH . 'Public/qrcode/' . $name . '.png';
//生成二维码图片
$object = new \QRcode();
$object->png($url, $path, $errorCorrectionLevel, $matrixPointSize, 2);
return $fileName;
}