cate.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* eslint-disable eqeqeq */
  2. import { model, getAll } from '../_utils/model';
  3. import { getCloudImageTempUrl } from '../../utils/cloudImageHandler';
  4. import { SPU_SELLING_STATUS } from '../../utils/spuStatus';
  5. import { DATA_MODEL_KEY } from '../../config/model';
  6. import { cloudbaseTemplateConfig } from '../../config/index';
  7. import { CATEGORY, SPU } from '../cloudbaseMock/index';
  8. const CATE_MODEL_KEY = DATA_MODEL_KEY.CATE;
  9. const HOME_SWIPER_MODEL_KEY = DATA_MODEL_KEY.HOME_SWIPER;
  10. // TODO: we should do pagination
  11. export async function getAllSpuOfCate(cateId) {
  12. if (cloudbaseTemplateConfig.useMock) {
  13. return { spu: CATEGORY.find((x) => x._id === cateId).spu.map(({ _id }) => SPU.find((x) => x._id === _id)) };
  14. }
  15. return (
  16. await model()[CATE_MODEL_KEY].get({
  17. filter: {
  18. where: {
  19. _id: {
  20. $eq: cateId,
  21. },
  22. },
  23. relateWhere: {
  24. spu: {
  25. where: {
  26. status: {
  27. $eq: SPU_SELLING_STATUS,
  28. },
  29. },
  30. },
  31. },
  32. },
  33. select: {
  34. spu: {
  35. $master: true,
  36. },
  37. },
  38. })
  39. ).data;
  40. }
  41. export async function getCates() {
  42. if (cloudbaseTemplateConfig.useMock) {
  43. return CATEGORY.filter((x) => x.child_cate?.length > 0);
  44. }
  45. const cateSelect = {
  46. _id: true,
  47. name: true,
  48. image: true,
  49. };
  50. const allCates = (
  51. await getAll({
  52. name: CATE_MODEL_KEY,
  53. select: {
  54. ...cateSelect,
  55. child_cate: cateSelect,
  56. },
  57. })
  58. ).filter((c) => c.child_cate.length !== 0);
  59. const childCates = allCates.flatMap((c) => c.child_cate);
  60. const res = await getCloudImageTempUrl(childCates.map((x) => x.image));
  61. res.forEach((image, index) => (childCates[index].image = image));
  62. return allCates;
  63. }
  64. export async function getSwipe() {
  65. // console.log("cloudbaseTemplateConfig.useMock",cloudbaseTemplateConfig.useMock);
  66. if (cloudbaseTemplateConfig.useMock) {
  67. // 返回包含多张模拟图片的数据
  68. return {
  69. images: [
  70. 'https://qcloudimg.tencent-cloud.cn/raw/1e793c70bb4f521fe277b2c207ab81b4.png',
  71. 'https://qcloudimg.tencent-cloud.cn/raw/063123361b3a397f4ba6894591c3a006.png',
  72. 'https://qcloudimg.tencent-cloud.cn/raw/62eb1d8d8ea3b05302c199636f787438.png'
  73. ]
  74. };
  75. }
  76. // 实际环境中获取所有记录,然后提取所有图片
  77. const records = (await model()[HOME_SWIPER_MODEL_KEY].list({ select: { images: true } })).data.records;
  78. // console.log("records:",records);
  79. // console.log("records[1].images:",records[1].images);
  80. // 合并所有记录中的images数组
  81. const allImages = records[1].images; //records.flatMap(record => record.images || []);
  82. return { images: allImages.length > 0 ? allImages : [] };
  83. }