references.ts 5.6 KB
/*
 * @Author: 赵丽婷
 * @Date: 2025-10-21 11:40:50
 * @LastEditors: 赵丽婷
 * @LastEditTime: 2026-01-09 11:11:57
 * @FilePath: \LinkMed\linkmed-vue3\src\api\references.ts
 * @Description:
 * Copyright (c) 2025 by 北京连心医疗科技有限公司, All Rights Reserved.
 */
import { service as http } from "@/utils/request";

// 参考文献项接口
export interface ReferenceItem {
  id: number;
  fileId: number;
  workId: number;
  title: string;
  publicationYear: number;
  authorsText: string;
  doi: string;
  landingUrl: string;
  pdfUrl: string;
  note: string;
  updatedAt: string;
}

// 分页响应接口
export interface ReferenceListResponse {
  items: ReferenceItem[];
  total: number;
  page: number;
  size: number;
}

// 导入搜索结果项接口
export interface ImportSearchResultItem {
  workId: number;
  doi: string;
  title: string;
  publicationYear: number;
  fileLibraryItemId: number | null;
  createdNewWork: boolean;
  createdFileLink: boolean;
  source: string;
  authorsText: string;
  itemType: string;
  venueName: string;
  journalAbbr: string;
  issn: string;
  volume: string;
  issue: string | null;
  pages: string;
  language: string;
  landingUrl: string;
  pdfUrl: string;
}

// 文献详情接口
export interface ReferenceDetail {
  workId: number;
  doi: string;
  title: string;
  publicationYear: number;
  publicationDate: string;
  itemType: string;
  authorsText: string;
  abstractText: string;
  venueName: string;
  journalAbbr: string;
  issn: string;
  volume: string;
  issue: string;
  pages: string;
  language: string;
  landingUrl: string;
  pdfUrl: string;
  source: string;
  identifiersJson: string;
  createdAt: string;
  updatedAt: string;
  note: string;
}

// 导入搜索响应接口
export interface ImportSearchResponse {
  items: ImportSearchResultItem[];
}

// 小工具:拼 query params,过滤 undefined
function qp(obj: any = {}) {
  const params: any = {};
  Object.keys(obj).forEach((k) => {
    const v = obj[k];
    if (v !== undefined && v !== null) params[k] = v;
  });
  return params;
}

/**
 * 分页查询文件文献列表
 * @param fileId 文件ID(必填)
 * @param page 页码
 * @param size 每页数量
 * @param isDraft 是否为草稿文件(默认false)
 *                - true: 从快问快答/深度检索创建的草稿文件
 *                - false: 从知识库打开的普通文件
 */
export const getFileReferences = (
  fileId: string | number,
  page?: number,
  size?: number,
  isDraft: boolean = false,
) => {
  return http.get<ReferenceListResponse>("/references/file-library", {
    params: qp({ fileId, page, size, isDraft }),
  });
};

/**
 * 按标题搜索参考文献
 * @param title 标题关键字
 */
export const searchReferencesByTitle = (title: string) => {
  return http.post<ImportSearchResponse>("/references/import/by-title", {
    title,
  });
};

/**
 * 按DOI搜索参考文献
 * @param doi DOI(支持带/不带 https://doi.org/ 前缀)
 */
export const searchReferencesByDoi = (doi: string) => {
  return http.post<ImportSearchResponse>("/references/import/by-doi", {
    doi,
  });
};

/**
 * 添加参考文献到文件
 * @param fileId 文件ID
 * @param workId 文献ID
 */
export const addReferenceToFile = (fileId: number, workId: number) => {
  return http.post("/references/file-library/add", {
    fileId,
    workId,
  });
};

/**
 * 【深度检索】获取参考文献详情
 * @param workId 文献ID(必填)
 * @returns 返回包含完整文献信息的 ReferenceDetail 对象
 */
export const getReferenceDetail = (workId: number) => {
  return http.get<ReferenceDetail>(`/references/${workId}`);
};

/**
 * 删除文件的参考文献
 * @param fileId 文件ID
 * @param workId 文献ID
 */
export const deleteReferenceFromFile = (
  fileId: number | string,
  workId: number,
) => {
  return http.delete(`/references/file-library/${fileId}/${workId}`);
};

/**
 * 更新文件的参考文献别名
 * @param fileId 文件ID
 * @param workId 文献ID
 * @param note 别名
 */
export const updateReferenceNote = (
  fileId: number | string,
  workId: number,
  note: string,
) => {
  return http.patch("/references/file-library/note", {
    fileId,
    workId,
    note,
  });
};

// 文内引用格式化响应接口
export interface InTextCitationResponse {
  format: "apa" | "ieee";
  texts: string[];
}

/**
 * 获取文内引用格式化文本
 * @param fileId 文件ID
 * @param citationGroups 二维文献ID分组(按出现顺序)
 * @param format 引用格式(apa/ieee)
 * @param outputFormat 输出格式(text/html/markdown)
 */
export const getInTextCitation = (
  fileId: number | string,
  citationGroups: number[][],
  format: "apa" | "ieee",
  outputFormat: "text" | "html" | "markdown",
) => {
  return http.post<InTextCitationResponse>(
    "/references/file-library/in-text-citation",
    {
      fileId,
      citationGroups,
      format,
      outputFormat,
    },
  );
};

// 完整参考文献列表响应接口
export interface FormatCitationResponse {
  format: "apa" | "ieee";
  entries: string[];
  count: number;
}

/**
 * 获取格式化的参考文献列表
 * @param fileId 文件ID
 * @param citationGroups 二维文献ID分组(按出现顺序)
 * @param format 引用格式(apa/ieee)
 * @param outputFormat 输出格式(text/html/markdown)
 */
export const getFormatCitation = (
  fileId: number | string,
  citationGroups: number[][],
  format: "apa" | "ieee",
  outputFormat: "text" | "html" | "markdown",
) => {
  return http.post<FormatCitationResponse>(
    "/references/file-library/format-citation",
    {
      fileId,
      citationGroups,
      format,
      outputFormat,
    },
  );
};