Files
journal/application/master/service/push/CnkiPusher.php

147 lines
6.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\master\service\push;
/**
* CNKI 中国知网 推送器
* 生成整期 Excel 并打包 zip通过 FTP 上传到知网。
* 逻辑迁移自 Datebase::createFtpFileInfo / createZip / sendFtp。
*/
class CnkiPusher extends AbstractPusher
{
public function code()
{
return 'cnki';
}
public function label()
{
return 'CNKI 中国知网';
}
public function pushStage($journal_stage_id, array $options = [])
{
$stage_info = $this->journal_stage_obj->where("journal_stage_id", $journal_stage_id)->find();
$journalInfo = $this->journal_obj->where('journal_id', $stage_info['journal_id'])->find();
$articles = $this->article_obj->where(['journal_stage_id' => $journal_stage_id, 'state' => 0])
->field('title as articleTitle,abbr as author,pub_date as date,keywords as keyWords,doi,abstract,file_pdf as pdf,article_id,journal_id')
->select();
foreach ($articles as $k => $v) {
$articles[$k]['abstract'] = strip_tags($v['abstract']);
$articles[$k]['journalTitle'] = $journalInfo['title'];
$articles[$k]['issn'] = $journalInfo['issn'];
$articles[$k]['vol'] = $stage_info['stage_vol'];
$articles[$k]['issue'] = $stage_info['stage_no'];
$articles[$k]['pdf'] = 'https://www.tmrjournals.com/public/articlePDF/' . $v['pdf'];
$articles[$k]['linkToAbstract'] = 'https://www.tmrjournals.com/article.html?J_num=' . $v['journal_id'] . '&a_id=' . $v['article_id'];
$articles[$k]['publisher'] = 'TMR publishing group';
$articles[$k]['reference'] = '';
$articles[$k]['eissn'] = '';
}
$baseDir = ROOT_PATH . 'public' . DS . 'ftpFile' . DS;
$outfileTitle = $journalInfo['title'] . "-" . $stage_info['stage_vol'] . "" . $stage_info['stage_no'] . "";
$res = $this->createZip($articles, $baseDir, $outfileTitle);
$resJosn = json_decode($res, true);
if ($resJosn['code'] != 0) {
throw new \Exception('CNKI zip 生成失败: ' . $resJosn['msg']);
}
$url = $baseDir . $outfileTitle . ".zip";
$this->sendFtp($url, $outfileTitle);
return PushResult::success('CNKI zip 已生成并上传');
}
private function createZip($articles, $baseDir, $outfileTitle)
{
Vendor('PHPExcel.PHPExcel');
Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
$objExcel = new \PHPExcel();
$objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
$objActSheet = $objExcel->getActiveSheet();
$letter = explode(',', "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O");
$arrHeader = array('Article Title', 'Author', 'Publishing Date', 'Key Words', 'Journal Title', 'ISSN', 'EISSN', 'Volume', 'Issue', 'Link to Full-Text Articles (PDF)', 'Abstract', 'DOI', 'Link to Abstract', 'Publisher', 'Reference');
$lenth = count($arrHeader);
for ($i = 0; $i < $lenth; $i++) {
$objActSheet->setCellValue("$letter[$i]1", "$arrHeader[$i]");
$objActSheet->getRowDimension('1')->setRowHeight(25);
}
foreach ($articles as $k => $v) {
$k += 2;
$objActSheet->setCellValue('A' . $k, $v['articleTitle']);
$objActSheet->setCellValue('B' . $k, $v['author']);
$objActSheet->setCellValue('C' . $k, $v['date']);
$objActSheet->setCellValue('D' . $k, $v['keyWords']);
$objActSheet->setCellValue('E' . $k, $v['journalTitle']);
$objActSheet->setCellValue('F' . $k, $v['issn']);
$objActSheet->setCellValue('G' . $k, $v['eissn']);
$objActSheet->setCellValue('H' . $k, $v['vol']);
$objActSheet->setCellValue('I' . $k, $v['issue']);
$objActSheet->setCellValue('J' . $k, $v['pdf']);
$objActSheet->setCellValue('K' . $k, $v['abstract']);
$objActSheet->setCellValue('L' . $k, $v['doi']);
$objActSheet->setCellValue('M' . $k, $v['linkToAbstract']);
$objActSheet->setCellValue('N' . $k, $v['publisher']);
$objActSheet->setCellValue('O' . $k, $v['reference']);
$objActSheet->getRowDimension($k)->setRowHeight(25);
}
$width = array(10, 15, 20, 25, 30);
$objActSheet->getColumnDimension('A')->setWidth($width[4]);
$objActSheet->getColumnDimension('B')->setWidth($width[2]);
$objActSheet->getColumnDimension('C')->setWidth($width[1]);
$objActSheet->getColumnDimension('D')->setWidth($width[4]);
$objActSheet->getColumnDimension('E')->setWidth($width[4]);
$objActSheet->getColumnDimension('F')->setWidth($width[2]);
$objActSheet->getColumnDimension('G')->setWidth($width[3]);
$objActSheet->getColumnDimension('H')->setWidth($width[1]);
$objActSheet->getColumnDimension('I')->setWidth($width[1]);
$objActSheet->getColumnDimension('J')->setWidth($width[4]);
$objActSheet->getColumnDimension('K')->setWidth($width[3]);
$objActSheet->getColumnDimension('L')->setWidth($width[3]);
$objActSheet->getColumnDimension('M')->setWidth($width[4]);
$objActSheet->getColumnDimension('N')->setWidth($width[2]);
$objActSheet->getColumnDimension('O')->setWidth($width[1]);
if (!is_dir($baseDir)) {
@mkdir($baseDir, 0777, true);
}
$objWriter->save($baseDir . $outfileTitle . ".xlsx");
$path = $baseDir . $outfileTitle . ".xlsx";
$fileName = $outfileTitle . ".zip";
$zip = new \ZipArchive;
$zip->open($baseDir . $fileName, \ZipArchive::CREATE);
$zip->addFile($path, basename($path));
$zip->close();
unlink($baseDir . $outfileTitle . ".xlsx");
if (!file_exists($baseDir . $fileName)) {
return json_encode(['code' => 1, 'msg' => '无法找到文件']);
}
return json_encode(['code' => 0, 'msg' => '成功', 'data' => $baseDir . $fileName]);
}
private function sendFtp($url, $title)
{
$cred = $this->cred('cnki');
$con = ftp_connect($cred['host'], $cred['port']);
if (!$con) {
throw new \Exception('CNKI FTP 连接失败');
}
$res = ftp_login($con, $cred['user'], $cred['pass']);
if (!$res) {
ftp_close($con);
throw new \Exception('CNKI FTP 登录失败');
}
$name = iconv("UTF-8", "GBK", $title . '.zip');
ftp_pasv($con, true);
$data = ftp_put($con, '/' . $name, $url, FTP_BINARY);
ftp_close($con);
if (!$data) {
throw new \Exception('CNKI FTP 上传失败');
}
}
}