<template>
    <view>
        <cart-container ref="cartContainer"></cart-container>
    </view>
</template>

<script>
    export default {
        data() {
            return {

            }
        },
        onShow() {
            // 每次显示页面时，更新购物车角标并调用子组件的刷新方法
            this.$updateCartBadge();
            this.$nextTick(() => {
                if (this.$refs.cartContainer) {
                    this.$refs.cartContainer.loadData();
                }
            });
        },
        // 分享配置
        onShareAppMessage(res) {
            console.log(res, '分享');
            if (res.from === "button") {
                let reduceCode = uni.getStorageSync("reduceCodeShare");
                // 调用分享接口
                // 使用 uni.$u 确保可用性，避免 this.$u 在某些上下文不可用
                if (this.$u && this.$u.api && this.$u.api.goToReduceShareAjax) {
                    this.$u.api.goToReduceShareAjax({
                        reduceCode: reduceCode
                    });
                } else {
                    // 降级尝试
                    uni.$u && uni.$u.http && uni.$u.http.get('/token/shop/order/goToShare', { reduceCode: reduceCode });
                }

                return {
                    title: "快来帮我减钱买书吧！",
                    path: "/pages/home/index?reduceCode=" + reduceCode,
                    imageUrl: "https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/share.jpg",
                    desc: "书嗨，专注于书籍交易的平台，提供新书和二手书的买卖服务",
                };
            } else {
                return {
                    title: "书嗨",
                    page: "/pages/home/index",
                };
            }
        },
        // 分享到朋友圈
        onShareTimeline(res) {
            if (res.from === "button") {
                let reduceCode = uni.getStorageSync("reduceCodeShare");
                // 调用分享接口
                if (this.$u && this.$u.api && this.$u.api.goToReduceShareAjax) {
                    this.$u.api.goToReduceShareAjax({
                        reduceCode: reduceCode
                    });
                } else {
                    uni.$u && uni.$u.http && uni.$u.http.get('/token/shop/order/goToShare', { reduceCode: reduceCode });
                }
                return {
                    title: "快来帮我减钱买书吧！",
                    path: "/pages/home/index?reduceCode=" + reduceCode,
                    imageUrl: "https://shuhi.oss-cn-qingdao.aliyuncs.com/mini/share.jpg",
                    desc: "书嗨，专注于书籍交易的平台，提供新书和二手书的买卖服务",
                };
            } else {
                return {
                    title: "书嗨",
                    page: "/pages/home/index",
                };
            }
        },
        methods: {

        }
    }
</script>

<style>
    /* Empty style as content is in the component */
</style>
