import { request } from "../../../utils/request"; import { join, leave, addCustomEventListener, removeCustomEventListener } from "../../../utils/server-sent-events"; const statusId = "roomId-1"; Component({ properties: { open: { type: Boolean, value: false }, hasTabBar: { type: Boolean, value: false, }, productNum: { type: Number, value: 1, }, joinedGroupId: { type: String, value: "" }, // 用户房间号 }, data: { name: "商品名称", price: 0, picUrl: "https://oneppp.mn188.cn/oneppp/image/037b08ef-e576-4063-9609-090773b50dfa.jpg", skuId: 0, joined: false, skuRes: {}, show: false, stock: 0, count:0 }, lifetimes: { async attached() { const that = this as any; // join SSE 频道 try { if (!that.data.joined) { await join(statusId); that.setData({ joined: true }); } } catch (err) { console.warn("join失败", err); } // 绑定 SSE 事件 that._listenerFn = async (data: any) => { const that = this as any; if (!data || data.type !== "product" || !data.skuId) return; try { if (that.data.skuId == data.skuId) { that.setData({ show: true }); wx.nextTick(() => { that.setData({ open: true }); }); return; } const info = await request({ options: { url: `/app/shop-store/sku-one`, data: { id: Number(data.skuId) } }, isLoading: false }); that.setData({ name: info.name, price: info.price, picUrl: info.picUrl, skuId: info.skuId, skuRes: info, show: true, stock: info.stock, count: info.count }); wx.nextTick(() => { that.setData({ open: true }); }); } catch (err) { console.error("获取商品信息失败", err); } }; addCustomEventListener("pushMsg", that._listenerFn); }, async detached() { const that = this as any; // leave SSE 频道 try { if (that.data.joined) { await leave(statusId); that.setData({ joined: false }); } } catch (err) { console.warn("leave失败", err); } // 移除事件监听 if (that._listenerFn) { removeCustomEventListener("pushMsg", that._listenerFn); that._listenerFn = null; } } }, methods: { handleClick() { const that = this as any; wx.navigateTo({ url: `/pages/common/live-good-detail/live-good-detail?skuId=${that.data.skuId}&roomId=${that.data.joinedGroupId}` }); }, handleClose() { const that = this as any; that.setData({ open: false }); }, openPopup(){ const that = this as any; const popup = that.selectComponent('#goodDetailPopup') popup.openPopup() } // , // handleAddShopCart() { // const that = this as any; // const mbuId = wx.getStorageSync("mbuId"); // let allShoppingCart = wx.getStorageSync("shoppingCart"); // if (!Array.isArray(allShoppingCart)) { // allShoppingCart = []; // } // let shoppingCart: any = allShoppingCart.find( // (item: any) => item.mbuId == mbuId // ); // if (shoppingCart) { // const temp = shoppingCart.data.find( // (item: any) => item.skuId == that.data.skuId // ); // if (temp) { // //购物车里已经有了 // temp.count += 1; // if (temp.count > that.data.stock) { // wx.showToast({ title: "购物车数量超过库存数量了!", icon: "none" }) // return; // } // } else { // //没有在购物车里 // shoppingCart.data.push({ ...that.data, count: 1 }); // } // } else { // //无添加购物车历史 // shoppingCart = { // mbuId, // // @ts-ignore // data: [{ ...this.data, count: 1 }] // }; // allShoppingCart.push(shoppingCart); // } // wx.setStorageSync("shoppingCart", allShoppingCart); // wx.showToast({ title: "加入购物车成功!" }) // } } });