const {handle, handleAll} = require('../tools/handle_cjs'); const d_base = require("../database/d_base"); const d_product = require("../database/d_product"); const d_news = require("../database/d_news"); const codeMap = require("../map/rcodeMap"); const {db_base} = require("../map/dbField"); const {searchHandle} = require("../tools/searchSql"); const {isEmpty, toNumber} = require("../tools/typeTool_cjs"); const config_path = require("../../configs/path.json"); const {mvFile, rmFile} = require("../tools/saveFiles_cjs"); const {getUnixTimeStamp} = require("../tools/time_cjs"); const {filePathToUrl} = require("../tools/filePathTool"); const log = require("../logger").logger("c_base","info"); // 获取启用的轮播列表 async function getEnableCarousel(){ let err,res; [err,res] = await handle(d_base.getCarousel({ state: db_base.carouselState.enable })); // console.log(res); if(err){ return [err,null]; } // 路径转换 res = res.map(item=>{ item.filePath = filePathToUrl(item.fileType,item.filePath); return item; }); return [null,res]; } async function getAllCarousel(){ let err,res; [err,res] = await handle(d_base.getCarousel()); // console.log(res); if(err){ return [err,null]; } // 路径转换 res = res.map(item=>{ item.filePath = filePathToUrl(item.fileType,item.filePath); return item; }); return [null,res]; } async function addCarousel(sort,fileId,type,value){ let err,res; let valueData = {}; let valueShowText = ''; let updateTime = getUnixTimeStamp(); // 根据type判断value if(type !== db_base.carouselType.href){ if(type === db_base.carouselType.production){ [err,res] = await handle(d_product.getProductById(value)); }else if(type === db_base.carouselType.news){ // 获取新闻信息 [err,res] = await handle(d_news.getNewsById(value)); } if(err){ console.log(err); log.info(`[新增轮播] 检索产品信息失败`); return [{eCode: codeMap.ServerError, eMsg: `检索数据异常`},null]; } if(isEmpty(res)){ log.info(`[新增轮播] 无法找到对应的产品或者文章信息`); return [{eCode: codeMap.NotFound, eMsg: `无法找到对应的产品或者文章信息`},null]; } valueData = res[0]; } valueShowText = valueData.title; [err,res] = await handle(d_base.addCarousel( sort,fileId,type,value,valueShowText,updateTime )); if(err){ log.info(`[新增轮播] 导入数据异常`) return [err,null]; } return [null,res]; } async function deleteCarousel(id){ let err,res; [err,res] = await handle(d_base.deleteCarousel(id)); if(err){ log.info(`[删除轮播] 删除数据异常`); return [err,null]; } return [null,res]; } async function updateCarousel(id,updateItems){ let err,res; let rawData = {}; let unixTime = getUnixTimeStamp(); let updateData = {}; let typeKeys = Object.keys(db_base.carouselType); let typeString = ''; let typeShowText = ''; let carouselType = db_base.carouselType.href; // 判断是否需要更新type if (updateItems.value){ // 获取基础type值 if(!isEmpty(updateItems.type)){ typeString = updateItems.type; updateData.type = updateItems.type; }else{ [err,res] = await handle(d_base.getCarouselById(id)); if(err){ log.error(`[修改轮播] 获取轮播基础数据时异常 ${err.message}`); return [{eCode: codeMap.ServerError, eMsg: `获取轮播基础数据异常`},null]; } if(isEmpty(res)){ log.info(`[修改轮播] 无法通过id找到轮播数据`); return [{eCode: codeMap.NotFound, eMsg: `请检测id是否正确`},null]; } rawData = res[0]; typeString = rawData.type; } console.log(toNumber(typeString)) // 需要更新具体值 if(toNumber(typeString) !== db_base.carouselType.href){ if(toNumber(typeString) === db_base.carouselType.production){ typeShowText = '产品'; // 获取产品信息 [err,res] = await handle(d_product.getProductById(updateItems.value)); }else if(toNumber(typeString) === db_base.carouselType.news){ typeShowText = '文章'; // 获取新闻信息 [err,res] = await handle(d_news.getNewsById(updateItems.value)); }else { return [{eCode: codeMap.NotParam, eMsg: `轮播类型异常! carouseType:${typeString} typeof:${typeof typeString}`},null]; } if(err){ console.log(err); log.error(`[修改轮播] 检索${typeShowText}信息失败 ${err.message}}`); return [{eCode: codeMap.ServerError, eMsg: `检索${typeShowText}数据异常`},null]; } if(isEmpty(res)){ log.info(`[修改轮播] 无法找到对应的${typeShowText}数据`); return [{eCode: codeMap.NotFound, eMsg: `无法找到对应的产品或者文章信息`},null]; } updateData.valueShowText = res[0].title; } else { updateData.valueShowText = updateItems.value; } updateData.value = updateItems.value; } if (!isEmpty(updateItems.sort)){ updateData.sort = toNumber(updateItems.sort); } if (!isEmpty(updateItems.state)){ updateData.state = updateItems.state; } if(updateItems.fileId){ updateData.fileId = updateItems.fileId; // todo 检测文件id是否存在 } console.log(updateData); [err,res] = await handle(d_base.updateCarousel(id,updateData,unixTime)); if(err){ console.log(err); log.error(`[修改轮播] 数据更新失败,${err.message}`); return [{eCode: codeMap.SaveError, eMsg: `数据更新失败,请稍后重试`},null]; } return [null,res]; } // 展示块控制函数 /** * 获取启用的展示块列表 * @returns {Promise<*>} */ async function getEnableShowBlocks() { let err, res; [err, res] = await handle(d_base.getShowBlocks()); if (err) { return [err, null]; } // 路径转换 res = res.map(item => { item.filePath = filePathToUrl(item.fileType, item.filePath); return item; }); return [null, res]; } /** * 获取所有展示块 * @returns {Promise<*>} */ async function getAllShowBlocks() { let err, res; [err, res] = await handle(d_base.getShowBlocks()); if (err) { return [err, null]; } // 路径转换 res = res.map(item => { item.filePath = filePathToUrl(item.fileType, item.filePath); return item; }); return [null, res]; } /** * 添加展示块 * @param sort 排序 * @param fileId 文件ID * @param type 类型 * @param value 值 * @param title 标题 * @param subTitle 副标题 * @returns {Promise<*>} */ async function addShowBlock(sort, fileId, type, value, title, subTitle) { let err, res; let valueData = {}; let valueShowText = ''; let updateTime = getUnixTimeStamp(); // 根据type判断value if (type !== db_base.carouselType.href) { if (type === db_base.carouselType.production) { [err, res] = await handle(d_product.getProductById(value)); } else if (type === db_base.carouselType.news) { // 获取新闻信息 [err, res] = await handle(d_news.getNewsById(value)); } if (err) { console.log(err); log.info(`[新增展示块] 检索产品信息失败`); return [{ eCode: codeMap.ServerError, eMsg: `检索数据异常` }, null]; } if (isEmpty(res)) { log.info(`[新增展示块] 无法找到对应的产品或者文章信息`); return [{ eCode: codeMap.NotFound, eMsg: `无法找到对应的产品或者文章信息` }, null]; } valueData = res[0]; } valueShowText = valueData.title; [err, res] = await handle(d_base.addShowBlock( sort, fileId, type, value, valueShowText, title, subTitle, updateTime )); if (err) { log.info(`[新增展示块] 导入数据异常`) return [err, null]; } return [null, res]; } /** * 更新展示块 * @param id 展示块ID * @param updateItems 更新项 * @returns {Promise<*>} */ async function updateShowBlock(id, updateItems) { let err, res; let rawData = {}; let unixTime = getUnixTimeStamp(); let updateData = {}; let typeKeys = Object.keys(db_base.carouselType); let typeString = ''; let typeShowText = ''; let carouselType = db_base.carouselType.href; // 判断是否需要更新type if (updateItems.value) { // 获取基础type值 if (!isEmpty(updateItems.type)) { typeString = updateItems.type; updateData.type = updateItems.type; } else { [err, res] = await handle(d_base.getShowBlockById(id)); if (err) { log.error(`[修改展示块] 获取展示块基础数据时异常 ${err.message}`); return [{ eCode: codeMap.ServerError, eMsg: `获取展示块基础数据异常` }, null]; } if (isEmpty(res)) { log.info(`[修改展示块] 无法通过id找到展示块数据`); return [{ eCode: codeMap.NotFound, eMsg: `请检测id是否正确` }, null]; } rawData = res[0]; typeString = rawData.type; } console.log(toNumber(typeString)) // 需要更新具体值 if (toNumber(typeString) !== db_base.carouselType.href) { if (toNumber(typeString) === db_base.carouselType.production) { typeShowText = '产品'; // 获取产品信息 [err, res] = await handle(d_product.getProductById(updateItems.value)); } else if (toNumber(typeString) === db_base.carouselType.news) { typeShowText = '文章'; // 获取新闻信息 [err, res] = await handle(d_news.getNewsById(updateItems.value)); } else { return [{ eCode: codeMap.NotParam, eMsg: `展示块类型异常! carouseType:${typeString} typeof:${typeof typeString}` }, null]; } if (err) { console.log(err); log.error(`[修改展示块] 检索${typeShowText}信息失败 ${err.message}}`); return [{ eCode: codeMap.ServerError, eMsg: `检索${typeShowText}数据异常` }, null]; } if (isEmpty(res)) { log.info(`[修改展示块] 无法找到对应的${typeShowText}数据`); return [{ eCode: codeMap.NotFound, eMsg: `无法找到对应的产品或者文章信息` }, null]; } updateData.valueShowText = res[0].title; } else { updateData.valueShowText = updateItems.value; } updateData.value = updateItems.value; } if (!isEmpty(updateItems.sort)) { updateData.sort = toNumber(updateItems.sort); } if (!isEmpty(updateItems.state)) { updateData.state = updateItems.state; } if (updateItems.fileId) { updateData.fileId = updateItems.fileId; // todo 检测文件id是否存在 } if (updateItems.title) { updateData.title = updateItems.title; } if (updateItems.subTitle) { updateData.subTitle = updateItems.subTitle; } console.log(updateData); [err, res] = await handle(d_base.updateShowBlock(id, updateData, unixTime)); if (err) { console.log(err); log.error(`[修改展示块] 数据更新失败,${err.message}`); return [{ eCode: codeMap.SaveError, eMsg: `数据更新失败,请稍后重试` }, null]; } return [null, res]; } /** * 文件上传 * @param type * @param files * @returns {Promise<[{eMsg: string, eCode: number},null]|*[][]|[{eMsg: string, eCode: *},null]|*[]>} */ async function uploadFile(type, files){ // 文件类型一般不会被改变,只移动文件至对应的存储目录,并将路径存储到数据库中.只存储文件名 let err,res,newFileName; let uploadPath = config_path.files; let fileNameArr = [];// 文件存储路径数组 // 文件转存,使用newFileName作为路径 if(type === db_base.fileType.image){ uploadPath = config_path.images; }else if(type === db_base.fileType.video){ uploadPath = config_path.videos; } let keys = Object.keys(files); // 遍历转移文件 for(let fileKey of keys){ let file = files[fileKey]; // 判断当前文件是否为数组 if(!file.newFilename && file.length){ for(let f of file){ // console.log(f); // console.log(f.newFilename); log.info(`[文件上传] 开始移动文件 ${f.newFilename} to ${uploadPath}`); [err,newFileName] = await mvFile(f,uploadPath, f.newFilename); if(err){ return [err,null]; } // console.log('移动文件成功' + newFileName); fileNameArr.push(newFileName); } }else if(file.newFilename){ [err,newFileName] = await mvFile(file,uploadPath,file.newFilename); if(err){ return [err,null]; } fileNameArr.push(newFileName); }else{ // 文件调用异常 log.error(`[文件上传] 文件调用异常,fileObj\n: ${JSON.stringify(file)}`); return [ { eCode:codeMap.ServerError, eMsg:`文件调用异常` },null]; } } // 存储至数据库 // console.log(type); [err,res] = await handle(d_base.uploadFiles(type,fileNameArr,getUnixTimeStamp())); if(err){ log.error(`[文件上传] 文件入库失败 ${err.message}`); return [{eCode: codeMap.SaveError,eMsg: `文件存储失败!!!`},null]; } log.info(`[文件上传] 文件上传成功 ${res.affectedRows}`); return [null,fileNameArr]; } /** * 搜索文件 * @param type * @param key * @param l * @param p * @returns {Promise} */ async function searchFiles(type = 0, key, l, p){ let err,res; let _params = { key: key, } // console.log(type); // type为 数据库值加一,因为数据库中存储值从0开始,而前端显示从1开始 if((type - 1) !== db_base.fileType.all){ _params.type = type; } // console.log(_params); [err,res] = await searchHandle('搜索文件失败', d_base.loadFiles, _params, null, l, p); if(err){ log.info(`[文件资源] 加载失败 ${err.eDetail||err.message}`) return [err,null]; } // console.log(res); res.arr = res.arr.map(item=>{ console.log(item); item.filePath = filePathToUrl(item.fileType,item.filePath); return item; }); return [null,res]; } async function deleteFile(fileId){ let err,res,rmRes; // 获取文件信息 [err,res] = await handle(d_base.getFileById(fileId)); if(err){ log.info(`[文件资源] 获取文件信息失败 ${err.message}`); return [{eCode:codeMap.ServerError,eMsg:`删除文件失败,无法查找文件`},null]; } if(!res.length){ log.info(`[文件资源] 获取文件信息失败,无法找到文件`); return [{eCode:codeMap.NotFound,eMsg:`删除文件失败,无法查找文件`},null]; } let fileData = res[0]; let filePath = filePathToUrl(fileData.fileType,fileData.filePath); [err,res,rmRes] = await handleAll(d_base.deleteFile(fileId),rmFile(filePath)); if(err){ console.log(err); log.info(`[文件资源] 删除文件${fileId}失败 ${err.message}`); return [{eCode:codeMap.SaveError,eMsg:`删除文件失败`},null]; } return [null,true]; } async function getBaseData(){ // 同时获取所有需要获取的基础数据 let err,pType,nType,carouselAndShowBlocks,baseInfo,products,articles; let carousel = [], showBlocks = []; // todo 同时获取所有需要获取的基础数据 [err, products, articles, baseInfo, pType, nType, carouselAndShowBlocks] = await handleAll( d_product.searchProducts('array', {}, '', 1, 4), d_news.searchAllNewsMini('array', {}, '', 1, 5), d_base.getBaseInfo(), d_product.loadTypes(), d_news.loadTypes(), d_base.getEnableCarouselAndShowBlocks(), ); if (err) { log.error(`[基础数据] 获取基础数据类型分类失败 ${err.message}`); return [{eCode: codeMap.ServerError, eMsg: `获取基础数据失败`}, null]; } baseInfo = baseInfo? baseInfo[0]: {}; baseInfo.products = products; baseInfo.news = articles; if (carouselAndShowBlocks && carouselAndShowBlocks.length) { // log.info(`[基础数据] 获取轮播数据成功 ${carouselAndShowBlocks.length}`); console.log(carouselAndShowBlocks) carouselAndShowBlocks.forEach(item=>{ item.filePath = filePathToUrl(item.fileType, item.filePath); // log.info(`carousel: ${db_base.showType.carousel} showBlock: ${db_base.showType.showBlock}`) // log.info(`showType: ${item.showType} ${item.showType == db_base.carouselType.carousel}`) if(item.showType == db_base.showType.carousel){ log.info('添加轮播图') carousel.push(item); } if(item.showType == db_base.showType.showBlock){ showBlocks.push(item); } }) } // 合并数据 let baseData = { baseInfo, pType, nType, carousel, showBlocks, }; // todo 缓存接口数据 log.info(`[基础数据] 类型分类获取成功 展示程序数据数量:${products.length} 展示文章数据数量:${articles.length} 程序类型数量:${pType.length} 文章类型数量:${nType.length} 轮播数据数量:${carousel.length} 展示快数量:${showBlocks.length} `); return [null,baseData]; } async function getBaseInfo(){ let err,res; [err,res] = await handle(d_base.getBaseInfo()); if(err){ log.error(`[基础数据] 获取基础数据失败 ${err.message}`); return [{eCode: codeMap.ServerError, eMsg: `获取基础数据失败`}, null]; } if(!res.length){ log.error(`[基础数据] 获取基础数据失败,数据库中无基础数据`); return [] } res = res[0]; return [null,res]; } async function editBaseInfo(data){ let err,res; let baseId = data.id; [err,res] = await handle(d_base.getBaseInfo(baseId)); if(err){ log.error(`[基础数据] 获取基础数据失败 ${err.message}`); return [{eCode: codeMap.ServerError, eMsg: `获取基础数据失败`}, null]; } if(!res.length){ log.error(`[基础数据] 编辑基础数据失败,数据库中无基础数据 尝试添加`); // 新增数据 [err,res] = await handle(d_base.addBaseInfo(data)); if(err){ log.error(`[基础数据] 新增基础数据失败 ${err.message}`); return [{eCode: codeMap.ServerError, eMsg: `新增基础数据失败`}, null]; } log.info(`[基础数据] 新增基础数据成功`); return [null,true]; } let baseInfo = res[0]; // console.log(data); [err,res] = await handle(d_base.editBaseInfo(data, baseInfo.id)); if(err){ log.error(`[基础数据] 编辑基础数据失败 ${err.message}`); return [{eCode: codeMap.ServerError, eMsg: `编辑基础数据失败`}, null]; } log.info(`[基础数据] 编辑基础数据成功`); return [null,true]; } module.exports = { getEnableCarousel, getAllCarousel, addCarousel, deleteCarousel, updateCarousel, getEnableShowBlocks, getAllShowBlocks, addShowBlock, updateShowBlock, uploadFile, searchFiles, deleteFile, getBaseData, getBaseInfo, editBaseInfo }