Files
tougao/application/api/controller/Contribute.php
2025-11-10 13:32:36 +08:00

429 lines
19 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\api\controller;
use app\api\controller\Base;
use think\Db;
use PhpOffice\PhpWord\IOFactory;
use think\Exception;
use \app\common\ArticleParserService;
/**
* @title 自动投稿控制器
* @description 所有的测试方法汇总
*/
class Contribute extends Base
{
private $phpWord; // PhpWord实例
private $sections; // 文档段落集合
public function __construct(\think\Request $request = null) {
parent::__construct($request);
}
/**
* 自动投稿
* @param file_url 文件地址
*/
public function contribute($aParam = []){
//获取参数
$aParam = empty($aParam) ? $this->request->post() : $aParam;
//必填值验证
$sFileUrl = empty($aParam['file_url']) ? '' : $aParam['file_url'];
$sOriginalFileUrl = $sFileUrl;
if(empty($sFileUrl)){
return json_encode(['status' => 2,'msg' => 'Please upload the submission file']);
}
// //文章类型
// $sType = empty($aParam['type']) ? '' : $aParam['type'];
// if(empty($sType)){
// return json_encode(['status' => 2,'msg' => 'Please select the article type']);
// }
// //期刊ID
// $iJournalId = empty($aParam['journal_id']) ? '' : $aParam['journal_id'];
// if(empty($iJournalId)){
// return json_encode(['status' => 2,'msg' => 'Please select the journal to which you belong']);
// }
//用户ID
$iUserId = empty($aParam['user_id']) ? '' : $aParam['user_id'];
if(empty($iUserId)){
return json_encode(['status' => 2,'msg' => 'Please select author']);
}
//判断文件是否执行
$sFileUrl = rtrim(ROOT_PATH,'/').'/public/'.ltrim(ltrim($sFileUrl,'/'),'public');
if (!file_exists($sFileUrl)) {
return json_encode(['status' => 3, 'msg' => 'The uploaded file does not exist']);
}
if (!is_readable($sFileUrl)) {
return json_encode(['status' => 4, 'msg' => 'The uploaded file is unreadable']);
}
//文章ID
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
$aWhere = [];
if(!empty($iArticleId)){//更新文章内容
$aWhere['article_id'] = $iArticleId;
$aArticle = Db::name('article')->field('state,user_id,manuscirpt_url')->where($aWhere)->find();
if(empty($aArticle)){
return json_encode(['status' => 7,'msg' => 'The article does not exist']);
}
if($aArticle['state'] != -1 && $aArticle['state'] != 3){
return json_encode(['status' => 8,'msg' => 'Article cannot be edited']);
}
if($aArticle['user_id'] != $iUserId){
return json_encode(['status' => 9,'msg' => 'Not modified by the author themselves']);
}
if($aArticle['manuscirpt_url'] == trim($sOriginalFileUrl,'/')){
return json_encode(['status' => 9,'msg' => 'The content of the article has not changed']);
}
$aWhere['article_id'] = ['<>',$iArticleId];
}
//获取数据
$aDealData = json_decode(ArticleParserService::uploadAndParse($sFileUrl),true);
$iStatus = empty($aDealData['status']) ? 0 : $aDealData['status'];
$sMsg = empty($aDealData['msg']) ? 'Content parsing failed' : $aDealData['msg'];
if($iStatus != 1){
return json_encode(['status' => 5, 'msg' => $sMsg]);
}
$aData = empty($aDealData['data']) ? '' : $aDealData['data'];
if(empty($aData['title'])){
return json_encode(['status' => 6,'msg' => 'The article title is empty']);
}
//查询标题是否存在
$aWhere += ['title' => trim($aData['title']),'state' => ['<>',3]];
$aArticle = Db::name('article')->field('article_id')->where($aWhere)->find();
if(!empty($aArticle)){
return json_encode(['code' => 7, 'msg' => 'Warning: you are re-submitting the article!']);
}
//数据入库
$aData += $aParam;
$result = $this->_addData($aData);
return $result;
}
/**
* 组装数据插入相关数据表
* @param array $aParam
*/
private function _addData($aParam = []){
if(empty($aParam)){
return json_encode(['status' => 2,'msg' => 'Data is empty']);
}
//获取文章ID
$iUpdateArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
if(!empty($iUpdateArticleId)){
$aWhere = ['article_id' => $iUpdateArticleId];
$aArticle = Db::name('article')->field('title,keywords,abstrart,fund,journal_id')->where($aWhere)->find();
}
//插入基础表 t_article
$aInsert = [];
//标题
// if(empty($aArticle['title'])){
$sTitile = empty($aParam['title']) ? '' : $aParam['title'];
if(!empty($sTitile)){
$aInsert['title'] = is_string($sTitile) ? strip_tags($sTitile) : '';
}
// }
//关键词
// if(empty($aArticle['keywords'])){
$sKeyWords = empty($aParam['keywords']) ? '' : $aParam['keywords'];
if(!empty($sKeyWords)){
$aInsert['keywords'] = is_array($sKeyWords) ? implode(',', $sKeyWords) : $sKeyWords;
$aInsert['keywords'] = is_string($aInsert['keywords']) ? str_replace(';', ',', $aInsert['keywords']) : '';
}
// }
//摘要
// if(empty($aArticle['abstrart'])){
$sAbstrart = empty($aParam['abstrart']) ? '' : $aParam['abstrart'];
if(!empty($sAbstrart)){
$aInsert['abstrart'] = is_string($sAbstrart) ? strip_tags($sAbstrart) : '';
}
// }
//基金
// if(empty($aArticle['fund'])){
$sFund = empty($aParam['fund']) ? '' : $aParam['fund'];
if(!empty($sFund)){
$aInsert['fund'] = is_string($sFund) ? strip_tags($sFund) : '';
}
// }
//期刊ID
$iJournalId = empty($aParam['journal_id']) ? 0 : $aParam['journal_id'];
if(!empty($iJournalId)){
$aInsert['journal_id'] = $iJournalId;
//查询期刊信息
$aJournalWhere = ['journal_id' => $iJournalId,'state' => 0];
$aJournal = DB::name('journal')->field('editor_id')->where($aJournalWhere)->find();
$aInsert['editor_id'] = empty($aJournal['editor_id']) ? 0 : $aJournal['editor_id'];
}
//类型
$sType = empty($aParam['type']) ? '' : $aParam['type'];
if(!empty($sType)){
$aInsert['type'] = $sType;
}
//上传文件地址
$sFileUrl = empty($aParam['file_url']) ? '' : $aParam['file_url'];
if(!empty($sFileUrl)){
$aInsert['manuscirpt_url'] = trim(trim($sFileUrl,'/'),'public/');
}
//用户ID
$iUserId = empty($aParam['user_id']) ? 0 : $aParam['user_id'];
if(!empty($iUserId)){
$aInsert['user_id'] = $iUserId;
//查询用户信息
$aUser = Db::name('user')->field('account')->where(['user_id' => $iUserId,'state' => 0])->find();
}
Db::startTrans();
//插入article
if(empty($aArticle) && !empty($aInsert)){//新插入
$aInsert['ctime'] = time();
$aInsert['state'] = -1;
$aInsert['is_use_ai'] = 3;
$aInsert['is_figure_copyright'] = 3;
// $aInsert['is_transfer'] = 3;
$aInsert['is_become_reviewer'] = 3;
$sType = empty($aParam['type']) ? 'D' : $aParam['type'];
$aInsert['accept_sn'] = getArticleSN('Draft',$sType);
$iArticleId = Db::name('article')->insertGetId($aInsert);
if(empty($iArticleId)){
return json_encode(['status' => 3,'msg' => 'Article added successfully']);
}
}
if(!empty($aArticle) && !empty($aInsert)){//更新
$aWhere = ['article_id' => $iUpdateArticleId,'state' => ['in',[-1,3]]];
$update_result = Db::name('article')->where($aWhere)->limit(1)->update($aInsert);
if($update_result === false){
return json_encode(['status' => 3,'msg' => 'Article update failed']);
}
//更新作者为逻辑删除
$aWhere['state'] = 0;
$update_author_result = DB::name('article_author')->where($aWhere)->update(['state' => 1]);
if($update_author_result === false){
return json_encode(['status' => 4,'msg' => 'Article author update failed']);
}
}
//处理作者
$aCompanyId = $aAuthor = [];//作者机构ID
if(!empty($aParam['author'])){
$aAuthor = $aParam['author'];
foreach ($aAuthor as $key => $value) {
if(empty($iArticleId)){
break;
}
//处理作者名字
$aName = empty($value['name']) ? [] : explode(' ', $value['name']);
if(empty($aName)){
continue;
}
//名字拆分
$sOldLastName = array_pop($aName);
$letters = [
'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z'
];
if(in_array(strtolower($sOldLastName), $letters)){
$value['company_id'] = [$sOldLastName];
$sLastName = array_pop($aName);
if(!empty($value['superscript'])){
$value['is_super'] = strpos($value['superscript'], '1') !== false ? 1 : 0;
$value['superscript'] = $sOldLastName.$value['superscript'];
}
}else{
$sLastName = $sOldLastName;
}
$value['firstname'] = empty($aName) ? '' : trim(implode(' ', $aName));
if(empty($value['firstname'])){
continue;
}
if(ctype_upper($value['firstname'][0]) == false){
continue;
}
$value['lastname'] = $sLastName;
$aAuthor[$key] = $value;
if(!empty($value['company_id'])){
$aCompanyId[] = implode(',', $value['company_id']);
}
}
}
$aCompanyId = empty($aCompanyId) ? [] : array_unique(explode(',', implode(',', $aCompanyId)));
//文章机构
$iArticleId = empty($iArticleId) ? $iUpdateArticleId : $iArticleId;
if(!empty($aCompanyId)){
$aCompany = empty($aParam['company']) ? [] : $aParam['company'];
$aWhere = ['article_id' => $iArticleId,'state' => 0];
$aOrgan = Db::name('article_organ')->field('organ_id,organ_name,sort')->where($aWhere)->select();
$aOrganName = empty($aOrgan) ? [] : array_column($aOrgan, 'organ_name');
$iMaxSort = empty($aOrgan) ? 0 : max(array_column($aOrgan, 'sort'));
//查询文章
$aCompanyInsert = $aSort = [];
foreach ($aCompany as $key => $value) {
if(empty($value)){
continue;
}
if(!in_array($key, $aCompanyId)){
continue;
}
$sName = trim(trim(trim($value,'*'),'#'));
if(in_array($sName, $aOrganName)){
continue;
}
$iMaxSort++;
$iSort = $iMaxSort;
$aSort[$key] = $iMaxSort;
$sName = is_string($sName) ? strip_tags($sName) : '';
if(empty($sName)){
continue;
}
$aCompanyInsert[] = ['article_id' => $iArticleId,'organ_name' =>$sName,'create_time' => time(),'sort' => $iSort];
}
}
if(!empty($aCompanyInsert)){
$company_result = Db::name('article_organ')->insertAll($aCompanyInsert);
if(empty($company_result)){
return json_encode(['status' => 3,'msg' => 'Article institution insertion failed']);
}
$aWhere = ['article_id' => $iArticleId,'state' => 0];
$aCompanyData = Db::name('article_organ')->field('sort,organ_id,organ_name')->where($aWhere)->select();
$aCompanyData = empty($aCompanyData) ? [] : array_column($aCompanyData, null,'sort');
}
//处理作者
$aAuthorData = $aAuthorOrgn = [];
if(!empty($aAuthor)){
//通讯作者
$aCorresponding = empty($aParam['corresponding']) ? [] : array_column($aParam['corresponding'], null,'name');
//查询文章作者
$aWhere = ['article_id' => $iArticleId,'state' => 0];
$aAuthorList = Db::name('article_author')->field('art_aut_id,firstname,sort')->where($aWhere)->select();
$aAuthorNameList = empty($aAuthorList) ? [] : array_column($aAuthorList, 'firstname');
$iMaxSort = empty($aAuthorList) ? 0 : max(array_column($aAuthorList, 'sort'));
foreach ($aAuthor as $key => $value) {
//处理作者名
if(empty($value['firstname'])){
continue;
}
if(ctype_upper($value['firstname'][0]) == false){
continue;
}
//处理作者机构单位关联
$aCountry = [];
if(!empty($value['company_id'])){
foreach ($value['company_id'] as $k => $v) {
$iNewSort = empty($aSort[$v]) ? $v : $aSort[$v];
$iOrgnId = empty($aCompanyData[$iNewSort]['organ_id']) ? 0 : $aCompanyData[$iNewSort]['organ_id'];
if(empty($iOrgnId)){
continue;
}
//获取
$sOrganName = empty($aCompanyData[$iNewSort]['organ_name']) ? 0 : $aCompanyData[$iNewSort]['organ_name'];
if(!empty($sOrganName)){
$sOrganName = str_replace([', ',''], ',', $sOrganName);
$aOrganName = explode(',',$sOrganName);
$aCountry[] = empty($aOrganName) ? '' : strtolower(end($aOrganName));
}
$aAuthorOrgn[] = ['article_id' => $iArticleId,'organ_id' => $iOrgnId,'art_aut_id' => $value['firstname']];
}
}
//处理城市
$aCountry = empty($aCountry) ? [] : array_unique($aCountry);
$value['country'] = '';
if(count($aCountry) == 1){
$value['country'] = empty($aCountry[0]) ? '' : ucfirst($aCountry[0]);
}
if(count($aCountry) > 1){
// 1. 找到目标值的键名
$key = array_search('china', $aCountry);
// 2. 确认找到后删除(避免键名为 0 时被误判为 false
if ($key !== false) {
unset($aCountry[$key]);
}
$aCountry = empty($aCountry) ? [] : array_values($aCountry);
$value['country'] = empty($aCountry[0]) ? '' : ucfirst($aCountry[0]);
}
//已添加
if(!empty($value['firstname']) && in_array($value['firstname'], $aAuthorList)){
continue;
}
$value['email'] = empty($aCorresponding[$value['name']]['email']) ? '' : $aCorresponding[$value['name']]['email'];
$value['article_id'] = $iArticleId;
unset($value['name'],$value['company_id']);
$iMaxSort++;
$value['sort'] = $iMaxSort;
$value['country'] = empty($value['country']) ? '' : ucfirst(str_replace(['Pr '], '', $value['country']));
$aAuthorData[] = $value;
}
}
if(!empty($aAuthorData)){
$author_result = DB::name('article_author')->insertAll($aAuthorData);
if(empty($author_result)){
return json_encode(['status' => 3,'msg' => 'Adding article author failed']);
}
$aWhere = ['article_id' => $iArticleId,'state' => 0];
$aAuthorResult = DB::name('article_author')->where($aWhere)->select();
}
//作者所属机构
$aAuthorOrgnInsert = [];
if(!empty($aAuthorOrgn)){
//查询文章作者所属机构
$aWhere = ['article_id' => $iArticleId,'state' => 0];
$aAuthorOrganList = Db::name('article_author_organ')->field('art_aut_id,organ_id')->where($aWhere)->select();
$aAuthorOrganIdList = [];
if(!empty($aAuthorOrganList)){
foreach ($aAuthorOrganList as $key => $value) {
$aAuthorOrganIdList[$value['art_aut_id']][] = $value['organ_id'];
}
}
$aAuthorName = empty($aAuthorResult) ? [] : array_column($aAuthorResult, 'art_aut_id','firstname');
foreach ($aAuthorOrgn as $key => $value) {
if(empty($value['art_aut_id'])){
continue;
}
$iAuthorId = empty($aAuthorName[$value['art_aut_id']]) ? 0 : $aAuthorName[$value['art_aut_id']];
if(empty($iAuthorId)){
continue;
}
if(!empty($aAuthorOrganIdList[$iAuthorId]) && in_array($value['organ_id'], $aAuthorOrganIdList[$iAuthorId])){
continue;
}
$value['art_aut_id'] = $iAuthorId;
$value['create_time'] = time();
$aAuthorOrgnInsert[] = $value;
}
}
if(!empty($aAuthorOrgnInsert)){
$author_orgn_result = DB::name('article_author_organ')->insertAll($aAuthorOrgnInsert);
if(empty($author_orgn_result)){
return json_encode(['status' => 3,'msg' => 'Adding article author orgn failed']);
}
}
//处理上传文件
if(!empty($aInsert['manuscirpt_url']) && !empty($iUserId)){
$aInsertFile['article_id'] = $iArticleId;
$aInsertFile['user_id'] = $iUserId;
$aInsertFile['username'] = empty($aUser['account']) ? '' : $aUser['account'];
$aInsertFile['file_url'] = $aInsert['manuscirpt_url'];
$aInsertFile['type_name'] = 'manuscirpt';
$aInsertFile['ctime'] = time();
$iFileId = Db::name('article_file')->insertGetId($aInsertFile);
}
Db::commit();
$aInsert['article_id'] = $iArticleId;
return json_encode(['status' => 1,'msg' => 'Successfully added article','article' => $aInsert]);
}
}