77 lines
2.4 KiB
JavaScript
77 lines
2.4 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("./common/vendor.js");
|
|
const utils_emitter = require("./utils/emitter.js");
|
|
const api_request = require("./api/request.js");
|
|
const _sfc_main = {
|
|
__name: "addProduct",
|
|
setup(__props) {
|
|
const productBrief = common_vendor.ref({});
|
|
const quantity = common_vendor.ref(1);
|
|
const userInfo = common_vendor.ref({});
|
|
common_vendor.onMounted(() => {
|
|
utils_emitter.emitter.on("product", (val) => {
|
|
productBrief.value = val;
|
|
});
|
|
});
|
|
common_vendor.onShow(() => {
|
|
userInfo.value = common_vendor.wx$1.getStorageSync("userInfo");
|
|
});
|
|
const close = () => {
|
|
utils_emitter.emitter.emit("close");
|
|
};
|
|
const addCart = async () => {
|
|
console.log("商品brief-->", productBrief.value);
|
|
console.log("用户信息-->", userInfo.value);
|
|
const res = await common_vendor.index.request({
|
|
url: api_request.baseUrl + "/cart/add",
|
|
method: "POST",
|
|
data: {
|
|
userId: userInfo.value.id,
|
|
goodId: productBrief.value.id,
|
|
quantity: quantity.value,
|
|
subtotal: productBrief.value.price * quantity.value,
|
|
isGoodType: productBrief.value.isGoodType
|
|
}
|
|
});
|
|
if (res.data.code === 1) {
|
|
common_vendor.index.showToast({
|
|
icon: "success",
|
|
title: "加入购物车成功"
|
|
});
|
|
close();
|
|
} else {
|
|
console.log("失败原因-->", res.data);
|
|
common_vendor.index.showToast({
|
|
icon: "error",
|
|
title: "请求失败"
|
|
});
|
|
return;
|
|
}
|
|
};
|
|
const decrease = () => {
|
|
if (quantity.value > 0) {
|
|
quantity.value -= 1;
|
|
}
|
|
};
|
|
const increase = () => {
|
|
if (quantity.value <= productBrief.value.inventory) {
|
|
quantity.value += 1;
|
|
}
|
|
};
|
|
return (_ctx, _cache) => {
|
|
return {
|
|
a: productBrief.value.goodImg,
|
|
b: common_vendor.t(productBrief.value.price),
|
|
c: common_vendor.t(productBrief.value.name),
|
|
d: common_vendor.o(($event) => close()),
|
|
e: common_vendor.o(decrease),
|
|
f: common_vendor.t(quantity.value),
|
|
g: common_vendor.o(increase),
|
|
h: common_vendor.o(addCart)
|
|
};
|
|
};
|
|
}
|
|
};
|
|
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-ac6f555c"]]);
|
|
exports.MiniProgramPage = MiniProgramPage;
|