123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import {
- record_insert,
- record_update,
- isType,
- type_insert,
- getRecordInfoById,
- addFileByRecordId,
- getFileByRecordId,
- getRecordsInfoByTime,
- getRecordsInfoByMonth,
- getTypesById,
- delFileByRecordId,
- delByRecordId,
- } from "#db";
- import { shanghaiTime, shanghaiTimeFormat } from "#utils";
- export async function getTypeInfoFn({ userInfo, book_id, type }) {
- console.log(1919191919, userInfo, book_id, type )
- if (!type) {
- return "";
- }
- // type 是否存在重复项,有就返回id,没有就新增 再返回id
- const isAddType = await isType({
- book_id,
- author_id: userInfo.user_id,
- type,
- });
- let typeInfo = {};
- if (!isAddType) {
- await type_insert({
- book_id,
- author_id: userInfo.user_id,
- type,
- create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
- update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
- });
- typeInfo = await isType({
- book_id,
- author_id: userInfo.user_id,
- type,
- });
- } else {
- typeInfo = { ...isAddType };
- }
- return Promise.resolve(typeInfo.id);
- }
- export async function setFilesByRecord(params) {
- const {files = [], more_id, userInfo, book_id} = params
- if(!files.length) return
- await Promise.all(
- files.map((file_id) =>
- addFileByRecordId({
- file_id,
- record_id: more_id,
- book_id,
- author_id: userInfo.user_id,
- create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
- update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
- })
- )
- );
- }
- export async function setFilesById(params) {
- const {record_id, userInfo, book_id, files = []} = params
- const getAllFiles = await getFileByRecordId(record_id);
- const getAllFilesIds = getAllFiles.map((elm) => elm.file_id);
- const dellFilesInRecordFiles = getAllFiles.filter(
- (elm) => files.indexOf(elm.file_id) < 0
- );
- const needAddFiles = files.filter(
- (file_id) => getAllFilesIds.indexOf(file_id) < 0
- );
- if (dellFilesInRecordFiles.length) {
- await Promise.all(
- dellFilesInRecordFiles.map((elm) =>
- delFileByRecordId(record_id, elm.file_id)
- )
- );
- }
- if (needAddFiles.length) {
- // 更新附件信息
- await Promise.all(
- needAddFiles.map((file_id) =>
- addFileByRecordId({
- file_id,
- record_id,
- book_id,
- author_id: userInfo.user_id,
- create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
- update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
- })
- )
- );
- }
- }
- export function getFileUrl(req,elm) {
- // 获取请求的来源域名
- const host = req.headers["host"]; // 主机名 + 端口
- const origin = req.headers["referer"]; // 请求的来源域(适用于跨域)
- if (`${origin}`.indexOf("3032") > -1 || `${host}`.indexOf("3000") > -1) {
- return "http://localhost:3000" + `/api_files_${elm.file_id}`;
- }
- if (`${origin}`.indexOf("zs_interval") > -1) {
- return `api_files_${elm.file_id}`;
- }
- return `${origin}api_files_${elm.file_id}`;
- }
|