CarOwner.js 10.5 KB
import {
    discoveryApi,
    shopmallApi,
    couponsApi
} from "~/api/index";
import {
    decimal,
    centsToyuan
} from '~/utils/format'
import { eventbus } from '~/utils/index'
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        height: null,
        // channelID: {
        //     type: Number,
        //     observer: function (val) {
        //         if (val) {
        //             // this.setList(val);
        //         }
        //     },
        // },
        // merchantid: {
        //     type: [String, Number],
        //     observer: function (val) {
        //         if (val) {
        //             this.init()
        //             // this.setList(val);
        //         }
        //     },
        // },
    },

    /**
     * 组件的初始数据
     */
    data: {
        curCategoryIdx: 0,
        hasCarOwnerCouponFlag: false,
        toCategory: 'toCategory-0',
        categoryList: [],
        categoryStyle: -1,
        goodsList: [],
        goodsItemHeight: 0,
        goodsItemTopList: [],
        curPlayIdx: 0
        // leftList: [],
        // rightList: [],
        // leftHeight: 0,
        // rightHeight: 0,
    },

    /**
     * 组件的方法列表
     */
    methods: {
        toSelectCategory(e) {
            const {
                index,
                item
            } = e.currentTarget.dataset
            this.setData({
                curCategoryIdx: index,
                toSeri: `toCategory-${index}`,
                categoryStyle: item.categoryStyle || 0
            })
            // this.triggerEvent('showShoppingCar', item.categoryStyle)
            const {
                merchantId
            } = wx.p.storage.get('merchant_info')
            this.getCategoryGoodsList(item.id, merchantId)
        },
        //获取分类
        getCategoryList() {
            const {
                merchantId
            } = wx.p.storage.get('merchant_info')
            const _params = {
                merchantId,
                categoryType: '2'
            }
            discoveryApi.getDiscoverChannelCategory(_params).then(res => {
                if (res?.length) {
                    this.setData({
                        categoryList: res,
                        categoryStyle: res[0].categoryStyle || 0
                    })
                    this.getCategoryGoodsList(res[0].id, merchantId)
                } else {
                    this.setData({
                        categoryList: [],
                        categoryStyle: 0,
                        goodsList: []
                    })
                }
            })
        },
        //获取商品列表
        async getCategoryGoodsList(categoryId, merchantId) {
            let _para = {
                merchantId,
                categoryId,
                categoryType: '2'
            }
            const res = await discoveryApi.getDiscoverChannelCategoryGoods(_para)
            // console.log(res)
            if (res && res.length) {
                res.forEach(el => {
                    el.activityPrice = centsToyuan(el.activityPrice)
                    el.originalPrice = centsToyuan(el.originalPrice)
                })
            }
            const idx = res.findIndex(item => (item.commerceVideo || item.goodsVideo))
            this.setData({
                goodsList: [...res] || [],
                curPlayIdx: idx > -1 ? idx : 0
            })
        },
        scrollFn(e) {
            // console.log(e.scrollTop)
            // try {
            // let idx = this.data.goodsItemTopList.findIndex(item => e.scrollTop+300 < item)
            // this.setData({
            //     curPlayIdx: idx >-1 ? idx : 0
            // })
            // } catch (error) {
            //     console.log(error)
            // }

            // let curPlayId = 'video' + this.data.curPlayIdx
            // let videoContext = wx.createvideoContext(curPlayId);
            // videoContext.stop();
            if (this.data.categoryStyle != 3) return;
            const query = wx.createSelectorQuery().in(this)
            query.selectAll('.goods_box').boundingClientRect((rect) => {
                if (rect?.length) {
                    let curPlayIdx = -1
                    let goodsItemTopList = rect.map(item => item.top)
                    let idx = goodsItemTopList.findIndex(item => item > 300)
                    curPlayIdx = idx - 1 > -1 ? idx - 1 : 0
                    const _targetItem = this.data.goodsList[curPlayIdx] || {}
                    if (!(_targetItem?.commerceVideo || _targetItem?.goodsVideo)) {
                        for (let i = idx; i < this.data.goodsList.length; i++) {
                            if (this.data.goodsList[i]?.commerceVideo || this.data.goodsList[i]?.goodsVideo) {
                                curPlayIdx = i
                                break;
                            }
                        }
                    }
                    this.setData({
                        curPlayIdx: curPlayIdx > -1 ? curPlayIdx : 0
                    })
                }
            }).exec();
        },
        // 跳转商品详情
        toGoodsDetail(e) {
            const {
                item
            } = e.currentTarget.dataset
            const B_userInfo = wx.p.storage.get('userInfo')
            this.jumpEvent = true
            if (B_userInfo && B_userInfo.userType) {
                wx.navigateTo({
                    url: '/pages/Login/Login'
                })
                return
            }
            if (item.commerceVideo) {
                const { coverUrl, commerceVideo, commerceDescribe, activityPrice, originalPrice, goodsName, goodsImage, id } = item
                wx.p.router.go(`/packageMall/pages/goodsVideo/goodsVideo`, {
                    id,
                    notIntercept: true,
                    coverUrl: goodsImage,
                    commerceVideo,
                    commerceDescribe,
                    activityPrice,
                    originalPrice,
                    goodsName,
                    hasCouponFlag: this.data.hasCarOwnerCouponFlag
                });
            } else {
                wx.p.router.go('/packageMall/pages/detail/detail', {
                    id: item.id,
                    hasCouponFlag: this.data.hasCarOwnerCouponFlag,
                    isCarOwner: true,
                    notIntercept: true
                })
            }
        },
        // imageLoadError(e) {
        //     this.imageLoaded()
        // },
        // imageLoaded(e) {
        //     console.log(e)
        //     let { goodsList, leftList, rightList } = this.data
        //     if (!goodsList?.length) return;
        //     let leftHeight = 0
        //     let rightHeight = 0
        //     const query = wx.createSelectorQuery().in(this)
        //     query.select('.left-box').boundingClientRect();
        //     query.select('.right-box').boundingClientRect();
        //     query.exec((res) => {
        //         if (res[0]) {
        //             leftHeight = res[0].height; //获取左边列表的高度
        //             rightHeight = res[1].height; //获取右边列表的高度
        //         }
        //         console.log(leftHeight, rightHeight)
        //         const item = goodsList.shift()
        //         leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item);
        //         this.setData({
        //             leftList,
        //             rightList
        //         })
        //     });
        // },
        // 获取购物车列表
        // async getBuyCarList() {
        //     const authStatus = await wx.p.router.intercept(false)
        //     if (authStatus) return
        //     const B_userInfo = wx.p.storage.get('userInfo')
        //     if (B_userInfo && B_userInfo.userType) return;
        //     const {
        //         merchantId
        //     } = wx.p.storage.get('merchant_info')
        //     const res = await shopmallApi.getShoppingcartList({
        //         cartType: 4,
        //         merchantId
        //     })
        //     eventbus.emit('getBuyCarList', res)
        // },
        // 加入购物车
        async toAddBuuCar(e) {
            const {
                item
            } = e.currentTarget.dataset
            const authStatus = await wx.p.router.intercept()
            if (authStatus) return
            this.jumpEvent = true
            if (this.data.hasCarOwnerCouponFlag) {
                wx.p.router.go(`/packageMall/pages/getCoupon/getCoupon?discoverGoodsType=2&from=2`, { notIntercept: true });
                return
            }
            const deliveryType = item.deliveryType == 2 ? 0 : 1
            const goodsListItem = {
                ...item,
                goodsNumber: 1,
                goodsId: item.id,
            }
            let _para = {
                goodsList: [goodsListItem],
                isCarOwner: true,
                showAddr: deliveryType == 0 ? 1 : 0,
                buyType: 'pay',
                discoverGoodsSource: item.discoverGoodsSource,
                deliveryType: 0
            }
            wx.p.router.go('/packageDiscovery/pages/pay/index', _para)
            // const res = await shopmallApi.addShoppingcart(_para)
            // if (res) {
            //     wx.p.wxApi.showToast('添加成功')
            //     this.getBuyCarList()
            // }
        },
        getCarOwnerCouponList() {
            const {
                merchantId
            } = wx.p.storage.get('merchant_info')
            couponsApi.getMerchantCouponsListApi({ merchantId, discoverGoodsType: 2 }).then(res => {
                if (res?.length) {
                    this.setData({
                        hasCarOwnerCouponFlag: !!res?.length
                    })
                }
            })
        },
        onShow() {
            // this.getBuyCarList()
            this.init()
        },
        init() {
            if (!this.jumpEvent) {
                this.getCategoryList()
            }
            // const B_userInfo = wx.p.storage.get('userInfo')
            // if (B_userInfo && B_userInfo.userType) return
            // this.getBuyCarList()
            this.getCarOwnerCouponList()
        }
    },
    lifetimes: {
        ready: function () {
            wx.p.eventbus.on('MALL_FRESH', () => {
                this.getCategoryList()
                this.setData({
                    curCategoryIdx:0,
                    curPlayIdx:0,
                    toCategory: 'toCategory-0',
                })
            })
        }
    }
})