@rxdrag/website-lib-core 0.0.85 → 0.0.86

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rxdrag/website-lib-core",
3
- "version": "0.0.85",
3
+ "version": "0.0.86",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./index.ts"
@@ -24,9 +24,9 @@
24
24
  "@types/react-dom": "^19.1.0",
25
25
  "eslint": "^7.32.0",
26
26
  "typescript": "^5",
27
+ "@rxdrag/slate-preview": "1.2.61",
27
28
  "@rxdrag/tsconfig": "0.2.0",
28
- "@rxdrag/eslint-config-custom": "0.2.12",
29
- "@rxdrag/slate-preview": "1.2.61"
29
+ "@rxdrag/eslint-config-custom": "0.2.12"
30
30
  },
31
31
  "dependencies": {
32
32
  "@iconify/utils": "^3.0.2",
@@ -36,8 +36,8 @@
36
36
  "lodash-es": "^4.17.21",
37
37
  "react": "^19.1.0",
38
38
  "react-dom": "^19.1.0",
39
- "@rxdrag/rxcms-models": "0.3.94",
40
- "@rxdrag/entify-lib": "0.0.23"
39
+ "@rxdrag/entify-lib": "0.0.23",
40
+ "@rxdrag/rxcms-models": "0.3.94"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "astro": "^4.0.0 || ^5.0.0"
@@ -48,6 +48,19 @@ export class Entify implements IEntify {
48
48
 
49
49
  private constructor(protected envVariables: EnvVariables, protected imageSizes: ImageSizes) { }
50
50
 
51
+ private flattenTree<T>(nodes: T[] | undefined): T[] {
52
+ if (!nodes) return [];
53
+ let result: T[] = [];
54
+ for (const node of nodes) {
55
+ result.push(node);
56
+ const children = (node as { children?: T[] }).children;
57
+ if (children && children.length > 0) {
58
+ result = result.concat(this.flattenTree(children));
59
+ }
60
+ }
61
+ return result;
62
+ }
63
+
51
64
  public static getInstance(envVariables: EnvVariables, imageSizes: ImageSizes): Entify {
52
65
  if (!Entify.instance) {
53
66
  Entify.instance = new Entify(envVariables, imageSizes);
@@ -277,7 +290,8 @@ export class Entify implements IEntify {
277
290
 
278
291
  public async getCategoredProductListPaths() {
279
292
  // 获取所有产品分类
280
- const categories = await this.getProductCategories();
293
+ const categoryTree = await this.getProductCategories();
294
+ const categories = this.flattenTree(categoryTree);
281
295
 
282
296
  // 为每个分类生成路径
283
297
  const paths = [];
@@ -335,14 +349,14 @@ export class Entify implements IEntify {
335
349
  }
336
350
 
337
351
  /**
338
- * TODO:未调试
339
352
  * 获取所有文章分类的分页路径
340
353
  * 用于生成静态页面
341
354
  * @returns 所有文章分类的分页路径
342
355
  */
343
356
  public async getCategoredPostListPaths() {
344
357
  // 获取所有文章分类
345
- const categories = await this.getPostCategories();
358
+ const categoryTree = await this.getPostCategories();
359
+ const categories = this.flattenTree(categoryTree);
346
360
 
347
361
  // 为每个分类生成路径
348
362
  const paths = [];
@@ -0,0 +1,20 @@
1
+ export interface CategoryNode {
2
+ id?: string | null;
3
+ children?: CategoryNode[];
4
+ }
5
+
6
+ export function collectCategoryIds(category: CategoryNode): string[] {
7
+ const ids: string[] = [];
8
+ if (category.id) ids.push(category.id);
9
+
10
+ const traverse = (node: CategoryNode) => {
11
+ if (node.children) {
12
+ node.children.forEach((child) => {
13
+ if (child.id) ids.push(child.id);
14
+ traverse(child);
15
+ });
16
+ }
17
+ };
18
+ traverse(category);
19
+ return ids;
20
+ }
@@ -28,4 +28,5 @@ export * from "./upsertEntity";
28
28
  export * from "./fulltextSearch";
29
29
  export * from "./sendEmail";
30
30
  export * from "./createUploadCredentials";
31
- export * from "./listToTree";
31
+ export * from "./listToTree";
32
+ export * from "./collectCategoryIds";
@@ -14,11 +14,18 @@ import {
14
14
  ImageSize,
15
15
  PostCategoryFields,
16
16
  MediaFields,
17
+ PostCategory,
18
+ PostCategoryBoolExp,
19
+ PostCategoryOrderBy,
20
+ PostCategoryDistinctExp,
21
+ PostCategoryQueryOptions,
17
22
  } from "@rxdrag/rxcms-models";
18
23
  import { queryEntityList } from "./queryEntityList";
19
24
  import { ListResult } from "@rxdrag/entify-lib";
20
25
  import { EnvVariables } from "../types";
21
26
  import { TPost } from "../view-model";
27
+ import { queryOneEntity } from "./queryOneEntity";
28
+ import { CategoryNode, collectCategoryIds } from "./collectCategoryIds";
22
29
 
23
30
  export interface ListConditions {
24
31
  category?: string; //category slug
@@ -37,13 +44,45 @@ export async function queryPosts(
37
44
 
38
45
  let where = {};
39
46
  if (categorySlug) {
40
- where = {
41
- [PostAssciations.category]: {
42
- slug: {
43
- _eq: categorySlug,
47
+ // 查询分类及其子分类
48
+ const category = await queryOneEntity<
49
+ PostCategory,
50
+ PostCategoryBoolExp,
51
+ PostCategoryOrderBy,
52
+ PostCategoryDistinctExp
53
+ >(
54
+ new PostCategoryQueryOptions(
55
+ [PostCategoryFields.id, PostCategoryFields.slug],
56
+ {
57
+ where: {
58
+ slug: {
59
+ _eq: categorySlug,
60
+ },
61
+ },
62
+ }
63
+ )
64
+ // 支持三级结构
65
+ .children(
66
+ new PostCategoryQueryOptions([
67
+ PostCategoryFields.id,
68
+ PostCategoryFields.slug,
69
+ ]).children([])
70
+ ),
71
+ envVariables
72
+ );
73
+
74
+ if (category) {
75
+ const categoryIds = collectCategoryIds(
76
+ category as unknown as CategoryNode
77
+ );
78
+ where = {
79
+ [PostAssciations.category]: {
80
+ id: {
81
+ _in: categoryIds,
82
+ },
44
83
  },
45
- },
46
- };
84
+ };
85
+ }
47
86
  }
48
87
 
49
88
  // 默认查询字段
@@ -7,6 +7,12 @@ import {
7
7
  ProductQueryOptions,
8
8
  PublishableStatus,
9
9
  ProductAssciations,
10
+ ProductCategory,
11
+ ProductCategoryBoolExp,
12
+ ProductCategoryOrderBy,
13
+ ProductCategoryDistinctExp,
14
+ ProductCategoryFields,
15
+ ProductCategoryQueryOptions,
10
16
  } from "@rxdrag/rxcms-models";
11
17
  import { ListConditions } from "./queryPosts";
12
18
  import { queryEntityList } from "./queryEntityList";
@@ -15,6 +21,8 @@ import { ListResult } from "@rxdrag/entify-lib";
15
21
  import { EnvVariables } from "../types";
16
22
  import { TProduct } from "../view-model";
17
23
  import { ImageSize } from "@rxdrag/rxcms-models";
24
+ import { queryOneEntity } from "./queryOneEntity";
25
+ import { CategoryNode, collectCategoryIds } from "./collectCategoryIds";
18
26
 
19
27
  export async function queryProducts(
20
28
  conditions: ListConditions,
@@ -31,13 +39,54 @@ export async function queryProducts(
31
39
 
32
40
  let where = {};
33
41
  if (categorySlug) {
34
- where = {
35
- [ProductAssciations.category]: {
36
- slug: {
37
- _eq: categorySlug,
42
+ //先查询分类以及其子分类
43
+ const category = await queryOneEntity<
44
+ ProductCategory,
45
+ ProductCategoryBoolExp,
46
+ ProductCategoryOrderBy,
47
+ ProductCategoryDistinctExp
48
+ >(
49
+ new ProductCategoryQueryOptions(
50
+ [
51
+ ProductCategoryFields.id,
52
+ ProductCategoryFields.slug,
53
+ ],
54
+ {
55
+ where: {
56
+ slug: {
57
+ _eq: categorySlug,
58
+ },
59
+ },
60
+ }
61
+ //支持三级结构
62
+ ).children(new ProductCategoryQueryOptions(
63
+ [
64
+ ProductCategoryFields.id,
65
+ ProductCategoryFields.slug,
66
+ ],
67
+ {
68
+ where: {
69
+ slug: {
70
+ _eq: categorySlug,
71
+ },
72
+ },
73
+ }
74
+ ).children([])),
75
+ envVariables
76
+ );
77
+ if (category) {
78
+ const categoryIds = collectCategoryIds(
79
+ category as unknown as CategoryNode
80
+ );
81
+ where = {
82
+ [ProductAssciations.category]: {
83
+ id: {
84
+ _in: categoryIds,
85
+ },
38
86
  },
39
- },
40
- };
87
+ };
88
+ }
89
+
41
90
  }
42
91
 
43
92
  // 默认查询字段