83 lines
2.6 KiB
JavaScript
83 lines
2.6 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("../../common/vendor.js");
|
|
const api_product = require("../../api/product.js");
|
|
if (!Math) {
|
|
ProductCard();
|
|
}
|
|
const ProductCard = () => "../../components/ProductCard.js";
|
|
const pageSize = 20;
|
|
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
|
|
__name: "index",
|
|
setup(__props) {
|
|
const products = common_vendor.ref([]);
|
|
const categories = common_vendor.ref([]);
|
|
const activeCategoryId = common_vendor.ref(void 0);
|
|
const loading = common_vendor.ref(false);
|
|
const page = common_vendor.ref(1);
|
|
async function loadCategories() {
|
|
try {
|
|
const data = await api_product.getCategories();
|
|
categories.value = [{ id: 0, name: "全部", sort: 0 }, ...data];
|
|
} catch {
|
|
}
|
|
}
|
|
async function loadProducts(reset = false) {
|
|
if (reset) {
|
|
page.value = 1;
|
|
products.value = [];
|
|
}
|
|
loading.value = true;
|
|
try {
|
|
const params = { page: page.value, pageSize };
|
|
if (activeCategoryId.value && activeCategoryId.value !== 0) {
|
|
params.categoryId = activeCategoryId.value;
|
|
}
|
|
const data = await api_product.getProducts(params);
|
|
if (reset) {
|
|
products.value = data.list;
|
|
} else {
|
|
products.value.push(...data.list);
|
|
}
|
|
} catch {
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
}
|
|
function selectCategory(id) {
|
|
activeCategoryId.value = id;
|
|
loadProducts(true);
|
|
}
|
|
common_vendor.onMounted(() => {
|
|
loadCategories();
|
|
loadProducts(true);
|
|
});
|
|
return (_ctx, _cache) => {
|
|
return common_vendor.e({
|
|
a: common_vendor.f(categories.value, (cat, k0, i0) => {
|
|
return {
|
|
a: common_vendor.t(cat.name),
|
|
b: cat.id,
|
|
c: activeCategoryId.value === cat.id ? 1 : "",
|
|
d: common_vendor.o(($event) => selectCategory(cat.id), cat.id)
|
|
};
|
|
}),
|
|
b: common_vendor.f(products.value, (product, k0, i0) => {
|
|
return {
|
|
a: "1cf27b2a-0-" + i0,
|
|
b: common_vendor.p({
|
|
product
|
|
}),
|
|
c: product.id
|
|
};
|
|
}),
|
|
c: !loading.value && products.value.length === 0
|
|
}, !loading.value && products.value.length === 0 ? {} : {}, {
|
|
d: loading.value
|
|
}, loading.value ? {} : {});
|
|
};
|
|
}
|
|
});
|
|
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-1cf27b2a"]]);
|
|
wx.createPage(MiniProgramPage);
|
|
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/index/index.js.map
|