diff --git a/.env b/.env index aa55d762..c7f7c3d5 100644 --- a/.env +++ b/.env @@ -66,6 +66,8 @@ static_root="/home/wwwroot/api.tmrjournals.com/public" [journal] ;官网服务器地址 base_url = http://journalapi.tmrjournals.com/public/index.php +;base_url =http://192.168.110.131/journal/public/index.php +journal_image_url = http://192.168.110.131/journal/public/ [gpt] api_key = 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 diff --git a/application/api/controller/Article.php b/application/api/controller/Article.php index 70371bb7..6272c181 100644 --- a/application/api/controller/Article.php +++ b/application/api/controller/Article.php @@ -10,6 +10,7 @@ use PhpOffice\PhpWord\IOFactory; use app\common\OpenAi; use app\common\CrossrefService; use app\common\PubmedService; +use app\common\ArticleParserService; /** * @title 文章接口 @@ -1181,6 +1182,213 @@ class Article extends Base return jsonSuccess($re); } + /** + * @title 判断作者是否已按审稿意见修改(逐条核对最终版) + * @description 异步检测:立即返回检测中,后台执行 LLM;请轮询 getRevisionCommentCheckResult + * @param name:article_id type:int require:1 desc:文章id + * @param name:type type:string require:0 desc:不传=综合(意见+回复信+修回稿);manuscript/response/comment 为单侧触发 + * @param name:force type:int require:0 desc:1强制重跑,忽略缓存 + * @param name:mode type:string require:0 desc:full=逐条核对(默认) quick=仅时间门 + * + * @url /api/Article/checkAuthorRevisedByReview + * @method POST + */ + public function checkAuthorRevisedByReview() + { + $data = $this->request->post(); + $rule = new Validate([ + 'article_id' => 'require|number', + ]); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + + $articleId = intval($data['article_id']); + $mode = strtolower(trim((string)($data['mode'] ?? 'full'))); + $force = !empty($data['force']) ? 1 : 0; + + try { + $service = new \app\common\service\RevisionCommentMatchService(); + $checkType = $service->normalizeCheckType($data['type'] ?? ''); + if ($mode === 'quick') { + $quick = $service->quickCheck($articleId); + $quick['check_type'] = $checkType; + return jsonSuccess($quick); + } + + $result = $service->check($articleId, [ + 'force' => $force, + 'use_cache' => $force ? 0 : 1, + 'type' => $checkType, + 'async' => 1, + ]); + $result['mode'] = 'full'; + return jsonSuccess($result); + } catch (\Exception $e) { + return jsonError($e->getMessage()); + } + } + + /** + * @title 获取/触发作者按审稿意见修改的逐条核对结果 + * @description 有成功结果直接返回;排队/检测中返回 status=1;无任务则投递 RabbitMQ。type 不传=综合检测(意见+回复信+修回稿) + * @param name:article_id type:int require:1 desc:文章id + * @param name:type type:string require:0 desc:不传=综合;manuscript/response/comment + * @param name:force type:int require:0 desc:1强制重新检测,忽略缓存 + * @url /api/Article/getRevisionCommentCheckResult + * @method POST + */ + public function getRevisionCommentCheckResult() + { + $data = $this->request->post(); + $rule = new Validate([ + 'article_id' => 'require|number', + ]); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + $articleId = intval($data['article_id']); + $force = !empty($data['force']) ? 1 : 0; + + try { + $service = new \app\common\service\RevisionCommentMatchService(); + $checkType = $service->normalizeCheckType($data['type'] ?? ''); + + if (!$force) { + // 未传 force:始终返回该文章该类型「最新一条」检测记录的进度/结果 + $latest = $service->getLatestJob($articleId, $checkType, null); + if (!empty($latest)) { + $latestStatus = intval($latest['status'] ?? 0); + if ($latestStatus === \app\common\service\RevisionCommentMatchService::STATUS_SUCCESS) { + $detail = $service->getCheckDetail(intval($latest['id'])); + $detail['mode'] = 'full'; + return jsonSuccess($detail); + } + + $meta = json_decode((string)($latest['result_json'] ?? ''), true); + if (!is_array($meta)) { + $meta = []; + } + if ($latestStatus === \app\common\service\RevisionCommentMatchService::STATUS_FAIL) { + return jsonSuccess([ + 'article_id' => $articleId, + 'check_id' => intval($latest['id']), + 'check_no' => (string)($latest['check_no'] ?? ''), + 'check_type' => $checkType, + 'status' => \app\common\service\RevisionCommentMatchService::STATUS_FAIL, + 'status_text' => '检测失败', + 'message' => $meta['message'] ?? ($latest['error_msg'] ?? '检测失败'), + 'error_msg' => $latest['error_msg'] ?? '', + 'mode' => 'full', + ]); + } + + return jsonSuccess([ + 'article_id' => $articleId, + 'check_id' => intval($latest['id']), + 'check_no' => (string)($latest['check_no'] ?? ''), + 'check_type' => $checkType, + 'check_type_label' => $meta['check_type_label'] ?? '', + 'status' => \app\common\service\RevisionCommentMatchService::STATUS_RUNNING, + 'status_text' => '检测中', + 'message' => $meta['message'] ?? '检测进行中,请稍后轮询', + 'mode' => 'full', + ]); + } + } + + $result = $service->check($articleId, [ + 'force' => $force ? 1 : 0, + 'use_cache' => $force ? 0 : 1, + 'type' => $checkType, + 'async' => 1, + ]); + $result['mode'] = 'full'; + return jsonSuccess($result); + } catch (\Exception $e) { + return jsonError($e->getMessage()); + } + } + + /** + * @title 审稿意见修改检测可视化报告页 + * @description 渲染检测报告页面;页面内可触发检测并实时查看逐条结果 + * @url /api/Article/revisionCommentCheckReport + * @method GET + */ + public function revisionCommentCheckReport() + { + $apiGetResult = '/index.php/api/Article/getRevisionCommentCheckResult'; + $apiGetDetail = '/index.php/api/Article/getRevisionCommentCheckDetail'; + $this->assign([ + 'api_get_result' => $apiGetResult, + 'api_get_detail' => $apiGetDetail, + 'init_article_id' => intval($this->request->param('article_id', 0)), + 'init_check_id' => intval($this->request->param('check_id', 0)), + 'init_type' => (string)$this->request->param('type', 'all'), + ]); + return $this->fetch('article/revision_comment_check_report'); + } + + /** + * @title 审稿意见修改检测记录列表 + * @description 仅返回某篇文章最新一条检测记录摘要(含明细条数);详情用 getRevisionCommentCheckDetail + * @param name:article_id type:int require:1 desc:文章id + * @param name:type type:string require:0 desc:按检测类型过滤,不传=全部 + * @param name:page type:int require:0 desc:页码,默认1 + * @param name:page_size type:int require:0 desc:每页条数,默认20 + * @url /api/Article/listRevisionCommentCheck + * @method POST + */ + public function listRevisionCommentCheck() + { + $data = $this->request->post(); + $rule = new Validate([ + 'article_id' => 'require|number', + ]); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + try { + $service = new \app\common\service\RevisionCommentMatchService(); + $list = $service->listChecks( + intval($data['article_id']), + intval($data['page'] ?? 1), + intval($data['page_size'] ?? ($data['pageSize'] ?? 20)), + (string)($data['type'] ?? '') + ); + return jsonSuccess($list); + } catch (\Exception $e) { + return jsonError($e->getMessage()); + } + } + + /** + * @title 审稿意见修改检测记录详情(含每条结果) + * @description 按 check_id 返回完整检测结果;items 来自明细表,逐条含意见文本、质量评估、是否落实、证据与理由 + * @param name:check_id type:int require:1 desc:检测记录id + * @url /api/Article/getRevisionCommentCheckDetail + * @method POST + */ + public function getRevisionCommentCheckDetail() + { + $data = $this->request->post(); + $rule = new Validate([ + 'check_id' => 'require|number', + ]); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + try { + $service = new \app\common\service\RevisionCommentMatchService(); + $detail = $service->getCheckDetail(intval($data['check_id'])); + $detail['mode'] = 'full'; + return jsonSuccess($detail); + } catch (\Exception $e) { + return jsonError($e->getMessage()); + } + } + /**获取用户所投的文章 * @return \think\response\Json|void @@ -3221,11 +3429,34 @@ class Article extends Base } /** + * 获取文章作者信息 + * 支持 article_id(查库)或 file_url(解析稿件,不读写库) * @return void */ public function getAuthors() { $data = $this->request->post(); + + // 从稿件解析作者(对齐 Contribute 读取逻辑,不入库) + if (!empty($data['file_url'])) { + $sFileUrl = rtrim(ROOT_PATH, '/') . '/public/' . ltrim(ltrim($data['file_url'], '/'), 'public'); + if (!file_exists($sFileUrl)) { + return jsonError('The uploaded file does not exist'); + } + if (!is_readable($sFileUrl)) { + return jsonError('The uploaded file is unreadable'); + } + + $aDealData = json_decode(ArticleParserService::uploadAndParse($sFileUrl), true); + $iStatus = empty($aDealData['status']) ? 0 : $aDealData['status']; + if ($iStatus != 1) { + return jsonError(empty($aDealData['msg']) ? 'Content parsing failed' : $aDealData['msg']); + } + $aParseData = empty($aDealData['data']) ? [] : $aDealData['data']; + $re['authors'] = $this->buildAuthorsFromParseData($aParseData); + return jsonSuccess($re); + } + $rule = new Validate([ "article_id" => "require" ]); @@ -3946,6 +4177,12 @@ class Article extends Base if (isset($data['is_agree'])) { $update_l['is_agree'] = $data['is_agree']; } + if (isset($data['special_num'])) { + $update_l['special_num'] = intval($data['special_num']); + } + if (isset($data['special_title'])) { + $update_l['special_title'] = trim((string)$data['special_title']); + } if(!empty($sArticleSn)){ $update_l['accept_sn'] = $sArticleSn; @@ -5737,7 +5974,9 @@ class Article extends Base // if(empty($iJournalId)){ // return json_encode(['status' => 2,'msg' => 'Please select a journal']); // } - $aArticleInsert = ['journal_id' => $iJournalId,'title' => $sTitle,'state' => -1,'user_id' => $iUserId]; + $aArticleInsert = ['journal_id' => $iJournalId,'title' => $sTitle,'abstrart' => '','use_ai_explain' => '','state' => -1,'user_id' => $iUserId]; + if(isset($aParam['special_num']))$aArticleInsert['special_num'] = $aParam['special_num']; + if(isset($aParam['special_title']))$aArticleInsert['special_title'] = $aParam['special_title']; $aArticleInsert['is_use_ai'] = 3; $aArticleInsert['is_figure_copyright'] = 3; // $aArticleInsert['is_transfer'] = 3; @@ -5785,7 +6024,7 @@ class Article extends Base if($becomeRev == false){ $aParam['is_become_reviewer'] = 2; } - $aField = ['is_use_ai','use_ai_explain','is_figure_copyright','is_become_reviewer','approval','approval_file','approval_content','code','is_become_reviewer','is_agree','title','abstrart','keywords','topics','fund','type','journal_id'];//,'title','abstrart','keywords','topics','fund','type','is_transfer', + $aField = ['is_use_ai','use_ai_explain','is_figure_copyright','is_become_reviewer','approval','approval_file','approval_content','code','is_become_reviewer','is_agree','title','abstrart','keywords','topics','fund','type','journal_id','special_num','special_title'];//,'title','abstrart','keywords','topics','fund','type','is_transfer', $sMsg = ''; $iIsUpdate = 1; foreach ($aField as $key => $value) { @@ -6412,9 +6651,65 @@ class Article extends Base // $update_result = Db::name('article')->where($aWhere)->limit(1)->update($aArticleUpdate); // } //操作日志 - $aLog = ['article_id' => $iArticleId,'user_id' => $iUserId,'type' => 7,'create_time' => time(),'content' => $sUserAccount . ':Operating the article copyright statement','is_view' => 1]; + $aLog = ['article_id' => $iArticleId,'user_id' => $iUserId,'type' => 7,'create_time' => time(),'update_time' => time(),'content' => $sUserAccount . ':Operating the article copyright statement','is_view' => 1]; Db::name('user_act_log')->insert($aLog); Db::commit(); return json_encode(['status' => 1,'msg' => 'success']); } + /** + * 修改未发表文章对应专刊(仅更新 tougao 本地 t_article) + * special_num = journal_special_id,special_title 由前端一并传入 + */ + public function changeArticleSpecialForSubmit() + { + $data = $this->request->post(); + $rule = new Validate([ + 'article_id' => 'require', + /*'journal_special_id' => 'require', + 'special_title' => 'require',*/ + ]); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + + $article_info = $this->article_obj->where('article_id', $data['article_id'])->find(); + if (!$article_info) { + return jsonError('article not found'); + } + + $journalSpecialRaw = isset($data['journal_special_id'])&&$data['journal_special_id'] ? trim((string)$data['journal_special_id']) : ''; + // journal_special_id 为空:清空专刊信息 + if ($journalSpecialRaw === '') { + $update = [ + 'special_num' => 0, + 'special_title' => '', + ]; + $this->article_obj->where('article_id', $data['article_id'])->update($update); + + return jsonSuccess([ + 'article_id' => intval($data['article_id']), + 'special_num' => 0, + 'special_title' => '', + ]); + } + + // journal_special_id 有值:special_title 必填 + $specialTitle = isset($data['special_title']) ? trim((string)$data['special_title']) : ''; + if ($specialTitle === '') { + return jsonError('special_title is required'); + } + + $specialId = intval($journalSpecialRaw); + $update = [ + 'special_num' => $specialId, + 'special_title' => $specialTitle, + ]; + $this->article_obj->where('article_id', $data['article_id'])->update($update); + + return jsonSuccess([ + 'article_id' => intval($data['article_id']), + 'special_num' => $specialId, + 'special_title' => $update['special_title'], + ]); + } } diff --git a/application/api/controller/References.php b/application/api/controller/References.php index 97ae255e..71695d70 100644 --- a/application/api/controller/References.php +++ b/application/api/controller/References.php @@ -25,6 +25,21 @@ class References extends Base public function __construct(\think\Request $request = null) { parent::__construct($request); } + + /** + * 参考文献堆叠统计可视化页 + * 访问:/api/References/index?p_article_id=xxx + */ + public function index() + { + $pArticleId = intval($this->request->param('p_article_id', 0)); + $this->assign([ + 'api_stats_js' => json_encode((string) url('api/References/referenceStackingStats'), JSON_UNESCAPED_SLASHES), + 'init_p_article_id' => $pArticleId, + ]); + return $this->fetch('references/index'); + } + //OPENAI token private $sApiKey = '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'; //OPENAI URL @@ -1847,6 +1862,8 @@ class References extends Base /** * 参考文献引用堆叠统计(同作者>15%、同刊>20%、自引>10%,实时计算) + * 仅读本地库,不实时请求 Crossref/OpenAlex。 + * 额外返回 author_data_issues:未获取到作者 / 作者信息不全的参考文献列表 * * POST/GET: p_article_id(必填) */ @@ -1871,6 +1888,66 @@ class References extends Base } } + /** + * 参考文献堆叠摘要(展示用) + * 返回:同作者名称+比例、同刊名称+比例、自引比例(仅超阈值的同作者/同刊) + * + * POST/GET: p_article_id(必填) + */ + public function referenceStackingSummary() + { + $aParam = $this->request->post(); + if (empty($aParam)) { + $aParam = $this->request->param(); + } + + $iPArticleId = empty($aParam['p_article_id']) ? 0 : intval($aParam['p_article_id']); + if ($iPArticleId <= 0) { + return json_encode(array('status' => 2, 'msg' => 'Please select an article')); + } + + try { + $svc = new ReferenceStackingStatsService(); + $result = $svc->getStackingSummaryByPArticleId($iPArticleId); + return jsonSuccess($result); + } catch (\Exception $e) { + return jsonError($e->getMessage()); + } + } + + /** + * 获取单条参考文献作者信息:先 Crossref/OpenAlex(或兜底 refer.author)同步入库,再返回明细 + * + * POST/GET: p_article_id(必填), p_refer_id(必填) + */ + public function referenceReferAuthors() + { + $aParam = $this->request->post(); + if (empty($aParam)) { + $aParam = $this->request->param(); + } + + $iPArticleId = empty($aParam['p_article_id']) ? 0 : intval($aParam['p_article_id']); + $iPReferId = empty($aParam['p_refer_id']) ? 0 : intval($aParam['p_refer_id']); + if ($iPArticleId <= 0) { + return json_encode(array('status' => 2, 'msg' => 'Please select an article')); + } + if ($iPReferId <= 0) { + return jsonError('p_refer_id is required'); + } + + try { + $svc = new ReferenceReferAuthorService(); + $result = $svc->fetchAuthorsByPReferId($iPArticleId, $iPReferId); + if (empty($result['refer'])) { + return jsonError('Reference not found'); + } + return jsonSuccess($result); + } catch (\Exception $e) { + return jsonError($e->getMessage()); + } + } + /** * 测试:同步参考文献作者明细到 t_production_article_refer_author * POST/GET: p_article_id(必填), p_refer_id(可选,仅同步单条), include_authors(可选,默认 0;1 返回作者明细), sleep_ms(可选,默认 120) diff --git a/application/api/controller/Special.php b/application/api/controller/Special.php index e80110ce..6685d75c 100644 --- a/application/api/controller/Special.php +++ b/application/api/controller/Special.php @@ -1009,4 +1009,1225 @@ class Special extends Controller $aResult = object_to_array(json_decode(myPost1($sUrl,$aParam))); return $aResult; } + + /** + * 获取期刊的专刊列表 + */ + public function getSpecialList(){ + $data = $this->request->post(); + $rule = new Validate([ + 'journal_id' => 'require' + ]); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + // 分页参数:pageIndex=页码,pageSize=每页条数 + $iPage = isset($data['pageIndex']) ? intval($data['pageIndex']) : 1; + $iSize = isset($data['pageSize']) ? intval($data['pageSize']) : 15; + if ($iPage < 1) { + $iPage = 1; + } + if ($iSize < 1) { + $iSize = 15; + } + $journal_id = $data['journal_id']; + $base_url = Env::get('journal.base_url'); + $journal_image_url = Env::get('journal.journal_image_url'); + $state = isset($data['state'])?$data['state']:-1; + $gwData = [ + 'journal_id' => $journal_id, + 'pageIndex' => $iPage, + 'pageSize' => $iSize, + 'state' => $state, + ]; + + $res = object_to_array(json_decode(myPost($base_url . "/master/Special/getSpecialList", $gwData))); + // master 接口通常直接返回 specials/count,也兼容 data 包裹结构 + $specials = isset($res['specials']) ? $res['specials'] : (isset($res['data']['specials']) ? $res['data']['specials'] : []); + foreach ($specials as $k => $v) { + unset($specials[$k]['abstract']); + $specials[$k]['icon'] = $specials[$k]['icon']?$journal_image_url."specialIcon/".$specials[$k]['icon']:""; + } + $re['specials'] = $specials; + $re['count'] = isset($res['data']['count']) ? intval($res['data']['count']) : 0; + $re['pageIndex'] = $iPage; + $re['pageSize'] = $iSize; + return jsonSuccess($re); + } + /** + * 获取期刊的专刊列表 + */ + public function getSpecialDetail(){ + $data = $this->request->post(); + $rule = new Validate([ + 'journal_special_id' => 'require' + ]); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + + $journal_special_id = $data['journal_special_id']; + $base_url = Env::get('journal.base_url'); + $res = object_to_array(json_decode(myPost($base_url . "/master/Special/getSpecialDetail", ['journal_special_id' => $journal_special_id]))); + $specialInfo = isset($res['data'])?$res['data']:[]; + $editorsGw = isset($specialInfo['editors']) && is_array($specialInfo['editors']) ? $specialInfo['editors'] : []; + // 客座编辑信息以投稿系统本地数据为准 + $localEditors = $this->user_to_special_obj + ->alias('uts') + ->field('u.user_id,u.account,u.email,u.realname,u.icon,u.orcid,u.g_website,r.firstname,r.lastname,r.address,r.introduction,r.website,r.interests,r.company,r.department,r.country') + ->join('t_user u', 'u.user_id = uts.user_id', 'left') + ->join('t_user_reviewer_info r', 'r.reviewer_id = u.user_id AND r.state = 0', 'left') + ->where('uts.special_id', intval($journal_special_id)) + ->where('uts.uts_state', 0) + ->where('u.state', 0) + ->select(); + $specialInfo['editors'] = empty($localEditors) ? [] : $localEditors; + + // editors_gw:去掉与本地 editors 邮箱相同的项,并去除邮箱字段 + $localEmails = []; + foreach ($specialInfo['editors'] as $localEditor) { + $email = trim((string)(is_array($localEditor) ? ($localEditor['email'] ?? '') : ($localEditor['email'] ?? ''))); + if ($email !== '') { + $localEmails[strtolower($email)] = true; + } + } + $filteredGw = []; + foreach ($editorsGw as $gwEditor) { + if (!is_array($gwEditor)) { + continue; + } + $gwEmail = trim((string)($gwEditor['email'] ?? '')); + if ($gwEmail !== '' && isset($localEmails[strtolower($gwEmail)])) { + continue; + } + unset($gwEditor['email']); + $filteredGw[] = $gwEditor; + } + $specialInfo['editors_gw'] = $filteredGw; + unset($specialInfo['applys']); + return jsonSuccess($specialInfo); + } + + /** + * 添加专刊 + * 投稿系统:一专刊最多一个客座编辑(hasEditor=1 时绑定一个 user_id) + * 官网:可存在多个客座编辑,hasEditor=1 时会同步创建/关联一个 + */ + public function addSpecial(){ + $data = $this->request->post(); + $rule = new Validate([ + 'journal_id' => 'require', + 'title' => 'require', + 'abstract' => 'require', +// 'intro' => 'require', + 'keywords' => 'require', + 'deadline' => 'require', + ]); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + + $hasEditor = $this->parseHasEditor(isset($data['hasEditor']) ? $data['hasEditor'] : 0); + if ($hasEditor) { + $editorRule = new Validate([ + 'user_id' => 'require|number', + /*'email' => 'require', + 'first_name' => 'require', + 'last_name' => 'require', + 'address' => 'require', + 'intro' => 'require',*/ + ]); + if (!$editorRule->check($data)) { + return jsonError($editorRule->getError()); + } + } + + $base_url = Env::get('journal.base_url'); + // 注意:官网 journal 的 /api/Special/addSpecial 在 hasEditor=1 时会同时创建专刊+客座编辑+关系 + $remoteData = [ + 'journal_id' => $data['journal_id'], + 'title' => $data['title'], + // 专刊封面(由前端先调 up_icon_file 拿到路径后传入) + 'specialIcon' => isset($data['specialIcon']) ? trim((string)$data['specialIcon']) : '', + 'abstract' => $data['abstract'], + 'intro' => $data['intro'], + 'keywords' => $data['keywords'], + 'deadline' => $data['deadline'], + // 同步 hasEditor 标记到官网;非 1 时不传/不存客座编辑信息 + 'hasEditor' => $hasEditor ? 1 : 0, + ]; + + $userId = 0; + if ($hasEditor) { + $userId = intval($data['user_id']); + // t_user + t_user_reviewer_info 联查,组装官网客座编辑 + $user = $this->user_obj + ->alias('u') + ->field('u.user_id,u.email,u.realname,u.icon,u.orcid,u.g_website,r.company,r.country,r.website,r.field,r.address,r.introduction,r.department,r.firstname,r.lastname,r.interests') + ->join('t_user_reviewer_info r', 'r.reviewer_id = u.user_id AND r.state = 0', 'LEFT') + ->where('u.user_id', $userId) + ->where('u.state', 0) + ->find(); + if (!$user) { + return jsonError('guest editor user not found'); + } + + $realname = trim(isset($user['realname']) ? $user['realname'] : ''); + $nameParts = preg_split('/\s+/', $realname, 2); + $addressParts = array_filter([ + trim(isset($user['company']) ? $user['company'] : ''), + trim(isset($user['department']) ? $user['department'] : ''), + trim(isset($user['country']) ? $user['country'] : ''), + ]); + $specialIcon = ''; + // 客座编辑头像 + $iconPath = trim(isset($user['icon']) ? $user['icon'] : ''); + if ($iconPath !== '') { + $normalized = $this->normalizeJournalSpecialIconPath($iconPath); + if ($normalized !== '') { + $specialIcon = $normalized; + } else { + $iconSync = $this->uploadGuestEditorIconToJournal($iconPath, $base_url); + if (!$iconSync['ok']) { + return jsonError('sync guest editor icon failed: ' . $iconSync['msg']); + } + $specialIcon = $iconSync['special_icon']; + } + } + + $remoteData['email'] = trim(isset($user['email']) ? $user['email'] : ''); + $remoteData['first_name'] = !empty($user['firstname']) ? $user['firstname'] : (isset($nameParts[0]) ? $nameParts[0] : ''); + $remoteData['last_name'] = !empty($user['lastname']) ? $user['lastname'] : (isset($nameParts[1]) ? $nameParts[1] : ''); + $remoteData['address'] = !empty($user['address']) ? $user['address'] : (!empty($addressParts) ? implode(', ', $addressParts) : '-'); + $remoteData['interests'] = isset($user['interests']) ? trim($user['interests']) : ''; + $remoteData['website'] = trim(!empty($user['website']) ? $user['website'] : (isset($user['g_website']) ? $user['g_website'] : '')); + $remoteData['orcid'] = trim(isset($user['orcid']) ? $user['orcid'] : ''); + // 客座编辑头像 + $remoteData['icon'] = $specialIcon; + } + + $raw = myPost($base_url . "/api/Special/addSpecial", $remoteData); + $res = object_to_array(json_decode($raw)); + if (!isset($res['code']) || intval($res['code']) !== 0) { + $msg = isset($res['msg']) ? $res['msg'] : ('add special failed: ' . (is_string($raw) ? $raw : 'remote empty')); + return jsonError($msg); + } + $specialInfo = isset($res['data']) ? $res['data'] : []; + + $specialInfo['guest_editor_bound'] = 0; + $specialInfo['guest_editor_synced'] = 0; + $specialInfo['hasEditor'] = $hasEditor ? 1 : 0; + $specialId = isset($specialInfo['journal_special_id']) ? intval($specialInfo['journal_special_id']) : 0; + if ($specialId <= 0) { + $specialId = $this->resolveCreatedSpecialIdFromJournal($base_url, $data); + } + if ($specialId <= 0) { + return jsonError('add special success but missing journal_special_id'); + } + $specialInfo['special_id'] = $specialId; + + // hasEditor=1:同步 tougao 客座身份(官网侧 addSpecial 已完成编辑与关系写入) + if ($hasEditor) { + $check = $this->user_to_special_obj + ->where('user_id', $userId) + ->where('special_id', $specialId) + ->where('uts_state', 0) + ->find(); + if (!$check) { + $insertId = $this->user_to_special_obj->insertGetId([ + 'user_id' => $userId, + 'special_id' => $specialId, + 'uts_ctime' => time(), + 'uts_state' => 0, + ]); + if (!$insertId) { + return jsonError('bind guest editor to special failed'); + } + } + $specialInfo['guest_editor_synced'] = 1; + $specialInfo['guest_editor_bound'] = 1; + $specialInfo['user_id'] = $userId; + } + + return jsonSuccess($specialInfo); + } + + /** + * 解析前端“是否有客座编辑”参数 + */ + private function parseHasEditor($value) + { + if (is_bool($value)) { + return $value; + } + if (is_numeric($value)) { + return intval($value) === 1; + } + $value = strtolower(trim(strval($value))); + return in_array($value, ['1', 'true', 'yes', 'y'], true); + } + + /** + * 将投稿系统的用户头像上传到官网 specialIcon 目录 + * @return array{ok:bool,msg:string,special_icon:string} + */ + private function uploadGuestEditorIconToJournal($iconPath, $baseUrl) + { + $localPath = $this->resolveGuestEditorIconLocalPath($iconPath); + if ($localPath === '' || !is_file($localPath)) { + return ['ok' => false, 'msg' => 'icon file not found: ' . $iconPath, 'special_icon' => '']; + } + + $mime = function_exists('mime_content_type') ? mime_content_type($localPath) : ''; + if ($mime === '' || $mime === false) { + $mime = 'application/octet-stream'; + } + $originName = basename($localPath); + $url = rtrim($baseUrl, '/') . '/api/Special/up_icon_file'; + $postFields = [ + 'specialIcon' => new \CURLFile($localPath, $mime, $originName), + ]; + + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + $raw = curl_exec($ch); + $err = curl_error($ch); + curl_close($ch); + + if ($raw === false || $raw === '') { + return ['ok' => false, 'msg' => ($err !== '' ? $err : 'upload icon failed'), 'special_icon' => '']; + } + + $res = object_to_array(json_decode($raw)); + if (!isset($res['code']) || intval($res['code']) !== 0) { + $msg = isset($res['msg']) ? $res['msg'] : 'upload icon failed'; + return ['ok' => false, 'msg' => $msg, 'special_icon' => '']; + } + $upurl = trim(isset($res['upurl']) ? $res['upurl'] : ''); + if ($upurl === '') { + return ['ok' => false, 'msg' => 'journal up_icon_file returned empty upurl', 'special_icon' => '']; + } + $upurl = ltrim(str_replace('\\', '/', $upurl), '/'); + return ['ok' => true, 'msg' => 'success', 'special_icon' => 'specialIcon/' . $upurl]; + } + + /** + * 解析投稿系统头像相对路径/URL到本地文件路径 + */ + private function resolveGuestEditorIconLocalPath($iconPath) + { + $iconPath = trim((string)$iconPath); + if ($iconPath === '') { + return ''; + } + + // 绝对路径 + if (preg_match('/^([a-zA-Z]:[\\\\\/]|\/)/', $iconPath) && is_file($iconPath)) { + return $iconPath; + } + + // URL 场景:尝试取 path 映射到本地 ROOT/public + if (preg_match('#^https?://#i', $iconPath)) { + $path = parse_url($iconPath, PHP_URL_PATH); + if (!empty($path)) { + $candidate = rtrim(ROOT_PATH, '/\\') . ltrim($path, '/\\'); + if (is_file($candidate)) { + return $candidate; + } + $candidate = rtrim(ROOT_PATH, '/\\') . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . ltrim($path, '/\\'); + if (is_file($candidate)) { + return $candidate; + } + } + } + + // 相对路径场景:优先按 public 目录解析 + $relative = ltrim($iconPath, '/\\'); + $relativeWithoutPublic = preg_replace('#^public[\/\\\\]#i', '', $relative); + $relativeWithoutUsericon = preg_replace('#^usericon[\/\\\\]#i', '', $relativeWithoutPublic); + $candidates = [ + rtrim(ROOT_PATH, '/\\') . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'usericon' . DIRECTORY_SEPARATOR . $relativeWithoutUsericon, + rtrim(ROOT_PATH, '/\\') . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . $relativeWithoutPublic, + rtrim(ROOT_PATH, '/\\') . DIRECTORY_SEPARATOR . $relative, + ]; + foreach ($candidates as $candidate) { + if (is_file($candidate)) { + return $candidate; + } + } + return ''; + } + + /** + * 若入参已是官网 specialIcon 路径,直接标准化返回;否则返回空字符串 + */ + private function normalizeJournalSpecialIconPath($iconPath) + { + $iconPath = trim((string)$iconPath); + if ($iconPath === '') { + return ''; + } + $path = str_replace('\\', '/', $iconPath); + // 已是相对路径 specialIcon/xxx + if (stripos($path, 'specialIcon/') === 0) { + return 'specialIcon/' . ltrim(substr($path, strlen('specialIcon/')), '/'); + } + // 可能是官网完整 URL,提取 /specialIcon/ 后半段 + if (preg_match('#/specialIcon/(.+)$#i', $path, $m)) { + return 'specialIcon/' . ltrim($m[1], '/'); + } + return ''; + } + + /** + * 官网 addSpecial 成功但未返回 journal_special_id 时,按条件反查 + */ + private function resolveCreatedSpecialIdFromJournal($baseUrl, array $data) + { + $journalId = intval(isset($data['journal_id']) ? $data['journal_id'] : 0); + $title = trim(isset($data['title']) ? $data['title'] : ''); + $deadline = trim(isset($data['deadline']) ? $data['deadline'] : ''); + if ($journalId <= 0 || $title === '') { + return 0; + } + + // 1) 优先走精确查询接口(不受 state=2 过滤影响) + $rawUnique = myPost(rtrim($baseUrl, '/') . '/api/Special/getSpecialIdByUnique', [ + 'journal_id' => $journalId, + 'title' => $title, + 'deadline' => $deadline, + ]); + $resUnique = object_to_array(json_decode($rawUnique)); + if (isset($resUnique['code']) && intval($resUnique['code']) === 0) { + $id = intval(isset($resUnique['data']['journal_special_id']) ? $resUnique['data']['journal_special_id'] : 0); + if ($id > 0) { + return $id; + } + } + + // 2) API 端(仅 state=2) + $raw = myPost(rtrim($baseUrl, '/') . '/api/Special/getSpecialsNew', [ + 'journal_id' => $journalId, + 'pageIndex' => 1, + 'pageSize' => 30, + ]); + $res = object_to_array(json_decode($raw)); + $specials = isset($res['data']['specials']) && is_array($res['data']['specials']) ? $res['data']['specials'] : []; + $id = $this->pickSpecialIdByTitleDeadline($specials, $title, $deadline); + if ($id > 0) { + return $id; + } + + // 3) Master 端(state<>1),覆盖“刚创建未到 state=2”场景 + $rawMaster = myPost(rtrim($baseUrl, '/') . '/master/Special/getSpecialList', [ + 'journal_id' => $journalId, + 'state' => -1, + 'pageIndex' => 1, + 'pageSize' => 50, + ]); + $resMaster = object_to_array(json_decode($rawMaster)); + $specialsMaster = isset($resMaster['specials']) && is_array($resMaster['specials']) ? $resMaster['specials'] : []; + if (empty($specialsMaster) && isset($resMaster['data']['specials']) && is_array($resMaster['data']['specials'])) { + $specialsMaster = $resMaster['data']['specials']; + } + $id = $this->pickSpecialIdByTitleDeadline($specialsMaster, $title, $deadline); + if ($id > 0) { + return $id; + } + + return 0; + } + + /** + * 按 title/deadline 从列表里挑选 journal_special_id + */ + private function pickSpecialIdByTitleDeadline(array $specials, $title, $deadline) + { + if (empty($specials)) { + return 0; + } + foreach ($specials as $sp) { + if (trim((string)($sp['title'] ?? '')) === $title + && trim((string)($sp['deadline'] ?? '')) === $deadline + ) { + return intval($sp['journal_special_id'] ?? 0); + } + } + foreach ($specials as $sp) { + if (trim((string)($sp['title'] ?? '')) === $title) { + return intval($sp['journal_special_id'] ?? 0); + } + } + return 0; + } + /** + * 获取已开通的专刊列表(代理 journal getSpecials,state=2) + * 入参为投稿系统 journal_id:先按 ISSN 换官网 journal_id,再调官网接口 + */ + public function getSpecials() + { + $data = $this->request->post(); + $rule = new Validate([ + 'journal_id' => 'require|number', + ]); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + + $localJournalId = intval($data['journal_id']); + $journalInfo = $this->journal_obj->where('journal_id', $localJournalId)->find(); + if (!$journalInfo) { + return jsonError('journal not found'); + } + $issn = trim(isset($journalInfo['issn']) ? (string)$journalInfo['issn'] : ''); + if ($issn === '') { + return jsonError('journal issn is empty'); + } + + $resolved = $this->resolveSiteJournalIdByIssn($issn); + if (!$resolved['ok']) { + return jsonError($resolved['msg']); + } + $siteJournalId = intval($resolved['journal_id']); + + $base_url = Env::get('journal.base_url'); + $res = object_to_array(json_decode(myPost(rtrim($base_url, '/') . '/api/Special/getSpecials', [ + 'journal_id' => $siteJournalId, + ]))); + if (!isset($res['code']) || intval($res['code']) !== 0) { + $msg = isset($res['msg']) ? $res['msg'] : 'get specials failed'; + return jsonError($msg); + } + + $re = isset($res['data']) ? $res['data'] : []; + if (!isset($re['specials'])) { + $re['specials'] = []; + } + if (!isset($re['is_show'])) { + $re['is_show'] = count($re['specials']) > 0 ? 'true' : 'false'; + } + return jsonSuccess($re); + } + + /** + * 编辑专刊基础信息(代理 journal master/Special/editSpecialBasic) + */ + public function editSpecialBasic() + { + $data = $this->request->post(); + $rule = new Validate([ + 'journal_special_id' => 'require|number', + 'title' => 'require', + 'intro' => 'require', + 'abstract' => 'require', + 'keywords' => 'require', + 'deadline' => 'require', + ]); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + + $base_url = Env::get('journal.base_url'); + $postData = [ + 'journal_special_id' => intval($data['journal_special_id']), + 'title' => trim((string)$data['title']), + 'intro' => trim((string)$data['intro']), + // 专刊封面 + 'icon' => isset($data['specialIcon']) ? trim((string)$data['specialIcon']) : '', + 'abstract' => trim((string)$data['abstract']), + 'keywords' => trim((string)$data['keywords']), + 'deadline' => trim((string)$data['deadline']), + ]; + $raw = myPost($base_url . "/master/Special/editSpecialBasic", $postData); + $res = object_to_array(json_decode($raw)); + if (!isset($res['code']) || intval($res['code']) !== 0) { + $msg = isset($res['msg']) ? $res['msg'] : 'edit special basic failed'; + return jsonError($msg); + } + return jsonSuccess([]); + } + + /** + * 修改专刊信息(同步官网)+ 客座编辑信息(仅更新投稿系统本地) + * 专刊:调用 journal master/Special/editSpecialBasic + * 投稿系统一专刊仅一个客座编辑:hasEditor=1 时若已有其他绑定则先解绑旧编辑(本地+官网),再绑定传入 user_id + * 官网允许一专刊多编辑,替换时只处理被替换的那条关系 + * hasEditor=1 时 user_id 必填;hasEditor 改为 0 时解除本地该专刊全部绑定,并调官网 delAllSpecialToEditor 清空关系 + */ + public function editSpecialAndEditorLocal() + { + $data = $this->request->post(); + $hasEditor = $this->parseHasEditor(isset($data['hasEditor']) ? $data['hasEditor'] : 0); + + $ruleFields = [ + 'journal_special_id' => 'require|number', + 'title' => 'require', +// 'intro' => 'require', + 'abstract' => 'require', + 'keywords' => 'require', + 'deadline' => 'require', + // 必填:漏传会被当成无客座编辑,导致误解绑 + 'hasEditor' => 'require', + ]; + if ($hasEditor) { + $ruleFields['user_id'] = 'require|number'; + } + $rule = new Validate($ruleFields); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + + $specialId = intval($data['journal_special_id']); + $userId = isset($data['user_id']) ? intval($data['user_id']) : 0; + $base_url = Env::get('journal.base_url'); + + // 1) 专刊信息同步官网(含 hasEditor 标记) + $remoteData = [ + 'journal_special_id' => $specialId, + 'title' => trim((string)$data['title']), + 'intro' => trim((string)$data['intro']), + // 专刊封面 + 'icon' => isset($data['specialIcon']) ? trim((string)$data['specialIcon']) : '', + 'abstract' => trim((string)$data['abstract']), + 'keywords' => trim((string)$data['keywords']), + 'deadline' => trim((string)$data['deadline']), + 'hasEditor' => $hasEditor ? 1 : 0, + ]; + $raw = myPost($base_url . "/master/Special/editSpecialBasic", $remoteData); + $res = object_to_array(json_decode($raw)); + if (!isset($res['code']) || intval($res['code']) !== 0) { + $msg = isset($res['msg']) ? $res['msg'] : 'sync special to journal failed'; + return jsonError($msg); + } + + // hasEditor 非 1:专刊无客座编辑,解除官网全部关系 + 本地全部绑定 + if (!$hasEditor) { + $rawDelAll = myPost($base_url . "/master/Special/delAllSpecialToEditor", [ + 'journal_special_id' => $specialId, + ]); + $delAllRes = object_to_array(json_decode($rawDelAll)); + if (!isset($delAllRes['code']) || intval($delAllRes['code']) !== 0) { + $msg = isset($delAllRes['msg']) ? $delAllRes['msg'] : 'delete all remote relations failed'; + return jsonError($msg); + } + + $localUnbound = $this->user_to_special_obj + ->where('special_id', $specialId) + ->where('uts_state', 0) + ->update(['uts_state' => 1]); + if ($localUnbound === false) { + return jsonError('delete local relation failed'); + } + + return jsonSuccess([ + 'journal_special_id' => $specialId, + 'hasEditor' => 0, + 'special_synced' => 1, + 'editor_local_updated' => 0, + 'local_unbound' => intval($localUnbound), + 'remote_unbound' => 1, + ]); + } + + // 2) 客座编辑信息只更新投稿系统本地 + Db::startTrans(); + try { + $firstname = trim((string)$data['firstname']); + $lastname = trim((string)$data['lastname']); + + $userUpdate = [ + 'realname' => trim($firstname . ' ' . $lastname), + ]; + if (isset($data['orcid'])) { + $userUpdate['orcid'] = trim((string)$data['orcid']); + } + if (isset($data['phone'])) { + $userUpdate['phone'] = trim((string)$data['phone']); + } + if (isset($data['icon'])) { + $userUpdate['icon'] = trim((string)$data['icon']); + } + $upUser = $this->user_obj->where('user_id', $userId)->where('state', 0)->update($userUpdate); + if ($upUser === false) { + throw new \Exception('update user failed'); + } + + $reviewerInfo = $this->user_reviewer_info_obj + ->where('reviewer_id', $userId) + ->where('state', 0) + ->find(); + $reviewerUpdate = [ + 'firstname' => $firstname, + 'lastname' => $lastname, + ]; + if (isset($data['website'])) { + $reviewerUpdate['website'] = trim((string)$data['website']); + } + if (isset($data['address'])) { + $reviewerUpdate['address'] = trim((string)$data['address']); + } + if (isset($data['interests'])) { + $reviewerUpdate['interests'] = trim((string)$data['interests']); + } + + if ($reviewerInfo) { + $upReviewer = $this->user_reviewer_info_obj + ->where('reviewer_id', $userId) + ->where('state', 0) + ->update($reviewerUpdate); + if ($upReviewer === false) { + throw new \Exception('update user_reviewer_info failed'); + } + } else { + $reviewerUpdate['reviewer_id'] = $userId; + $reviewerUpdate['test_from'] = 'kzeditor'; + $insReviewer = $this->user_reviewer_info_obj->insert($reviewerUpdate); + if (!$insReviewer) { + throw new \Exception('insert user_reviewer_info failed'); + } + } + + Db::commit(); + } catch (\Exception $e) { + Db::rollback(); + return jsonError($e->getMessage()); + } + + // 3) 一专刊一个客座编辑:先解绑其他本地绑定(并解除对应官网关系),再绑定当前 user_id + $editorReplaced = 0; + $unbindOthers = $this->unbindGuestEditorsOfSpecial($specialId, $base_url, $userId); + if (!$unbindOthers['ok']) { + return jsonError($unbindOthers['msg']); + } + if ($unbindOthers['local_unbound'] > 0) { + $editorReplaced = 1; + } + + $guestBound = 0; + $guestSynced = 0; + $check = $this->user_to_special_obj + ->where('user_id', $userId) + ->where('special_id', $specialId) + ->where('uts_state', 0) + ->find(); + if ($check) { + $guestBound = 1; + } else { + $bindResult = $this->bindGuestEditorToSpecial($specialId, $userId, $base_url); + if (!$bindResult['ok']) { + return jsonError($bindResult['msg']); + } + $guestBound = 1; + $guestSynced = intval($bindResult['remote_bound']); + } + + return jsonSuccess([ + 'journal_special_id' => $specialId, + 'user_id' => $userId, + 'hasEditor' => 1, + 'special_synced' => 1, + 'editor_local_updated' => 1, + 'editor_replaced' => $editorReplaced, + 'local_unbound' => $unbindOthers['local_unbound'], + 'remote_unbound' => $unbindOthers['remote_unbound'], + 'guest_editor_bound' => $guestBound, + 'guest_editor_synced' => $guestSynced, + ]); + } + + /** + * 删除专刊的客座编辑 + * 默认会同步删除官网专刊-编辑关系(可通过 sync_remote=0 关闭) + */ + public function delSpecialEditor() + { + $data = $this->request->post(); + $rule = new Validate([ + 'journal_special_id' => 'require|number', + 'user_id' => 'require|number', + ]); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + + $specialId = intval($data['journal_special_id']); + $userId = intval($data['user_id']); + $syncRemote = !isset($data['sync_remote']) || intval($data['sync_remote']) === 1; + + $bind = $this->user_to_special_obj + ->where('special_id', $specialId) + ->where('user_id', $userId) + ->where('uts_state', 0) + ->find(); + if (!$bind) { + return jsonSuccess([ + 'journal_special_id' => $specialId, + 'user_id' => $userId, + 'local_deleted' => 0, + 'remote_deleted' => 0, + 'msg' => 'already deleted or not bound', + ]); + } + + $remoteDeleted = 0; + if ($syncRemote) { + $base_url = Env::get('journal.base_url'); + $user = $this->user_obj->where('user_id', $userId)->find(); + $email = trim(isset($user['email']) ? $user['email'] : ''); + + if ($email !== '') { + $raw = myPost($base_url . "/master/Special/getSpecialDetail", [ + 'journal_special_id' => $specialId + ]); + $res = object_to_array(json_decode($raw)); + $editors = isset($res['editors']) ? $res['editors'] : (isset($res['data']['editors']) ? $res['data']['editors'] : []); + if (!is_array($editors)) { + $editors = []; + } + + $journalSpecialEditorId = 0; + foreach ($editors as $editor) { + if (trim((string)($editor['email'] ?? '')) === $email) { + $journalSpecialEditorId = intval($editor['journal_special_editor_id'] ?? 0); + if ($journalSpecialEditorId > 0) { + break; + } + } + } + + if ($journalSpecialEditorId > 0) { + $rawDel = myPost($base_url . "/master/Special/delSpecialToEditor", [ + 'journal_special_id' => $specialId, + 'journal_special_editor_id' => $journalSpecialEditorId, + ]); + $delRes = object_to_array(json_decode($rawDel)); + if (!isset($delRes['code']) || intval($delRes['code']) !== 0) { + $msg = isset($delRes['msg']) ? $delRes['msg'] : 'delete remote relation failed'; + return jsonError($msg); + } + $remoteDeleted = 1; + } + } + } + + $up = $this->user_to_special_obj + ->where('special_id', $specialId) + ->where('user_id', $userId) + ->where('uts_state', 0) + ->update(['uts_state' => 1]); + if ($up === false) { + return jsonError('delete local relation failed'); + } + + return jsonSuccess([ + 'journal_special_id' => $specialId, + 'user_id' => $userId, + 'local_deleted' => $up > 0 ? 1 : 0, + 'remote_deleted' => $remoteDeleted, + ]); + } + + /** + * hasEditor=1 且传入 user_id 无有效绑定时: + * 1) 本地把该 user_id 绑定到专刊 + * 2) 同步官网:邮箱已存在则跳过 addSpecialEditor;关系已存在则跳过 addSpecialToEditor + * @return array{ok:bool,msg:string,remote_bound:int} + */ + private function bindGuestEditorToSpecial($specialId, $userId, $baseUrl) + { + $specialId = intval($specialId); + $userId = intval($userId); + $baseUrl = rtrim($baseUrl, '/'); + + $user = $this->user_obj + ->alias('u') + ->field('u.user_id,u.email,u.realname,u.icon,u.orcid,u.g_website,r.company,r.country,r.website,r.address,r.department,r.firstname,r.lastname,r.interests') + ->join('t_user_reviewer_info r', 'r.reviewer_id = u.user_id AND r.state = 0', 'LEFT') + ->where('u.user_id', $userId) + ->where('u.state', 0) + ->find(); + if (!$user) { + return ['ok' => false, 'msg' => 'guest editor user not found', 'remote_bound' => 0]; + } + + $email = trim(isset($user['email']) ? $user['email'] : ''); + if ($email === '') { + return ['ok' => false, 'msg' => 'guest editor email is empty', 'remote_bound' => 0]; + } + + $raw = myPost($baseUrl . "/master/Special/getSpecialDetail", [ + 'journal_special_id' => $specialId, + ]); + $res = object_to_array(json_decode($raw)); + if (!isset($res['code']) || intval($res['code']) !== 0) { + $msg = isset($res['msg']) ? $res['msg'] : 'get special detail failed'; + return ['ok' => false, 'msg' => $msg, 'remote_bound' => 0]; + } + $payload = isset($res['data']) && is_array($res['data']) ? $res['data'] : $res; + $special = isset($payload['special']) ? $payload['special'] : []; + $journalId = intval(isset($special['journal_id']) ? $special['journal_id'] : 0); + if ($journalId <= 0) { + return ['ok' => false, 'msg' => 'journal_id missing from special detail', 'remote_bound' => 0]; + } + + $editors = isset($payload['editors']) ? $payload['editors'] : []; + if (!is_array($editors)) { + $editors = []; + } + + // 专刊下已建立有效关系:按邮箱匹配,跳过 addSpecialEditor / addSpecialToEditor + $journalSpecialEditorId = 0; + $alreadyRelated = false; + $emailLower = strtolower($email); + foreach ($editors as $editor) { + if ($emailLower !== '' && strtolower(trim((string)($editor['email'] ?? ''))) === $emailLower) { + $journalSpecialEditorId = intval($editor['journal_special_editor_id'] ?? 0); + if ($journalSpecialEditorId > 0) { + $alreadyRelated = true; + break; + } + } + } + + if (!$alreadyRelated) { + // 官网是否已有该邮箱编辑档案 + if ($journalSpecialEditorId <= 0) { + $rawFind = myPost($baseUrl . "/master/Special/getSpecialEditorByEmail", [ + 'journal_id' => $journalId, + 'email' => $email, + ]); + $findRes = object_to_array(json_decode($rawFind)); + if (isset($findRes['code']) && intval($findRes['code']) === 0) { + $findData = isset($findRes['data']) && is_array($findRes['data']) ? $findRes['data'] : $findRes; + $journalSpecialEditorId = intval(isset($findData['journal_special_editor_id']) ? $findData['journal_special_editor_id'] : 0); + } + } + + // 邮箱不存在才创建编辑档案 + if ($journalSpecialEditorId <= 0) { + $realname = trim(isset($user['realname']) ? $user['realname'] : ''); + $nameParts = preg_split('/\s+/', $realname, 2); + $firstname = !empty($user['firstname']) ? $user['firstname'] : (isset($nameParts[0]) ? $nameParts[0] : ''); + $lastname = !empty($user['lastname']) ? $user['lastname'] : (isset($nameParts[1]) ? $nameParts[1] : ''); + $addressParts = array_filter([ + trim(isset($user['company']) ? $user['company'] : ''), + trim(isset($user['department']) ? $user['department'] : ''), + trim(isset($user['country']) ? $user['country'] : ''), + ]); + $address = !empty($user['address']) ? $user['address'] : (!empty($addressParts) ? implode(', ', $addressParts) : '-'); + + $specialIcon = ''; + $iconPath = trim(isset($user['icon']) ? $user['icon'] : ''); + if ($iconPath !== '') { + $normalized = $this->normalizeJournalSpecialIconPath($iconPath); + if ($normalized !== '') { + $specialIcon = $normalized; + } else { + $iconSync = $this->uploadGuestEditorIconToJournal($iconPath, $baseUrl); + if ($iconSync['ok']) { + $specialIcon = $iconSync['special_icon']; + } + } + } + + $rawAddEditor = myPost($baseUrl . "/master/Special/addSpecialEditor", [ + 'journal_id' => $journalId, + 'email' => $email, + 'first_name' => $firstname !== '' ? $firstname : '-', + 'last_name' => $lastname !== '' ? $lastname : '-', + 'address' => $address !== '' ? $address : '-', + 'interests' => isset($user['interests']) ? trim($user['interests']) : '', + 'website' => trim(!empty($user['website']) ? $user['website'] : (isset($user['g_website']) ? $user['g_website'] : '')), + 'orcid' => trim(isset($user['orcid']) ? $user['orcid'] : ''), + 'specialIcon' => $specialIcon, + ]); + $addEditorRes = object_to_array(json_decode($rawAddEditor)); + if (!isset($addEditorRes['code']) || intval($addEditorRes['code']) !== 0) { + $msg = isset($addEditorRes['msg']) ? $addEditorRes['msg'] : 'add special editor failed'; + return ['ok' => false, 'msg' => $msg, 'remote_bound' => 0]; + } + $journalSpecialEditorId = intval(isset($addEditorRes['data']['journal_special_editor_id']) + ? $addEditorRes['data']['journal_special_editor_id'] + : (isset($addEditorRes['journal_special_editor_id']) ? $addEditorRes['journal_special_editor_id'] : 0)); + if ($journalSpecialEditorId <= 0) { + return ['ok' => false, 'msg' => 'add special editor success but missing journal_special_editor_id', 'remote_bound' => 0]; + } + } + + // 未建立关系:调用 addSpecialToEditor + $rawBind = myPost($baseUrl . "/master/Special/addSpecialToEditor", [ + 'journal_id' => $journalId, + 'journal_special_id' => $specialId, + 'journal_special_editor_id' => $journalSpecialEditorId, + ]); + $bindRes = object_to_array(json_decode($rawBind)); + if (!isset($bindRes['code']) || intval($bindRes['code']) !== 0) { + $msg = isset($bindRes['msg']) ? $bindRes['msg'] : 'add special to editor failed'; + return ['ok' => false, 'msg' => $msg, 'remote_bound' => 0]; + } + } + + // 本地绑定传入的 user_id:优先恢复软删,否则新建 + $softBind = $this->user_to_special_obj + ->where('user_id', $userId) + ->where('special_id', $specialId) + ->where('uts_state', 1) + ->find(); + if ($softBind) { + $up = $this->user_to_special_obj + ->where('user_id', $userId) + ->where('special_id', $specialId) + ->where('uts_state', 1) + ->update(['uts_state' => 0]); + if ($up === false) { + return ['ok' => false, 'msg' => 'restore local bind failed', 'remote_bound' => $alreadyRelated ? 0 : 1]; + } + } else { + $insertId = $this->user_to_special_obj->insertGetId([ + 'user_id' => $userId, + 'special_id' => $specialId, + 'uts_ctime' => time(), + 'uts_state' => 0, + ]); + if (!$insertId) { + return ['ok' => false, 'msg' => 'bind guest editor to special failed', 'remote_bound' => $alreadyRelated ? 0 : 1]; + } + } + + return ['ok' => true, 'msg' => '', 'remote_bound' => $alreadyRelated ? 0 : 1]; + } + + /** + * 解除专刊下本地客座绑定,并同步官网 delSpecialToEditor + * 投稿系统一专刊仅一个客座编辑;exceptUserId>0 时保留该 user_id(替换场景) + * 官网可有多编辑:只解除投稿侧曾绑定编辑的 delSpecialToEditor,不清理其他官网编辑 + * @return array{ok:bool,msg:string,local_unbound:int,remote_unbound:int} + */ + private function unbindGuestEditorsOfSpecial($specialId, $baseUrl, $exceptUserId = 0) + { + $specialId = intval($specialId); + $baseUrl = rtrim($baseUrl, '/'); + + $query = $this->user_to_special_obj + ->where('special_id', $specialId) + ->where('uts_state', 0); + if (intval($exceptUserId) > 0) { + $query->where('user_id', '<>', intval($exceptUserId)); + } + $oldUserIds = $query->column('user_id'); + if (empty($oldUserIds)) { + return ['ok' => true, 'msg' => '', 'local_unbound' => 0, 'remote_unbound' => 0]; + } + + // 旧 user_id → 邮箱 → 官网编辑关系 + $emails = []; + foreach ($this->user_obj->where('user_id', 'in', $oldUserIds)->column('email') as $email) { + $email = strtolower(trim((string)$email)); + if ($email !== '') { + $emails[$email] = true; + } + } + + $remoteUnbound = 0; + foreach ($this->fetchJournalSpecialEditors($specialId, $baseUrl) as $editor) { + $editorId = intval($editor['journal_special_editor_id'] ?? 0); + if ($editorId <= 0 || !isset($emails[strtolower(trim((string)($editor['email'] ?? '')))])) { + continue; + } + $delRes = object_to_array(json_decode(myPost($baseUrl . "/master/Special/delSpecialToEditor", [ + 'journal_special_id' => $specialId, + 'journal_special_editor_id' => $editorId, + ]))); + if (!isset($delRes['code']) || intval($delRes['code']) !== 0) { + $msg = isset($delRes['msg']) ? $delRes['msg'] : 'delete remote relation failed'; + return ['ok' => false, 'msg' => $msg, 'local_unbound' => 0, 'remote_unbound' => $remoteUnbound]; + } + $remoteUnbound++; + } + + $localUnbound = $this->user_to_special_obj + ->where('special_id', $specialId) + ->where('user_id', 'in', $oldUserIds) + ->where('uts_state', 0) + ->update(['uts_state' => 1]); + if ($localUnbound === false) { + return ['ok' => false, 'msg' => 'delete local relation failed', 'local_unbound' => 0, 'remote_unbound' => $remoteUnbound]; + } + + return [ + 'ok' => true, + 'msg' => '', + 'local_unbound' => intval($localUnbound), + 'remote_unbound' => $remoteUnbound, + ]; + } + + /** + * 获取官网专刊已关联的客座编辑列表 + */ + private function fetchJournalSpecialEditors($specialId, $baseUrl) + { + $res = object_to_array(json_decode(myPost(rtrim($baseUrl, '/') . "/master/Special/getSpecialDetail", [ + 'journal_special_id' => intval($specialId), + ]))); + $payload = isset($res['data']) && is_array($res['data']) ? $res['data'] : $res; + return isset($payload['editors']) && is_array($payload['editors']) ? $payload['editors'] : []; + } + + /** + * 上传专刊封面图(转发至 journal Special/up_icon_file) + * 表单字段名:specialIcon + */ + public function up_icon_file() + { + $file = request()->file('specialIcon'); + if (!$file) { + return jsonError('specialIcon is required'); + } + + $info = $file->getInfo(); + $tmpPath = isset($info['tmp_name']) ? $info['tmp_name'] : ''; + $originName = isset($info['name']) ? $info['name'] : 'specialIcon'; + $mime = isset($info['type']) && $info['type'] !== '' ? $info['type'] : 'application/octet-stream'; + if ($tmpPath === '' || !is_file($tmpPath)) { + return jsonError('upload file invalid'); + } + + $base_url = Env::get('journal.base_url'); + $url = rtrim($base_url, '/') . '/api/Special/up_icon_file'; + $postFields = [ + 'specialIcon' => new \CURLFile(realpath($tmpPath), $mime, $originName), + ]; + + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + $raw = curl_exec($ch); + $err = curl_error($ch); + curl_close($ch); + + if ($raw === false || $raw === '') { + return jsonError($err !== '' ? $err : 'upload to journal failed'); + } + + $res = object_to_array(json_decode($raw)); + if (!isset($res['code']) || intval($res['code']) !== 0) { + $msg = isset($res['msg']) ? $res['msg'] : 'upload icon failed'; + return jsonError($msg); + } + + // 与 journal 返回保持一致,便于前端直接用 upurl + return json([ + 'code' => 0, + 'msg' => 'success', + 'upurl' => isset($res['upurl']) ? $res['upurl'] : '', + ]); + } + + /** + * 更改专刊状态(代理 journal master/Special/changeSpecialState) + * state: 0初始 1拒绝 2通过 + */ + public function changeSpecialState() + { + $data = $this->request->post(); + $rule = new Validate([ + 'journal_special_id' => 'require|number', + 'state' => 'require|number|in:0,1,2', + ]); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + + $specialId = intval($data['journal_special_id']); + $state = intval($data['state']); + $base_url = Env::get('journal.base_url'); + $raw = myPost($base_url . "/master/Special/changeSpecialState", [ + 'journal_special_id' => $specialId, + 'state' => $state, + ]); + $res = object_to_array(json_decode($raw)); + if (!isset($res['code']) || intval($res['code']) !== 0) { + $msg = isset($res['msg']) ? $res['msg'] : 'change special state failed'; + return jsonError($msg); + } + + return jsonSuccess([ + 'journal_special_id' => $specialId, + 'state' => $state, + ]); + } + + /** + * 根据 ISSN 获取官网期刊 ID(代理 journal master/Special/getJournalIdByIssn) + * 入参兼容 journal_issn / issn;转发官网时使用字段名 issn + */ + public function getJournalIdByIssn() + { + $data = $this->request->post(); + $issn = ''; + if (isset($data['journal_issn'])) { + $issn = trim((string)$data['journal_issn']); + } + if ($issn === '' && isset($data['issn'])) { + $issn = trim((string)$data['issn']); + } + if ($issn === '') { + return jsonError('issn不能为空'); + } + + $resolved = $this->resolveSiteJournalIdByIssn($issn); + if (!$resolved['ok']) { + return jsonError($resolved['msg']); + } + + return jsonSuccess([ + 'journal_issn' => $issn, + 'journal_id' => intval($resolved['journal_id']), + ]); + } + + /** + * 按 ISSN 向官网解析 journal_id + * @return array{ok:bool,msg:string,journal_id:int} + */ + private function resolveSiteJournalIdByIssn($issn) + { + $issn = trim((string)$issn); + if ($issn === '') { + return ['ok' => false, 'msg' => 'issn不能为空', 'journal_id' => 0]; + } + + $base_url = Env::get('journal.base_url'); + $raw = myPost(rtrim($base_url, '/') . '/master/Special/getJournalIdByIssn', [ + 'issn' => $issn, + ]); + $res = object_to_array(json_decode($raw)); + if (!isset($res['code']) || intval($res['code']) !== 0) { + $msg = isset($res['msg']) ? $res['msg'] : ('get journal id by issn failed: ' . (is_string($raw) ? $raw : 'remote empty')); + return ['ok' => false, 'msg' => $msg, 'journal_id' => 0]; + } + + $payload = isset($res['data']) ? $res['data'] : []; + $journalId = 0; + if (is_array($payload)) { + $journalId = intval(isset($payload['journal_id']) ? $payload['journal_id'] : 0); + } elseif (is_numeric($payload)) { + $journalId = intval($payload); + } + if ($journalId <= 0 && isset($res['journal_id'])) { + $journalId = intval($res['journal_id']); + } + if ($journalId <= 0) { + return ['ok' => false, 'msg' => 'journal id not found for issn', 'journal_id' => 0]; + } + + return ['ok' => true, 'msg' => '', 'journal_id' => $journalId]; + } + } diff --git a/application/api/controller/User.php b/application/api/controller/User.php index 1b8edef4..bd4e29db 100644 --- a/application/api/controller/User.php +++ b/application/api/controller/User.php @@ -2081,7 +2081,149 @@ class User extends Base $re['user'] = $user_info; return jsonSuccess($re); } + public function createUserForEditor(){ + $data = $this->request->post(); + $rule = new Validate([ + "email"=>"require", + "address"=>"require", + "firstname"=>"require", + "lastname"=>"require", + "intro"=>"require" + ]); + if(!$rule->check($data)){ + return jsonError($rule->getError()); + } + $account = isset($data['account'])?$data['account']:$data['email']; + $email = $data['email']; + $res_once = $this->user_obj->where("account='$account' or email = '$email'")->find(); + if ($res_once != null) { + return json('existence'); + } + Db::startTrans(); + + $inser_data['account'] = trim($account); + $inser_data['password'] = md5(isset($data['password'])?$data['password']:"123456qwe"); + $inser_data['email'] = $email; + if(isset($data['phone']))$inser_data['phone'] = $data['phone']; + if(isset($data['orcid']))$inser_data['orcid'] = $data['orcid']; + $inser_data['realname'] = $data['firstname']." ".$data['lastname']; + $inser_data['icon'] = $data['icon']; + $inser_data['ctime'] = time(); + $inser_data['openid'] = ""; + $id = $this->user_obj->insertGetId($inser_data); + //存入个人额外信息 + $insert_reviewer['reviewer_id'] = $id; + $insert_reviewer['test_from'] = "kzeditor"; + $insert_reviewer['address'] = $data['address']; + $insert_reviewer['introduction'] = $data['intro']; + $insert_reviewer['firstname'] = $data['firstname']; + $insert_reviewer['lastname'] = $data['lastname']; + if(isset($data['website']))$insert_reviewer['website'] = $data['website']; + if(isset($data['interests']))$insert_reviewer['interests'] = $data['interests']; + $r_res = $this->user_reviewer_info_obj->insert($insert_reviewer); + + if($id&&$r_res){ + Db::commit(); + return jsonSuccess($id); + }else{ + Db::rollback(); + return jsonError("system error"); + } + } + /** + * 修改客座编辑本地资料 + * t_user: orcid、phone、realname(firstname lastname)、icon + * t_user_reviewer_info: address、interests、firstname、lastname、introduction(intro) + */ + public function updateUserForEditor() + { + $data = $this->request->post(); + $rule = new Validate([ + 'user_id' => 'require|number', + 'firstname' => 'require', + 'lastname' => 'require', + 'intro' => 'require', + ]); + if (!$rule->check($data)) { + return jsonError($rule->getError()); + } + + $userId = intval($data['user_id']); + $user = $this->user_obj->where('user_id', $userId)->where('state', 0)->find(); + if (!$user) { + return jsonError('user not found'); + } + + Db::startTrans(); + try { + $firstName = trim((string)$data['firstname']); + $lastName = trim((string)$data['lastname']); + + $userUpdate = [ + 'realname' => trim($firstName . ' ' . $lastName), + ]; + if (isset($data['orcid'])) { + $userUpdate['orcid'] = trim((string)$data['orcid']); + } + if (isset($data['phone'])) { + $userUpdate['phone'] = trim((string)$data['phone']); + } + if (isset($data['icon'])) { + $userUpdate['icon'] = trim((string)$data['icon']); + } + $upUser = $this->user_obj->where('user_id', $userId)->update($userUpdate); + if ($upUser === false) { + throw new \Exception('update t_user failed'); + } + + $reviewerInfo = $this->user_reviewer_info_obj + ->where('reviewer_id', $userId) + ->where('state', 0) + ->find(); + $reviewerUpdate = [ + 'firstname' => $firstName, + 'lastname' => $lastName, + ]; + if (isset($data['address'])) { + $reviewerUpdate['address'] = trim((string)$data['address']); + } + if (isset($data['website'])) { + $reviewerUpdate['website'] = trim((string)$data['website']); + } + if (isset($data['interests'])) { + $reviewerUpdate['interests'] = trim((string)$data['interests']); + } + $reviewerUpdate['introduction'] = trim((string)$data['intro']); + + if ($reviewerInfo) { + $upReviewer = $this->user_reviewer_info_obj + ->where('reviewer_id', $userId) + ->where('state', 0) + ->update($reviewerUpdate); + if ($upReviewer === false) { + throw new \Exception('update t_user_reviewer_info failed'); + } + } else { + $reviewerUpdate['reviewer_id'] = $userId; + $reviewerUpdate['test_from'] = 'kzeditor'; + $insReviewer = $this->user_reviewer_info_obj->insert($reviewerUpdate); + if (!$insReviewer) { + throw new \Exception('insert t_user_reviewer_info failed'); + } + } + + Db::commit(); + return jsonSuccess([ + 'user_id' => $userId, + 'updated' => 1, + ]); + } catch (\Exception $e) { + Db::rollback(); + return jsonError($e->getMessage()); + } + } + /** * 注册功能 */ @@ -3370,4 +3512,17 @@ class User extends Base } return implode("\n", $lines); } + + public function up_editorIcon_file() + { + $file = request()->file('icon'); + if ($file) { + $info = $file->move(ROOT_PATH . 'public' . DS . 'usericon'); + if ($info) { + return json(['code' => 0, 'upurl' => str_replace("\\", "/", $info->getSaveName())]); + } else { + return json(['code' => 1, 'msg' => $file->getError()]); + } + } + } } diff --git a/application/api/view/references/index.html b/application/api/view/references/index.html new file mode 100644 index 00000000..6ce5cb13 --- /dev/null +++ b/application/api/view/references/index.html @@ -0,0 +1,674 @@ + + +
+ + +