This commit is contained in:
wangjinlei
2024-12-18 09:10:10 +08:00
parent f11e97e89c
commit 3f03d9357a
4 changed files with 540 additions and 36 deletions

View File

@@ -78,6 +78,9 @@ class Base extends Controller
protected $email_account_obj="";
protected $article_image_obj="";
protected $article_table_obj="";
protected $article_main_obj="";
protected $article_main_image_obj = "";
protected $article_main_table_obj = '';
public function __construct(\think\Request $request = null)
@@ -151,6 +154,9 @@ class Base extends Controller
$this->email_account_obj = Db::name("email_account");
$this->article_image_obj = Db::name("article_image");
$this->article_table_obj = Db::name("article_table");
$this->article_main_obj = Db::name("article_main");
$this->article_main_image_obj = Db::name("article_main_image");
$this->article_main_table_obj = Db::name("article_main_table");
}
@@ -382,6 +388,202 @@ class Base extends Controller
return jsonSuccess([]);
}
public function addArticleMainEx($article_id){
$article_info = $this->article_obj->where('article_id', $article_id)->find();
$files = $this->article_file_obj
->where('article_id', $article_info['article_id'])
->where('type_name', 'manuscirpt')
->order('ctime desc')
->limit(1)
->select();
if (count($files) == 0) {
return jsonError('No Manuscript');
}
$dir = ROOT_PATH."public/articleImage/".$article_id;
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
$url = $this->ts_base_url."api/typeset/readDocx";
$program['fileRoute'] = "https://submission.tmrjournals.com/public/" . $files[0]['file_url'];
$program['article_id'] = $article_id;
$res = object_to_array(json_decode(myPost($url, $program)));
$file_runs = $res['data'];
//需要处理很多事情
if(!isset($file_runs['mains'])){
return "system error";
}
$refer_start = false;
foreach ($file_runs['mains'] as $k=>$v){
if($this->isImageTag($v)){//img
preg_match("/\/public\/articleImage\/(.*?)'/", $v, $matches);
if (!isset($matches[1])) {continue;}
$insert_image['article_id'] = $article_id;
$insert_image['url'] = $matches[1];
$insert_image['ctime'] = time();
$c_id = $this->article_main_image_obj->insertGetId($insert_image);
$insert1['article_id'] = $article_id;
$insert1['type'] = 1;
$insert1['ami_id'] = $c_id;
$insert1['content'] = $v;
$insert1['sort'] = $k+1;
$insert1['ctime'] = time();
$this->article_main_obj->insert($insert1);
}else{//文本
if($refer_start&&$v!=""){
if (strlen($v) > 500) {
$refer_start = false;
$cc_t = $this->replaceChinesePunctuation($this->removeExtraSpaces(trim($v)));
$insert['article_id'] = $article_id;
$insert['type'] = 0;
$insert['content'] = $cc_t;
$insert['sort'] = $k+1;
$insert['ctime'] = time();
$this->article_main_obj->insert($insert);
continue;
}
continue ;
}
if (strtolower($v) == 'reference:' || strtolower($v)=="<b>references</b>"||strtolower($v) == 'references:' || strtolower($v) == 'references' || strtolower($v) == 'reference') {
$refer_start = true;
continue;
}
$c_t = $this->replaceChinesePunctuation($this->removeExtraSpaces(trim($v)));
$insert['article_id'] = $article_id;
$insert['type'] = 0;
$insert['content'] = $c_t;
$insert['sort'] = $k+1;
$insert['ctime'] = time();
$this->article_main_obj->insert($insert);
}
}
//处理表格
$tables = $this->article_table_obj->where("article_id",$article_id)->where("state",0)->select();
foreach ($tables as $table) {
$ins['article_id'] = $article_id;
$ins['type'] = 0;
$ins['table_data'] = $table['table'];
$ins['ctime'] = time();
$this->article_main_table_obj->insert($ins);
}
$pics = $this->article_file_obj->where("article_id",$article_id)->where("type_name","picturesAndTables")->where("state",0)->order("file_id desc")->limit(1)->select();
if(!isset($pics[0])){
return ;
}
$extension = pathinfo($pics[0]['file_url'], PATHINFO_EXTENSION);
if($extension=="zip"){
$file = ROOT_PATH."public/".$pics[0]['file_url'];
$zip = new \ZipArchive();
// 打开 ZIP 文件
if ($zip->open($file) === true) {
$fileCount = $zip->numFiles;
$num_rand = uniqid('', true); // 使用 uniqid 来生成更唯一的文件名
for ($i = 0; $i < $fileCount; $i++) {
$stat = $zip->statIndex($i);
$fileName = basename($stat['name']); // 获取文件名并去掉路径
$fileContent = $zip->getFromIndex($i);
$base_url = ROOT_PATH . "public/articleImage/" . $article_id . "/";
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
// 创建文件名并确保唯一性
$newFileName = "zipimg" . $num_rand . $i . '.' . $extension;
if (in_array(strtolower($extension), ['jpg', 'jpeg', 'png'])) {
// 处理图片文件
file_put_contents($base_url . $newFileName, $fileContent);
$insert['article_id'] = $article_id;
$insert['url'] = $article_id . "/" . $newFileName;
$insert['ctime'] = time();
$this->article_main_image_obj->insert($insert);
} elseif (in_array(strtolower($extension), ['tiff', 'tif'])) {
// 处理 TIFF 和 EMF 文件并转换为 JPG
file_put_contents($base_url . $newFileName, $fileContent);
$insert['article_id'] = $article_id;
$insert['url'] = $article_id . "/" . $newFileName;
$insert['ctime'] = time();
$this->article_main_image_obj->insert($insert);
} elseif ($extension == "docx") {
$word_dir = ROOT_PATH . "public/articleCache/" . $article_id;
if (!is_dir($word_dir)) {
mkdir($word_dir, 0777, true);
}
$word_file = uniqid('', true) . $fileName;
file_put_contents($word_dir . "/" . $word_file, $fileContent);
$url = $this->ts_base_url . "api/typeset/markImages";
$program['file'] = "https://submission.tmrjournals.com/public/articleCache/" . $article_id . "/" . $word_file;
$program['article_id'] = $article_id;
$res = object_to_array(json_decode(myPost($url, $program)));
if (!isset($res['data']['list'])) {
return;
}
$file_runs = $res['data']['list'];
foreach ($file_runs as $val) {
$insert['article_id'] = $article_id;
$insert['url'] = $val;
$insert['ctime'] = time();
$this->article_main_image_obj->insert($insert);
}
}
}
// 关闭 ZIP 文件
$zip->close();
} else {
return json(['error' => 'Unable to open ZIP file.']);
}
}
}
public function isImageTag($str) {
// 正则表达式匹配 <img> 标签,忽略大小写
$pattern = '/^\s*<img\s+[^>]*\s*src=["\'][^"\']+["\'][^>]*>\s*$/i';
// 使用 preg_match 判断是否符合 img 标签的格式
if (preg_match($pattern, $str)) {
return true; // 是 img 标签
}
return false; // 不是 img 标签
}
public function removeExtraSpaces($str) {
// 使用正则表达式匹配两个及以上的连续空格,并替换为一个空格
return preg_replace('/\s{2,}/', ' ', $str);
}
public function replaceChinesePunctuation($str) {
// 定义中文标点和对应的英文标点替换
$replacements = [
'' => ',', // 中文逗号 -> 英文逗号
'。' => '.', // 中文句号 -> 英文句号
'' => '!', // 中文感叹号 -> 英文感叹号
'' => '?', // 中文问号 -> 英文问号
'' => ':', // 中文冒号 -> 英文冒号
'' => ';', // 中文分号 -> 英文分号
'' => '(', // 中文左括号 -> 英文左括号
'' => ')', // 中文右括号 -> 英文右括号
'【' => '[', // 中文左方括号 -> 英文左方括号
'】' => ']', // 中文右方括号 -> 英文右方括号
'《' => '<', // 中文左书名号 -> 英文左书名号
'》' => '>', // 中文右书名号 -> 英文右书名号
'、' => ',', // 中文顿号 -> 英文逗号
'——' => '--', // 中文破折号 -> 英文破折号
'·' => '.', // 中文间隔符 -> 英文点
];
// 替换所有中文标点
return str_replace(array_keys($replacements), array_values($replacements), $str);
}
public function getBoardsForJournal($journal_id, $aar = false)
{
$ca_board = [];