Blame view

stores/product_list.ts 1.86 KB
1
2
3
4
import { getProductList } from "./../service";
import { ref, watchEffect } from "vue";
import { defineStore } from "pinia";
import type { Product, ProductImage, ProductListQuery } from "@/type";
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();
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,
          },
        })
      );
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
          imgList: record?.productimageliststore ? JSON.parse(
33
34
            record.productimageliststore as unknown as string
          ).map((item: ProductImage) => ({
boyang authored
35
            url: `/api/show/image?fileKey=${item.fileKey}&psize=p256`,
36
          })): [],
37
38
        })) || [];
      total.value = data.value?.data?.total || 0;
39
    }
40
  };
41
42

  const updatePageNo = (value: number) => {
43
44
    pageNo.value = value;
  };
45
46

  const updateCategory = (value: string) => {
47
48
    productCategoryId.value = value;
  };
49
50

  const updateParams = (value: any) => {
51
52
    params.value = value;
  };
53
54

  const updateKeyword = (value: string) => {
55
56
    keyword.value = value;
  };
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,
  };
});