c_base.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. const {handle, handleAll} = require('../tools/handle_cjs');
  2. const d_base = require("../database/d_base");
  3. const d_product = require("../database/d_product");
  4. const d_news = require("../database/d_news");
  5. const codeMap = require("../map/rcodeMap");
  6. const {db_base} = require("../map/dbField");
  7. const {searchHandle} = require("../tools/searchSql");
  8. const {isEmpty, toNumber} = require("../tools/typeTool_cjs");
  9. const config_path = require("../../configs/path.json");
  10. const {mvFile, rmFile} = require("../tools/saveFiles_cjs");
  11. const {getUnixTimeStamp} = require("../tools/time_cjs");
  12. const {filePathToUrl} = require("../tools/filePathTool");
  13. const log = require("../logger").logger("c_base","info");
  14. // 获取启用的轮播列表
  15. async function getEnableCarousel(){
  16. let err,res;
  17. [err,res] = await handle(d_base.getCarousel({
  18. state: db_base.carouselState.enable
  19. }));
  20. // console.log(res);
  21. if(err){
  22. return [err,null];
  23. }
  24. // 路径转换
  25. res = res.map(item=>{
  26. item.filePath = filePathToUrl(item.fileType,item.filePath);
  27. return item;
  28. });
  29. return [null,res];
  30. }
  31. async function getAllCarousel(){
  32. let err,res;
  33. [err,res] = await handle(d_base.getCarousel());
  34. // console.log(res);
  35. if(err){
  36. return [err,null];
  37. }
  38. // 路径转换
  39. res = res.map(item=>{
  40. item.filePath = filePathToUrl(item.fileType,item.filePath);
  41. return item;
  42. });
  43. return [null,res];
  44. }
  45. async function addCarousel(sort,fileId,type,value){
  46. let err,res;
  47. let valueData = {};
  48. let valueShowText = '';
  49. let updateTime = getUnixTimeStamp();
  50. // 根据type判断value
  51. if(type !== db_base.carouselType.href){
  52. if(type === db_base.carouselType.production){
  53. [err,res] = await handle(d_product.getProductById(value));
  54. }else if(type === db_base.carouselType.news){
  55. // 获取新闻信息
  56. [err,res] = await handle(d_news.getNewsById(value));
  57. }
  58. if(err){
  59. console.log(err);
  60. log.info(`[新增轮播] 检索产品信息失败`);
  61. return [{eCode: codeMap.ServerError, eMsg: `检索数据异常`},null];
  62. }
  63. if(isEmpty(res)){
  64. log.info(`[新增轮播] 无法找到对应的产品或者文章信息`);
  65. return [{eCode: codeMap.NotFound, eMsg: `无法找到对应的产品或者文章信息`},null];
  66. }
  67. valueData = res[0];
  68. }
  69. valueShowText = valueData.title;
  70. [err,res] = await handle(d_base.addCarousel(
  71. sort,fileId,type,value,valueShowText,updateTime
  72. ));
  73. if(err){
  74. log.info(`[新增轮播] 导入数据异常`)
  75. return [err,null];
  76. }
  77. return [null,res];
  78. }
  79. async function deleteCarousel(id){
  80. let err,res;
  81. [err,res] = await handle(d_base.deleteCarousel(id));
  82. if(err){
  83. log.info(`[删除轮播] 删除数据异常`);
  84. return [err,null];
  85. }
  86. return [null,res];
  87. }
  88. async function updateCarousel(id,updateItems){
  89. let err,res;
  90. let rawData = {};
  91. let unixTime = getUnixTimeStamp();
  92. let updateData = {};
  93. let typeKeys = Object.keys(db_base.carouselType);
  94. let typeString = '';
  95. let typeShowText = '';
  96. let carouselType = db_base.carouselType.href;
  97. // 判断是否需要更新type
  98. if (updateItems.value){
  99. // 获取基础type值
  100. if(!isEmpty(updateItems.type)){
  101. typeString = updateItems.type;
  102. updateData.type = updateItems.type;
  103. }else{
  104. [err,res] = await handle(d_base.getCarouselById(id));
  105. if(err){
  106. log.error(`[修改轮播] 获取轮播基础数据时异常 ${err.message}`);
  107. return [{eCode: codeMap.ServerError, eMsg: `获取轮播基础数据异常`},null];
  108. }
  109. if(isEmpty(res)){
  110. log.info(`[修改轮播] 无法通过id找到轮播数据`);
  111. return [{eCode: codeMap.NotFound, eMsg: `请检测id是否正确`},null];
  112. }
  113. rawData = res[0];
  114. typeString = rawData.type;
  115. }
  116. console.log(toNumber(typeString))
  117. // 需要更新具体值
  118. if(toNumber(typeString) !== db_base.carouselType.href){
  119. if(toNumber(typeString) === db_base.carouselType.production){
  120. typeShowText = '产品';
  121. // 获取产品信息
  122. [err,res] = await handle(d_product.getProductById(updateItems.value));
  123. }else if(toNumber(typeString) === db_base.carouselType.news){
  124. typeShowText = '文章';
  125. // 获取新闻信息
  126. [err,res] = await handle(d_news.getNewsById(updateItems.value));
  127. }else {
  128. return [{eCode: codeMap.NotParam, eMsg: `轮播类型异常! carouseType:${typeString} typeof:${typeof typeString}`},null];
  129. }
  130. if(err){
  131. console.log(err);
  132. log.error(`[修改轮播] 检索${typeShowText}信息失败 ${err.message}}`);
  133. return [{eCode: codeMap.ServerError, eMsg: `检索${typeShowText}数据异常`},null];
  134. }
  135. if(isEmpty(res)){
  136. log.info(`[修改轮播] 无法找到对应的${typeShowText}数据`);
  137. return [{eCode: codeMap.NotFound, eMsg: `无法找到对应的产品或者文章信息`},null];
  138. }
  139. updateData.valueShowText = res[0].title;
  140. }
  141. else {
  142. updateData.valueShowText = updateItems.value;
  143. }
  144. updateData.value = updateItems.value;
  145. }
  146. if (!isEmpty(updateItems.sort)){
  147. updateData.sort = toNumber(updateItems.sort);
  148. }
  149. if (!isEmpty(updateItems.state)){
  150. updateData.state = updateItems.state;
  151. }
  152. if(updateItems.fileId){
  153. updateData.fileId = updateItems.fileId;
  154. // todo 检测文件id是否存在
  155. }
  156. console.log(updateData);
  157. [err,res] = await handle(d_base.updateCarousel(id,updateData,unixTime));
  158. if(err){
  159. console.log(err);
  160. log.error(`[修改轮播] 数据更新失败,${err.message}`);
  161. return [{eCode: codeMap.SaveError, eMsg: `数据更新失败,请稍后重试`},null];
  162. }
  163. return [null,res];
  164. }
  165. // 展示块控制函数
  166. /**
  167. * 获取启用的展示块列表
  168. * @returns {Promise<*>}
  169. */
  170. async function getEnableShowBlocks() {
  171. let err, res;
  172. [err, res] = await handle(d_base.getShowBlocks());
  173. if (err) {
  174. return [err, null];
  175. }
  176. // 路径转换
  177. res = res.map(item => {
  178. item.filePath = filePathToUrl(item.fileType, item.filePath);
  179. return item;
  180. });
  181. return [null, res];
  182. }
  183. /**
  184. * 获取所有展示块
  185. * @returns {Promise<*>}
  186. */
  187. async function getAllShowBlocks() {
  188. let err, res;
  189. [err, res] = await handle(d_base.getShowBlocks());
  190. if (err) {
  191. return [err, null];
  192. }
  193. // 路径转换
  194. res = res.map(item => {
  195. item.filePath = filePathToUrl(item.fileType, item.filePath);
  196. return item;
  197. });
  198. return [null, res];
  199. }
  200. /**
  201. * 添加展示块
  202. * @param sort 排序
  203. * @param fileId 文件ID
  204. * @param type 类型
  205. * @param value 值
  206. * @param title 标题
  207. * @param subTitle 副标题
  208. * @returns {Promise<*>}
  209. */
  210. async function addShowBlock(sort, fileId, type, value, title, subTitle) {
  211. let err, res;
  212. let valueData = {};
  213. let valueShowText = '';
  214. let updateTime = getUnixTimeStamp();
  215. // 根据type判断value
  216. if (type !== db_base.carouselType.href) {
  217. if (type === db_base.carouselType.production) {
  218. [err, res] = await handle(d_product.getProductById(value));
  219. } else if (type === db_base.carouselType.news) {
  220. // 获取新闻信息
  221. [err, res] = await handle(d_news.getNewsById(value));
  222. }
  223. if (err) {
  224. console.log(err);
  225. log.info(`[新增展示块] 检索产品信息失败`);
  226. return [{ eCode: codeMap.ServerError, eMsg: `检索数据异常` }, null];
  227. }
  228. if (isEmpty(res)) {
  229. log.info(`[新增展示块] 无法找到对应的产品或者文章信息`);
  230. return [{ eCode: codeMap.NotFound, eMsg: `无法找到对应的产品或者文章信息` }, null];
  231. }
  232. valueData = res[0];
  233. }
  234. valueShowText = valueData.title;
  235. [err, res] = await handle(d_base.addShowBlock(
  236. sort, fileId, type, value, valueShowText, title, subTitle, updateTime
  237. ));
  238. if (err) {
  239. log.info(`[新增展示块] 导入数据异常`)
  240. return [err, null];
  241. }
  242. return [null, res];
  243. }
  244. /**
  245. * 更新展示块
  246. * @param id 展示块ID
  247. * @param updateItems 更新项
  248. * @returns {Promise<*>}
  249. */
  250. async function updateShowBlock(id, updateItems) {
  251. let err, res;
  252. let rawData = {};
  253. let unixTime = getUnixTimeStamp();
  254. let updateData = {};
  255. let typeKeys = Object.keys(db_base.carouselType);
  256. let typeString = '';
  257. let typeShowText = '';
  258. let carouselType = db_base.carouselType.href;
  259. // 判断是否需要更新type
  260. if (updateItems.value) {
  261. // 获取基础type值
  262. if (!isEmpty(updateItems.type)) {
  263. typeString = updateItems.type;
  264. updateData.type = updateItems.type;
  265. } else {
  266. [err, res] = await handle(d_base.getShowBlockById(id));
  267. if (err) {
  268. log.error(`[修改展示块] 获取展示块基础数据时异常 ${err.message}`);
  269. return [{ eCode: codeMap.ServerError, eMsg: `获取展示块基础数据异常` }, null];
  270. }
  271. if (isEmpty(res)) {
  272. log.info(`[修改展示块] 无法通过id找到展示块数据`);
  273. return [{ eCode: codeMap.NotFound, eMsg: `请检测id是否正确` }, null];
  274. }
  275. rawData = res[0];
  276. typeString = rawData.type;
  277. }
  278. console.log(toNumber(typeString))
  279. // 需要更新具体值
  280. if (toNumber(typeString) !== db_base.carouselType.href) {
  281. if (toNumber(typeString) === db_base.carouselType.production) {
  282. typeShowText = '产品';
  283. // 获取产品信息
  284. [err, res] = await handle(d_product.getProductById(updateItems.value));
  285. } else if (toNumber(typeString) === db_base.carouselType.news) {
  286. typeShowText = '文章';
  287. // 获取新闻信息
  288. [err, res] = await handle(d_news.getNewsById(updateItems.value));
  289. } else {
  290. return [{ eCode: codeMap.NotParam, eMsg: `展示块类型异常! carouseType:${typeString} typeof:${typeof typeString}` }, null];
  291. }
  292. if (err) {
  293. console.log(err);
  294. log.error(`[修改展示块] 检索${typeShowText}信息失败 ${err.message}}`);
  295. return [{ eCode: codeMap.ServerError, eMsg: `检索${typeShowText}数据异常` }, null];
  296. }
  297. if (isEmpty(res)) {
  298. log.info(`[修改展示块] 无法找到对应的${typeShowText}数据`);
  299. return [{ eCode: codeMap.NotFound, eMsg: `无法找到对应的产品或者文章信息` }, null];
  300. }
  301. updateData.valueShowText = res[0].title;
  302. } else {
  303. updateData.valueShowText = updateItems.value;
  304. }
  305. updateData.value = updateItems.value;
  306. }
  307. if (!isEmpty(updateItems.sort)) {
  308. updateData.sort = toNumber(updateItems.sort);
  309. }
  310. if (!isEmpty(updateItems.state)) {
  311. updateData.state = updateItems.state;
  312. }
  313. if (updateItems.fileId) {
  314. updateData.fileId = updateItems.fileId;
  315. // todo 检测文件id是否存在
  316. }
  317. if (updateItems.title) {
  318. updateData.title = updateItems.title;
  319. }
  320. if (updateItems.subTitle) {
  321. updateData.subTitle = updateItems.subTitle;
  322. }
  323. console.log(updateData);
  324. [err, res] = await handle(d_base.updateShowBlock(id, updateData, unixTime));
  325. if (err) {
  326. console.log(err);
  327. log.error(`[修改展示块] 数据更新失败,${err.message}`);
  328. return [{ eCode: codeMap.SaveError, eMsg: `数据更新失败,请稍后重试` }, null];
  329. }
  330. return [null, res];
  331. }
  332. /**
  333. * 文件上传
  334. * @param type
  335. * @param files
  336. * @returns {Promise<[{eMsg: string, eCode: number},null]|*[][]|[{eMsg: string, eCode: *},null]|*[]>}
  337. */
  338. async function uploadFile(type, files){
  339. // 文件类型一般不会被改变,只移动文件至对应的存储目录,并将路径存储到数据库中.只存储文件名
  340. let err,res,newFileName;
  341. let uploadPath = config_path.files;
  342. let fileNameArr = [];// 文件存储路径数组
  343. // 文件转存,使用newFileName作为路径
  344. if(type === db_base.fileType.image){
  345. uploadPath = config_path.images;
  346. }else if(type === db_base.fileType.video){
  347. uploadPath = config_path.videos;
  348. }
  349. let keys = Object.keys(files);
  350. // 遍历转移文件
  351. for(let fileKey of keys){
  352. let file = files[fileKey];
  353. // 判断当前文件是否为数组
  354. if(!file.newFilename && file.length){
  355. for(let f of file){
  356. // console.log(f);
  357. // console.log(f.newFilename);
  358. log.info(`[文件上传] 开始移动文件 ${f.newFilename} to ${uploadPath}`);
  359. [err,newFileName] = await mvFile(f,uploadPath, f.newFilename);
  360. if(err){
  361. return [err,null];
  362. }
  363. // console.log('移动文件成功' + newFileName);
  364. fileNameArr.push(newFileName);
  365. }
  366. }else if(file.newFilename){
  367. [err,newFileName] = await mvFile(file,uploadPath,file.newFilename);
  368. if(err){
  369. return [err,null];
  370. }
  371. fileNameArr.push(newFileName);
  372. }else{
  373. // 文件调用异常
  374. log.error(`[文件上传] 文件调用异常,fileObj\n: ${JSON.stringify(file)}`);
  375. return [
  376. {
  377. eCode:codeMap.ServerError,
  378. eMsg:`文件调用异常`
  379. },null];
  380. }
  381. }
  382. // 存储至数据库
  383. // console.log(type);
  384. [err,res] = await handle(d_base.uploadFiles(type,fileNameArr,getUnixTimeStamp()));
  385. if(err){
  386. log.error(`[文件上传] 文件入库失败 ${err.message}`);
  387. return [{eCode: codeMap.SaveError,eMsg: `文件存储失败!!!`},null];
  388. }
  389. log.info(`[文件上传] 文件上传成功 ${res.affectedRows}`);
  390. return [null,fileNameArr];
  391. }
  392. /**
  393. * 搜索文件
  394. * @param type
  395. * @param key
  396. * @param l
  397. * @param p
  398. * @returns {Promise<err[]|{arr, total: number, limit, page}[]>}
  399. */
  400. async function searchFiles(type = 0, key, l, p){
  401. let err,res;
  402. let _params = {
  403. key: key,
  404. }
  405. // console.log(type);
  406. // type为 数据库值加一,因为数据库中存储值从0开始,而前端显示从1开始
  407. if((type - 1) !== db_base.fileType.all){
  408. _params.type = type;
  409. }
  410. // console.log(_params);
  411. [err,res] = await searchHandle('搜索文件失败', d_base.loadFiles, _params, null, l, p);
  412. if(err){
  413. log.info(`[文件资源] 加载失败 ${err.eDetail||err.message}`)
  414. return [err,null];
  415. }
  416. // console.log(res);
  417. res.arr = res.arr.map(item=>{
  418. console.log(item);
  419. item.filePath = filePathToUrl(item.fileType,item.filePath);
  420. return item;
  421. });
  422. return [null,res];
  423. }
  424. async function deleteFile(fileId){
  425. let err,res,rmRes;
  426. // 获取文件信息
  427. [err,res] = await handle(d_base.getFileById(fileId));
  428. if(err){
  429. log.info(`[文件资源] 获取文件信息失败 ${err.message}`);
  430. return [{eCode:codeMap.ServerError,eMsg:`删除文件失败,无法查找文件`},null];
  431. }
  432. if(!res.length){
  433. log.info(`[文件资源] 获取文件信息失败,无法找到文件`);
  434. return [{eCode:codeMap.NotFound,eMsg:`删除文件失败,无法查找文件`},null];
  435. }
  436. let fileData = res[0];
  437. let filePath = filePathToUrl(fileData.fileType,fileData.filePath);
  438. [err,res,rmRes] = await handleAll(d_base.deleteFile(fileId),rmFile(filePath));
  439. if(err){
  440. console.log(err);
  441. log.info(`[文件资源] 删除文件${fileId}失败 ${err.message}`);
  442. return [{eCode:codeMap.SaveError,eMsg:`删除文件失败`},null];
  443. }
  444. return [null,true];
  445. }
  446. async function getBaseData(){
  447. // 同时获取所有需要获取的基础数据
  448. let err,pType,nType,carouselAndShowBlocks,baseInfo,products,articles;
  449. let carousel = [], showBlocks = [];
  450. // todo 同时获取所有需要获取的基础数据
  451. [err, products, articles, baseInfo, pType, nType, carouselAndShowBlocks] = await handleAll(
  452. d_product.searchProducts('array', {}, '', 1, 4),
  453. d_news.searchAllNewsMini('array', {}, '', 1, 5),
  454. d_base.getBaseInfo(),
  455. d_product.loadTypes(),
  456. d_news.loadTypes(),
  457. d_base.getEnableCarouselAndShowBlocks(),
  458. );
  459. if (err) {
  460. log.error(`[基础数据] 获取基础数据类型分类失败 ${err.message}`);
  461. return [{eCode: codeMap.ServerError, eMsg: `获取基础数据失败`}, null];
  462. }
  463. baseInfo = baseInfo? baseInfo[0]: {};
  464. baseInfo.products = products;
  465. baseInfo.news = articles;
  466. if (carouselAndShowBlocks && carouselAndShowBlocks.length) {
  467. // log.info(`[基础数据] 获取轮播数据成功 ${carouselAndShowBlocks.length}`);
  468. console.log(carouselAndShowBlocks)
  469. carouselAndShowBlocks.forEach(item=>{
  470. item.filePath = filePathToUrl(item.fileType, item.filePath);
  471. // log.info(`carousel: ${db_base.showType.carousel} showBlock: ${db_base.showType.showBlock}`)
  472. // log.info(`showType: ${item.showType} ${item.showType == db_base.carouselType.carousel}`)
  473. if(item.showType == db_base.showType.carousel){
  474. log.info('添加轮播图')
  475. carousel.push(item);
  476. }
  477. if(item.showType == db_base.showType.showBlock){
  478. showBlocks.push(item);
  479. }
  480. })
  481. }
  482. // 合并数据
  483. let baseData = {
  484. baseInfo,
  485. pType,
  486. nType,
  487. carousel,
  488. showBlocks,
  489. };
  490. // todo 缓存接口数据
  491. log.info(`[基础数据] 类型分类获取成功
  492. 展示程序数据数量:${products.length}
  493. 展示文章数据数量:${articles.length}
  494. 程序类型数量:${pType.length}
  495. 文章类型数量:${nType.length}
  496. 轮播数据数量:${carousel.length}
  497. 展示快数量:${showBlocks.length}
  498. `);
  499. return [null,baseData];
  500. }
  501. async function getBaseInfo(){
  502. let err,res;
  503. [err,res] = await handle(d_base.getBaseInfo());
  504. if(err){
  505. log.error(`[基础数据] 获取基础数据失败 ${err.message}`);
  506. return [{eCode: codeMap.ServerError, eMsg: `获取基础数据失败`}, null];
  507. }
  508. if(!res.length){
  509. log.error(`[基础数据] 获取基础数据失败,数据库中无基础数据`);
  510. return []
  511. }
  512. res = res[0];
  513. return [null,res];
  514. }
  515. async function editBaseInfo(data){
  516. let err,res;
  517. let baseId = data.id;
  518. [err,res] = await handle(d_base.getBaseInfo(baseId));
  519. if(err){
  520. log.error(`[基础数据] 获取基础数据失败 ${err.message}`);
  521. return [{eCode: codeMap.ServerError, eMsg: `获取基础数据失败`}, null];
  522. }
  523. if(!res.length){
  524. log.error(`[基础数据] 编辑基础数据失败,数据库中无基础数据 尝试添加`);
  525. // 新增数据
  526. [err,res] = await handle(d_base.addBaseInfo(data));
  527. if(err){
  528. log.error(`[基础数据] 新增基础数据失败 ${err.message}`);
  529. return [{eCode: codeMap.ServerError, eMsg: `新增基础数据失败`}, null];
  530. }
  531. log.info(`[基础数据] 新增基础数据成功`);
  532. return [null,true];
  533. }
  534. let baseInfo = res[0];
  535. // console.log(data);
  536. [err,res] = await handle(d_base.editBaseInfo(data, baseInfo.id));
  537. if(err){
  538. log.error(`[基础数据] 编辑基础数据失败 ${err.message}`);
  539. return [{eCode: codeMap.ServerError, eMsg: `编辑基础数据失败`}, null];
  540. }
  541. log.info(`[基础数据] 编辑基础数据成功`);
  542. return [null,true];
  543. }
  544. module.exports = {
  545. getEnableCarousel,
  546. getAllCarousel,
  547. addCarousel,
  548. deleteCarousel,
  549. updateCarousel,
  550. getEnableShowBlocks,
  551. getAllShowBlocks,
  552. addShowBlock,
  553. updateShowBlock,
  554. uploadFile,
  555. searchFiles,
  556. deleteFile,
  557. getBaseData,
  558. getBaseInfo,
  559. editBaseInfo
  560. }