二维码生成记录一下

日期:2019-01-26 浏览:199次
//二维码生成
public function random_short()
{
    if ($this->request->isPost() && $this->request->isAjax()) {
        $post = $this->request->param();
        if (empty($post['id'])){
            return Response::create(['code'=>'0','msg'=>'非法访问'], 'json');
        }else{
            $short_url = Db::name('random_code_group')->where('id','eq',$post['id'])->value('short_url');
            if (!empty($short_url)){
                return Response::create(['code'=>'0','msg'=>$short_url], 'json');
            }else{
             require_once EXTEND_PATH.'phpqrcode.php';
                $qrcode = new \QRcode();
                $url = Config::get('domain') . 'wap/random_code/index/id/'.$post['id'].'.html';
             $root_path = Config::get('file_root') . 'data/random_code/';
             if (!is_dir($root_path)) {
              mk_dir($root_path);
             }
             $objectName = 'data/random_code/' . $post['id'] . '.jpg';
                $local_file = Config::get('file_root') . $objectName;//文件名
                $errorCorrectionLevel = 'L'; //容错级别
                $matrixPointSize = 8; //生成图片大小
                $qrcode->png($url, $local_file, $errorCorrectionLevel, $matrixPointSize, 2);//生成二维码图片
                //获取配置项,并赋值给对象$config
                $config = Config::get('aliyun_oss');
                $ossClient = new \OSS\OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
                $options = array(
                    \OSS\OssClient::OSS_FILE_DOWNLOAD => $local_file
                );
                // 二维码图片
                $ossClient->uploadFile($config['Bucket'], $objectName, $local_file);
                $img_url = Config::get('cdn') . $objectName;
                Db::name('random_code_group')->where('id','eq',$post['id'])->setField('short_url',$img_url);
                return Response::create(['code'=>'0','msg'=>$img_url], 'json');
            }
        }
    }
}

没有评论

发表评论