1
2
3
4
import { getProductList } from "./../service";
import { ref, watchEffect } from "vue";
import { defineStore } from "pinia";
import type { Product, ProductImage, ProductListQuery } from "@/type";
sanmu
authored
about a year ago
5
6
7
8
9
10
11
12
13
export const useProductListStore = defineStore("productList", () => {
const list = ref<Product[]>([]);
const productCategoryId = ref(""); // 选中的类别
const keyword = ref();
const pageNo = ref(1);
const pageSize = ref(20);
const total = ref(0);
const params = ref();
sanmu
authored
about a year ago
14
15
16
const getList = async (params: ProductListQuery) => {
if (params.productCategoryId || params.keyword) {
17
18
19
20
21
22
23
24
25
26
27
const { data } = await useAsyncData("product", () =>
$fetch("/shop/product/list", {
method: "GET",
params: {
...params,
pageNo: pageNo.value,
pageSize: pageSize.value,
},
})
);
sanmu
authored
about a year ago
28
29
30
31
list.value =
(data.value?.data?.records || []).map((record: Product) => ({
...record,
// http://112.74.45.244:8100/api/show/image?fileKey=ac82abea34243b7f7a56e5c3ca03f3a9
32
33
34
imgList: JSON.parse(
record.productimageliststore as unknown as string
).map((item: ProductImage) => ({
35
url: `/api/show/image?fileKey=${item.fileKey}&psize=p512`,
36
37
38
})),
})) || [];
total.value = data.value?.data?.total || 0;
sanmu
authored
about a year ago
39
}
40
};
sanmu
authored
about a year ago
41
42
const updatePageNo = (value: number) => {
43
44
pageNo.value = value;
};
sanmu
authored
about a year ago
45
46
const updateCategory = (value: string) => {
47
48
productCategoryId.value = value;
};
sanmu
authored
about a year ago
49
50
const updateParams = (value: any) => {
51
52
params.value = value;
};
sanmu
authored
about a year ago
53
54
const updateKeyword = (value: string) => {
55
56
keyword.value = value;
};
sanmu
authored
about a year ago
57
58
59
60
61
62
63
64
65
66
67
68
return {
pageNo,
pageSize,
total,
list,
keyword,
params,
getList,
updateCategory,
updatePageNo,
updateParams,
69
70
71
updateKeyword,
};
});