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

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

    /**
     * 组件的方法列表
     */
    methods: {
         // 前往视频列表
    toVideoPage(e) {
        const {
            index
            } = e.currentTarget.dataset
            let temp = JSON.stringify(this.data.videoList)
            wx.navigateTo({
                url: `/packageDiscovery/pages/VideoPlayList/index?list=${temp}&index=${index}`
            });
        },
        toSelectCategory(e) {
            const {
                index,
                item
            } = e.currentTarget.dataset
            this.setData({
                curCategoryIdx: index,
                toSeri: `toCategory-${index}`,
                categoryStyle: item.categoryStyle || 0
            })
            if(item.isCaseVideo) return;
            this.triggerEvent('showShoppingCar', item.categoryStyle)
            this.getCategoryGoodsList(item.id)
        },
        init(id) {
            wx.$api.showLoading('加载中')
            const merchantId = wx.$storage.get("srcm_shopmall_merchant_id");
            discoveryApi.getDiscoverChannelDetail({
              id,
              merchantId
            }).then((res) => {
              wx.hideLoading();
              if (res) {
                const {
                  discoverCaseList
                } = res;
                let videoList = []
                discoverCaseList.forEach(el => {
                  videoList.push(el.mediaInfo.mediaUrl)
                })
                this.setData({
                    videoList,
                    discoverCaseList : discoverCaseList || []
                });
              }
            }).finally(end => {
                this.getCategoryList()
            });
        },
        //获取分类
        getCategoryList() {
            const merchantId = wx.$storage.get("srcm_shopmall_merchant_id");
            const _params = {
                merchantId,
                categoryType: '1'
            }
            discoveryApi.getDiscoverChannelCategory(_params).then(res => {
                if (res?.length) {
                    const { discoverCaseList } = this.data
                    const caseItem = {
                        categoryStyle: -1,
                        isCaseVideo: 1,
                        categoryName: '案例视频',
                        categoryImage: 'https://img02.glsx.com.cn/weapp/resource/miniprogram/glsx/discover/version/3_13_0/case.png'
                    }
                    let categoryStyle  = discoverCaseList?.length ? -1: res[0].categoryStyle
                    this.setData({
                        categoryList: discoverCaseList?.length ? [caseItem, ...res] : res,
                        categoryStyle: categoryStyle || 0
                    })
                    this.triggerEvent('showShoppingCar', categoryStyle)
                    if(!discoverCaseList?.length) this.getCategoryGoodsList(res[0].id)
                }
            })
        },
        //获取商品列表
        async getCategoryGoodsList(categoryId) {
            const merchantId = wx.$storage.get("srcm_shopmall_merchant_id");
            let _para = {
                merchantId,
                categoryId,
                categoryType: '1'
            }
            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.$storage.get('userInfo')
            if (B_userInfo && B_userInfo.userType) {
              wx.navigateTo({
                url: '/pages/Login/Login'
              })
              return
            }
            wx.$router.go('/packageShopmall/packageMall/pages/detail/detail', {
                id: item.id,
                hasCouponFlag: this.data.hasUpgradeCouponFlag,
                isUpgrade: true,
                notIntercept: true
            })
        },
        // 获取购物车列表
        async getBuyCarList() {
            const B_userInfo = wx.$storage.get('userInfo')
            if (B_userInfo && B_userInfo.userType) return;
            const merchantId = wx.$storage.get('srcm_shopmall_merchant_id')
            const res = await shopmallApi.getShoppingcartList({
                cartType: 4,
                merchantId
            })
            wx.$eventbus.emit('getBuyCarList', res)
        },
        // 加入购物车
        async toAddBuuCar(e) {
            const {
                item
            } = e.currentTarget.dataset
            let _para = {
            goodsList: {
                ...item,
                goodsNumber: 1
            },
            deliveryType: 1,
            isUpgrade:true
            }
            if (wx.$authStatus) {
                this.showNeedQuery = _para
                if (wx.$authStatus) {
                    if(this.data.hasUpgradeCouponFlag){
                        wx.$router.go(`/packageShopmall/packageMall/pages/getCoupon/getCoupon?discoverGoodsType=1&from=1`, { notIntercept:true });
                    }else{
                        wx.navigateTo({
                            url: '/pages/Login/Login'
                        })
                    }
                    return
                }
                return
            }
            wx.$router.go('/packageDiscovery/pages/pay/index', _para)
        },
        getUpgradeCouponList(){
            const merchantId = wx.$storage.get('srcm_shopmall_merchant_id')
            couponsApi.getMerchantCouponsListApi({merchantId, discoverGoodsType: 1}).then(res => {
                if(res?.length){
                    this.setData({
                        hasUpgradeCouponFlag: !!res?.length
                    })
                } 
            })
        },
        onShow(){
            this.getBuyCarList()
            
        }
    },
    lifetimes: {
        ready: function () {
            const B_userInfo = wx.$storage.get('userInfo')
            if(B_userInfo && B_userInfo.userType)return
            this.getBuyCarList()
            this.getUpgradeCouponList()
        }
    }
})