This commit is contained in:
wangjinlei
2021-06-15 10:33:18 +08:00
parent 08ea33979d
commit 936978d35c
5 changed files with 335 additions and 12 deletions

View File

@@ -0,0 +1,259 @@
<?php
namespace app\api\controller;
use think\Controller;
use think\Queue;
/**
* Description of Auto
*
* @author jgll2
*/
class Img extends Controller {
public function __construct(\think\Request $request = null) {
parent::__construct($request);
}
public function test() {
echo 'okokoook';
}
public function reviewerImport() {
//接收信息
$data = $this->request->post();
// $data['url'] = 'zhengshu/import/20210610/e7a3ae0879619862f0f5584d674ffc69.xlsx';
// $data['course'] = 'ZYMR';
//读取excel
$path = ROOT_PATH . 'public' . DS . $data['url'];
$arr = self::readExcel($path);
foreach ($arr as $v) {
if ($v['username'] == '') {
continue;
}
$datae['name'] = $v['username'];
$datae['course'] = $data['course'];
Queue::push('app\api\job\img@fire', $datae, "img");
}
// $this->imgCL($arr,$data['course']);
}
// public function t(){
// Queue::push( 'app\api\job\img@fire',[], "img" );
// }
/**
* 接收导入文件
* @return type
*/
public function up_import() {
$file = request()->file('importExcel');
if ($file) {
$info = $file->move(ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'import');
if ($info) {
return json(['code' => 0, 'upurl' => str_replace("\\", "/", $info->getSaveName())]);
} else {
return json(['code' => 1, 'msg' => $file->getError()]);
}
}
}
/**
* 获取excel数据
*/
public function getExcelData() {
//接收数据
$data = $this->request->post();
$has = $this->getImgs($data['course']);
$frag = [];
if ($data['url'] != '') {
$path = ROOT_PATH . 'public' . DS . $data['url'];
$frag = self::readExcel($path);
}
$re['list'] = array_merge($frag, $has);
$re['count'] = count($frag);
$re['has'] = count($has);
return json($re);
}
/**
* 获取目前科目下的文件总数
*/
public function getAllNum(){
$data = $this->request->post();
$path = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img/'.$data['course'].DS;
$res = scandir($path);
$dirs = [];
foreach ($res as $v) {
if ($v == '.' || $v == '..') {
continue;
}
$dirs[] = ['dirname' => $v];
}
return json(count($dirs));
}
/**
* 获取课程列表
*/
public function getCourse() {
$base = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img/';
$res = scandir($base);
$dirs = [];
foreach ($res as $v) {
if ($v == '.' || $v == '..') {
continue;
}
$dirs[] = ['dirname' => $v];
}
return json($dirs);
}
/**
* 获取文件下的证书文件
*/
private function getImgs($course) {
$dir = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img/' . $course . DS;
$res = scandir($dir);
$imgs = [];
foreach ($res as $v) {
if ($v == '.' || $v == '..') {
continue;
}
$imgs[] = ['username' => substr($v, 0, strrpos($v, '.')), 'has' => '已出证'];
}
return $imgs;
}
public function ttt() {
$v = '12_王金磊.png';
echo substr($v, 0, strrpos($v, '_'));
}
/**
* 处理图片主方法
*/
private function imgCL($arr, $course) {
$cous = $this->getImgs($course);
foreach ($arr as $k => $v) {
if ($v['username'] == '') {
continue;
}
$lim = [];
if (preg_match("/^[\x7f-\xff]+$/", $v['username']) && strlen($v['username']) == 9) {
$lim = [640, 890];
} else if (preg_match("/^[\x7f-\xff]+$/", $v['username']) && strlen($v['username']) == 6) {
$lim = [670, 890];
} else {
$left = (730 - ((35 * strlen($v['username'])) / 2));
$lim = [$left, 890];
}
$template = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'template.png';
$ziti = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'siyuan.ttf';
$image = \think\Image::open($template);
$image->text($v['username'], $ziti, 50, '#000000', $lim)
->text('NO.ZYMR' . (20210619001 + $k + count($cous)), $ziti, 25, '#000000', [573, 650])
->save(ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img' . DS . $course . DS . (1 + $k + count($cous)) . '_' . $v['username'] . '.png');
}
}
/**
*
* 清空
*/
public function clear() {
$data = $this->request->post();
$path = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img/' . $data['course'] . DS;
$dh = opendir($path);
while ($file = readdir($dh)) {
if ($file != "." && $file != "..") {
$fullpath = $path . "/" . $file;
// if (!is_dir($fullpath)) {
unlink($fullpath);
// } else {
// deldir($fullpath);
// }
}
}
closedir($dh);
return jsonSuccess(['code'=>0]);
}
//压缩文件
public function getZip() {
$data = $this->request->post();
$zipName = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'zip' . DS . $data['course'] . '.zip';
$zip = new \ZipArchive; //使用本类linux需开启zlibwindows需取消php_zip.dll前的注释
if (is_file($zipName)) {
$zip->open($zipName, \ZIPARCHIVE::OVERWRITE);
} else {
$zip->open($zipName, \ZIPARCHIVE::CREATE);
}
$dir = ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img' . DS . $data['course'] . '/';
$files = scandir($dir);
foreach ($files as $val) {
if ($val == '.' || $val == '..') {
continue;
}
if (file_exists(ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img' . DS . $data['course'] . '/' . $val)) {
//addFile函数首个参数如果带有路径则压缩的文件里包含的是带有路径的文件压缩
//若不希望带有路径,则需要该函数的第二个参数
$zip->addFile(ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img' . DS . $data['course'] . '/' . $val, basename(ROOT_PATH . 'public' . DS . 'zhengshu' . DS . 'img' . DS . $data['course'] . '/' . $val)); //第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下
}
}
$zip->close(); //关闭
if (!file_exists($zipName)) {
exit("无法找到文件"); //即使创建,仍有可能失败
}
$re['url'] = 'public/zhengshu' . DS . 'zip' . DS . $data['course'] . '.zip';
return jsonSuccess($re);
//如果不要下载,下面这段删掉即可,如需返回压缩包下载链接,只需 return $zipName;
// header("Cache-Control: public");
// header("Content-Description: File Transfer");
// header('Content-disposition: attachment; filename='.basename($zipName)); //文件名
// header("Content-Type: application/zip"); //zip格式的
// header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
// header('Content-Length: '. filesize($zipName)); //告诉浏览器,文件大小
// @readfile($zipName);
}
/**
* 读取excel数据
*/
private function readExcel($path) {
$extension = substr($path, strrpos($path, '.') + 1);
vendor("PHPExcel.PHPExcel");
if ($extension == 'xlsx') {
$objReader = new \PHPExcel_Reader_Excel2007();
$objPHPExcel = $objReader->load($path);
} else if ($extension == 'xls') {
$objReader = new \PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load($path);
}
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$frag = [];
for ($i = 1; $i <= $highestRow; $i++) {
$aa['username'] = $objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();
$aa['has'] = '未出证';
if($aa['username']==''){
continue;
}
$frag[] = $aa;
}
return $frag;
}
}