select_index.js 13.2 KB
// pages/index/select_index/select_index.js
const app = getApp();
Page({

        /**
         * 页面的初始数据
         */
        data: {
                ids: [],
                goods: [],
                imgUrl: app.globalData.imgUrl,
                wangge: 0,
                show_cart_mask: false,
                value: '',
                goods_item: [],
                goods_id: 0,
                skuid_price: 0,
                old_skuid_price: 0,
                vip_skuid_price: 0,
                skuid_arr: [],
                skuid: 0,
                item: 1,
                sku_values: ''
        },
        changeIds(e) {
                let t = this;
                let goods_item = t.data.goods_item;
                let listAll = t.data.goods_item.attr.attr_sku;
                let list = listAll[e.currentTarget.dataset.parent].item;
                let x = e.currentTarget.dataset.index;
                // 当前点击Id
                let nowId = e.currentTarget.dataset.id;
                //当前点击value
                let nowValue = e.currentTarget.dataset.item;
                //  父级索引
                let parent = e.currentTarget.dataset.parent;
                // 子集索引
                let nowIndex = e.currentTarget.dataset.index;
                // 当前可否多选
                let is_checkMore = false
                let new_arr = t.data.skuid_arr
                let foo = new_arr.indexOf(nowId)
                if (foo > -1) {
                        new_arr.splice(foo, 1)
                } else {
                        goods_item.attr.attr_sku.forEach(function(ele, indexxxx) {
                                if (indexxxx == parent) {
                                        for (let i = 0; i < ele.item.length; i++) {
                                                let havId = goods_item.attr.attr_sku[indexxxx].item[i].id
                                                let have = new_arr.indexOf(havId)
                                                if (have > -1) {
                                                        new_arr.splice(have, 1)
                                                }
                                        }
                                }
                        })
                }
                // 当前点击父级值
                let tempArry = JSON.parse(JSON.stringify(this.data.goods_item.attr.attr_sku[parent]));
                !tempArry.chooseIndex ? tempArry.chooseIndex = [] : '';
                !tempArry.chooseValue ? tempArry.chooseValue = [] : '';
                // 是否多选
                if (is_checkMore) {} else {
                        tempArry.chooseValue.pop();
                        tempArry.chooseValue.push(nowValue);
                        tempArry.chooseIndex.pop();
                        console.log(tempArry.chooseIndex)
                        tempArry.chooseIndex.push(nowId);
                        for (let obj of tempArry.item) {
                                obj.t = false
                        }
                        tempArry.item[nowIndex].t = true;
                }
                this.data.goods_item.attr.attr_sku[parent] = tempArry;
                this.setData({
                        goods_item: this.data.goods_item
                })
                let sys_attrprice = goods_item.sys_attrprice;
                //     let new_arr = []
                // let new_arr_item=[];
                let str = ''
                let skuid = 0
                let skuid_price = 0
                let old_skuid_price = 0
                let vip_skuid_price = 0
                let sku_values = ''
                goods_item.attr.attr_sku.forEach(function(ele, indexx) {
                        if (indexx == parent) {
                                new_arr = new_arr.concat(ele.chooseIndex);
                        }
                })
                new_arr = new_arr.sort()
                str = new_arr.join('_');
                console.log(new_arr)
                console.log(str + "组合后的str")
                for (let i in sys_attrprice) {
                        if (i == str) {
                                skuid = sys_attrprice[i].skuid
                                skuid_price = sys_attrprice[i].price
                                old_skuid_price = sys_attrprice[i].old_price
                                vip_skuid_price = sys_attrprice[i].vip_price
                                sku_values = sys_attrprice[i].sku_values
                        }
                }
                this.setData({
                        idss: new_arr,
                        skuid: skuid,
                        skuid_price: skuid_price,
                        old_skuid_price: old_skuid_price,
                        vip_skuid_price: vip_skuid_price,
                        skuid_arr: new_arr,
                        sku_values: sku_values,
                });
                console.log(skuid + "组合后的id值")
        },
        //点击添加购物车数量
        dec_item() {
                let item = this.data.item;
                if (item <= 1) {
                        wx.showToast({
                                title: '已经是最少了哦',
                                icon: 'none'
                        })
                        return;
                }
                item--;
                this.setData({
                        item: item
                });
        },
        add_item() {
                let item = this.data.item;
                item++;
                this.setData({
                        item: item
                });
        },
        //根据传过来的ids获取渲染数据
        fetchGoodsLists() {
                let ids = this.data.ids;
                let url = '/wxapp/product/getProductListByScreen'
                let params = {
                        ids: this.data.ids,
                }
                app.post(url, params).then(r => {
                        this.setData({
                                goods: r.data.list
                        });
                        console.log(this.data.goods)
                });
        },
        //切换网格列表状态
        changeWangGe0() {
                this.setData({
                        wangge: 0
                });
        },
        changeWangGe1() {
                this.setData({
                        wangge: 1
                });
        },
        changeWangGe2() {
                this.setData({
                        wangge: 2
                });
        },
        // 跳转商品详情
        get_goodsDetial(e) {
                wx.navigateTo({
                        url: '/pages/index/goodsDetial/goodsDetial?id=' + e.currentTarget.dataset.goods
                })
        },
        //点击商品列表显示加购弹框
        show_cart_mask(e) {
                let id = e.currentTarget.dataset.goods;
                this.setData({
                        show_cart_mask: true,
                        goods_id: id
                });
                this.fetchGoodsData();
        },
        //点击加入购物车
        jump_cart_id(e) {
                let id = this.data.goods_id
                let url = '/wxapp/cart/addCart'
                let params = {
                        skuid: this.data.skuid,
                        num: this.data.item
                }
                if (e.currentTarget.dataset.limit >= this.data.item || e.currentTarget.dataset.limit == 0) {
                        app.post(url, params).then(r => {
                                console.log(r)
                                if (r.code == 1) {
                                        wx.showToast({
                                                title: '添加购物车成功',
                                                icon: 'none'
                                        })
                                } else {
                                        wx.showToast({
                                                title: r.msg,
                                                icon: 'none'
                                        })
                                }
                        });
                } else {
                        wx.showToast({
                                title: '限购' + e.currentTarget.dataset.limit + '件',
                                icon: 'none'
                        })
                }
                this.setData({
                        show_cart_mask: false,
                        item: 1
                });

        },
        fetchGoodsData() {
                let id = this.data.goods_id;
                let url = '/wxapp/product/getProductInfo?id=' + id
                let sku_values = ''
                app.post(url).then(r => {
                        if (r.code == 1) {
                                console.log(r.data)
                                for (let i = 0; i < r.data.attr.attr_sku.length; i++) {
                                        sku_values += r.data.attr.attr_sku[i].item[0].attribute_value
                                        for (let x = 0; x < r.data.attr.attr_sku[i].item.length; x++) {
                                                r.data.attr.attr_sku[i].item[x].t = false;
                                        }
                                }
                                let skuid = 0;
                                let skuid_price = 0
                                let old_skuid_price = 0
                                let vip_skuid_price = 0
                                let arr = []
                                r.data.attr.attr_sku.forEach(function(ele, index) {
                                        ele.item[0].t = true;
                                        arr.push(ele.item[0].id)
                                })
                                arr = arr.sort()
                                let str = arr.join('_');
                                let sys_attrprice = r.data.sys_attrprice
                                for (let i in sys_attrprice) {
                                        if (i == str) {
                                                skuid = sys_attrprice[i].skuid
                                                skuid_price = sys_attrprice[i].price
                                                old_skuid_price = sys_attrprice[i].old_price
                                                vip_skuid_price = sys_attrprice[i].vip_price
                                        }
                                }
                                this.setData({
                                        goods_item: r.data,
                                        skuid_price: skuid_price,
                                        old_skuid_price: old_skuid_price,
                                        vip_skuid_price: vip_skuid_price,
                                        skuid: skuid,
                                        skuid_arr: arr,
                                        sku_values: sku_values
                                });
                        } else {
                                wx.showToast({
                                        title: r.msg,
                                        icon: 'none'
                                })
                        }
                });
        },
        //关闭加购框
        close_jump_msk() {
                this.setData({
                        show_cart_mask: false
                });
        },
        //关闭筛选
        close_all() {
                this.setData({
                        flag: !this.data.flag
                });
                let ids = this.data.ids;
                console.log(ids);
                if (ids == undefined) {
                        wx.showToast({
                                title: '请选择属性',
                                icon: 'none'
                        })
                        return;
                } else {
                        wx.navigateTo({
                                url: '../select_index/select_index?ids=' + ids + '&keyword=' + this.data.value
                        })
                }

        },

        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function(options) {
                this.setData({
                        ids: options.idss,
                });
                this.fetchGoodsLists()
        },

        /**
         * 生命周期函数--监听页面初次渲染完成
         */
        onReady: function() {

        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function() {

        },

        /**
         * 生命周期函数--监听页面隐藏
         */
        onHide: function() {

        },

        /**
         * 生命周期函数--监听页面卸载
         */
        onUnload: function() {

        },

        /**
         * 页面相关事件处理函数--监听用户下拉动作
         */
        onPullDownRefresh: function() {

        },

        /**
         * 页面上拉触底事件的处理函数
         */
        onReachBottom: function() {

        },

        /**
         * 用户点击右上角分享
         */
        onShareAppMessage: function() {

        }
})