var app = getApp(); var specsManage = []; //选择池 用于排序 var optionsId = 0; //选择后的 规格ID var optionsMaxCount = 1; Page({ /** * 页面的初始数据 */ data: { language: '', imageURL: 'https://img.alicdn.com/tfscom/i2/256691439/TB2wjP9bXgkyKJjSspfXXcj1XXa_!!256691439.jpg_180x180xzq90.jpg_.webp', countDownList: [], actEndTimeList: [], groupDetail: {}, // 总的信息 goodsDetail: {}, // 商品信息 has_many_success_member: '', // 参团会员信息 has_many_success_member_count: '', // 已参团的成员数 member_num: '', // 总的参团人数 info: { has_one_group: { has_one_goods: { thumb: '', market_price: '' } }, has_one_level: { group_price: '' } }, id: '', showOpenGroup: false, // 规格弹窗 goodsInfo: {}, goodsDescription: '请选择', popPrice: 0, level_id: 0, // 当前拼团的层级id goodsCount: 1, // 购买数量 goodsId: '', activityId: '', // 活动id isOpen: 0, // 活动是否开启 status: 0, action: 0, activityTest: '', // 当前活动状态 btnTest: '', // 按钮的文字 btnStatus: false, // 按钮是否禁用 option_level_id: "", //规格层级id }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if (options.mid) { app._setMid(options.mid); } console.log(options, '参数'); specsManage = []; optionsId = 0; optionsMaxCount = 1; // let endTimeList = []; // 将活动的结束时间参数提成一个单独的数组,方便操作 this._getData(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { let language = wx.getStorageSync('langIndex'); this.setData({ 'language': language.en }); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { wx.showShareMenu({ withShareTicket: false, menus: ['shareAppMessage', 'shareTimeline'] }); console.log(app.globalData.STORE_ID,'ssssssssssssssss'); }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () {}, /** * 生命周期函数--监听页面卸载 */ onUnload: function () {}, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () {}, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () {}, /** * 用户点击右上角分享 */ onShareTimeline() { var value = wx.getStorageSync('yz_uid'); var mid = ''; if (value) { mid = value; } let _query = `id=${this.options.id}&mid=${mid}`; if(this.options.store_id){ _query = `id=${this.options.id}&store_id=${this.options.store_id}&mid=${mid}`; } return { query: _query }; }, onShareAppMessage: function (res) { var value = wx.getStorageSync('yz_uid'); var mid = ''; if (value) { mid = value; } let _path = '/packageB/member/group/GroupDetail/GroupDetail?id=' + this.options.id + '&mid=' + mid; if(this.options.store_id){ _path = '/packageB/member/group/GroupDetail/GroupDetail?id=' + this.options.id +'&store_id='+ this.options.store_id + '&mid=' + mid; } return { title: this.data.info.has_one_group.desc_title ? this.data.info.has_one_group.desc_title : this.data.info.has_one_group.share_title, path: _path }; }, tapOverlay() { this.setData({ showOpenGroup: false }); }, _getData() { let urlStr = app.getNetAddresss('plugin.fight-groups.frontend.controllers.team.index'); urlStr += '&team_id=' + this.options.id; app._getNetWork({ url: urlStr, success: (resdata) => { var res = resdata.data; if (res.result == 1) { console.log(res.data, '数据'); this.setData({ info: res.data, goodsInfo: res.data.has_one_group.has_one_goods, level_id: res.data.has_one_level.id, goodsId: res.data.has_one_group.goods_id, isOpen: res.data.has_one_group.status, status: res.data.status, action: res.data.action, activityId: res.data.has_one_group.id, has_many_success_member: res.data.has_many_success_member, has_many_success_member_count: res.data.has_many_success_member_count, member_num: res.data.has_one_level.member_num }); console.log(this.data.isOpen, '判断是否显示'); console.log(res.data, '+66+55555555555'); let endTimeList = []; let end_time = res.data.end_time * 1000; end_time = new Date(end_time); console.log(end_time, '结束时间'); endTimeList.push(end_time); this.setData({ actEndTimeList: endTimeList }); // 执行倒计时函数 this.countDown(); this.setStatusBtn(); this.initPopView(); if (this.data.goodsInfo.has_option != 1) { this.selectGroup(); // 无规格 直接根据level_id过滤数据 } } else { wx.showToast({ icon: 'none', title: res.msg, duration: 1500 }); } } }); }, // 初始化弹窗view initPopView() { if (this.data.goodsInfo.has_option == 1) { this.setData({ popPrice: this.data.info.has_one_group.min_price + '起', popStock: this.data.goodsInfo.stock, goodsDescription: '选择' }); // 设置默认价格 // 设置默认库存 let goodsDescription = '请选择'; for (let i = 0; i < this.data.goodsInfo.has_many_specs.length; i++) { goodsDescription += ' ' + this.data.goodsInfo.has_many_specs[i].title; } console.log(this.data.goodsDescription, '详情'); this.setData({ goodsDescription }); } else { this.setData({ popPrice: this.data.info.has_one_group.min_price + '起----', popStock: this.data.info.has_one_group.has_one_goods.stock, // 当前库存 }); // 设置默认价格 optionsMaxCount = this.data.info.has_one_group.has_many_options[0].buy_limit == 0 ? this.data.popStock : this.data.info.has_one_group.has_many_options[0].buy_limit; // 当前购买限制数, 为0 时为无限制购买 } }, //处理不同拼团状态下,文字显示和按钮状态 setStatusBtn() { let that = this; if (this.data.isOpen == 0) { //活动未开始 that.setData({ activityTest: "当前活动未开始", btnTest: "未开始", btnStatus: true }); } else if (this.data.isOpen == 1) { //活动已开始 if (that.data.status == 0) { //团长未支付 if (that.data.action == 1) { that.setData({ activityTest: "支付成功后拼团活动开始!", btnTest: "去支付" }); } else if (that.data.action == 4) { //不是团长,按钮去开团跳转商品详情 that.setData({ activityTest: "活动正在进行中", btnTest: "我要开团" }); } } else if (that.data.status == 1) { //团长已支付 if (that.data.action == 2) { //团长本人,显示邀请好友 that.setData({ btnTest: "邀请好友" }); } else if (that.data.action == 1) { //不是团长,未参团 但是有等待支付记录,去支付 that.setData({ btnTest: "去支付" }); } else if (that.data.action == 3) { //不是团长,未参团 that.setData({ btnTest: "我要参团" }); } } else if (that.data.status == 2 && that.data.action == 4) { //已经成团了 that.setData({ activityTest: "该团已拼团成功!", btnTest: "我要开团" }); } else if (that.data.status == 3 && that.data.action == 4) { //拼团失败 that.setData({ activityTest: "未达到拼团人数,拼团失败!", btnTest: "我要开团" }); } } else if (this.data.isOpen == 2) { //活动已结束 that.setData({ activityTest: "很抱歉,活动已结束!", btnTest: "已结束", btnStatus: true }); } else { console.log("遇到未知的活动状态,response.data.has_one_group.status"); return false; } }, // 参团 tapGoteam() { //@prames:action 支付1,邀请2,参团3,开团4 // let that = this; if (this.data.btnStatus) { //为true时,当前按钮已禁用 return false; } else if (this.data.action == 4) { //去开团 wx.navigateTo({ url: '/packageA/detail_v2/detail_v2?id=' + this.data.activityId + '/'+this.options.store_id+'&name=group' }); } else if (this.data.action == 3) { this.setData({ showOpenGroup: true }); } else if (this.data.action == 2) { console.log('分享啊啊啊'); // that.shareWeixin(); // 分享 } else if (this.data.action == 1) { //未支付 跳转到支付 wx.navigateTo({ url: "/packageD/buy/orderPay/orderPay?status=3" + '&order_id=' + this.data.info.order_id + '&name=groups' }); // that.$router.push( // that.fun.getUrl("orderpay", { // status: "2", // order_ids: that.info.order_id //-----------------等待后端接口弄好返回---------------------------- // }) // ); } }, //界面选择规格触发事件 selectSpecs(e) { let item = e.target.dataset.item || e.currentTarget.dataset.item; let index = e.target.dataset.index || e.currentTarget.dataset.index; let datas = this.data.goodsInfo; datas.has_many_specs[index].description = item.title; this.setData({ goodsInfo: datas }); // console.log("selectSpecs"); // var specid = item.specid; //对其他数据筛选 不筛选同级 //console.log(specid); //处理选择池 this.manageSpecs(item); //处理规格组合选择状态 this.setGoodsSpecs(item); //设置选择规格后的 价格、照片、库存 this.setGoodsSpecsChangeInfo(); //判断当前购买总量与库存的关系 this.getMaxCount(); }, //判断当前购买总量与库存的关系 getMaxCount() { if (specsManage.length == this.data.goodsInfo.has_many_specs.length) { // console.log(optionsMaxCount); // console.log(this.goodsCount); if (optionsMaxCount == 0) { //库存不足 this.setData({ goodsCount: 0 }); } if (this.data.goodsCount > optionsMaxCount) { this.setData({ goodsCount: optionsMaxCount }); } } }, //设置选择规格后的 价格、照片、库存 setGoodsSpecsChangeInfo() { //根据ID 排序 specsManage.sort(); specsManage.sort(function (a, b) { return a.id - b.id; }); if (specsManage.length == this.data.goodsInfo.has_many_specs.length) { let goodsSpecs = ""; for (let j = 0; j < specsManage.length; j++) { goodsSpecs += specsManage[j].id + "_"; } goodsSpecs = goodsSpecs.substring(0, goodsSpecs.length - 1); //处理"_" // console.log("goodsSpecs", goodsSpecs); for (let i = 0; i < this.data.goodsInfo.has_many_options.length; i++) { if ( goodsSpecs == this.setGoodsSpecsBySort(this.data.goodsInfo.has_many_options[i].specs) ) { this.setData({ popPrice: this.data.goodsInfo.has_many_options[i].product_price, //设置价格 popThumb: app._isTextEmpty(this.data.goodsInfo.has_many_options[i].thumb) ? this.data.goodsInfo.thumb : this.data.goodsInfo.has_many_options[i].thumb, //设置图片 popStock: this.data.info.has_one_group.has_many_options[i].stock //设置库存 }); optionsId = this.data.goodsInfo.has_many_options[i].id; //设置规格ID,用于生成订单 optionsMaxCount = this.data.goodsInfo.has_many_options[i].stock; //库存最大数 用于切换更改买家购买数量 if (optionsMaxCount > 0) { this.setData({ goodsCount: 1 }); } this.setGroupInfo(optionsId); break; } } console.log("options", optionsId); } }, //处理商品goodsSpecs 并排序 新方法 setGoodsSpecsBySort(specs) { let _specs = specs.split("_"); //先变成数组 //_specs.sort();//排序 _specs.sort(function (a, b) { return a - b; }); // 在组装回去 let goodsSpecs = ""; for (let j = 0; j < _specs.length; j++) { goodsSpecs += _specs[j] + "_"; } goodsSpecs = goodsSpecs.substring(0, goodsSpecs.length - 1); //处理"_" return goodsSpecs; }, //处理选择池 manageSpecs(data) { var specsObject = {}; specsObject.id = data.id; specsObject.specid = data.specid; specsObject.title = data.title; if (specsManage.length > 0) { for (let i = 0; i < specsManage.length; i++) { if (specsManage[i].specid == specsObject.specid) { specsManage.splice(i, 1); } } specsManage.push(specsObject); } else { specsManage.push(specsObject); } //排序 if (specsManage.length == this.data.goodsInfo.has_many_specs.length) { var newSpecsManage = []; for (let i = 0; i < this.data.goodsInfo.has_many_specs.length; i++) { for (let j = 0; j < specsManage.length; j++) { if (this.data.goodsInfo.has_many_specs[i].id == specsManage[j].specid) { newSpecsManage.push(specsManage[j]); break; } } } specsManage = newSpecsManage; } this.setGoodsDescription(); }, //处理goodsDescription 数据 setGoodsDescription() { var description = ""; //相等代表全选了 体现语句 if (specsManage.length == this.data.goodsInfo.has_many_specs.length) { description = "已选择 "; for (let i = 0; i < specsManage.length; i++) { description += specsManage[i].title + " "; } this.setData({ goodsDescription: description }); } else { description = "请选择 "; for (let i = 0; i < this.data.goodsInfo.has_many_specs.length; i++) { for (let j = 0; j < specsManage.length; j++) { if (this.data.goodsInfo.has_many_specs[i].id != specsManage[j].specid) { description += this.data.goodsInfo.has_many_specs[i].title + " "; break; } } } this.setData({ goodsDescription: description }); } console.log(this.data.goodsDescription, '参数'); }, //处理规格组合选择状态 过滤数据 setGoodsSpecs(specs) { for (let i = 0; i < this.data.goodsInfo.has_many_specs.length; i++) { if (specs.specid != this.data.goodsInfo.has_many_specs[i].id) { this.setGoodsSpecsStatus( this.data.goodsInfo.has_many_specs[i].has_many_specs_item, specs.id ); } } }, //处理规格组合选择状态 处理状态 specitem 组合数组(未选中的) id当前选中的ID 根据ID 组合算是否有当前组合 setGoodsSpecsStatus(specitem, id) { // console.log(specitem); // console.log(id); let options = []; //数据池 for (let i = 0; i < this.data.goodsInfo.has_many_options.length; i++) { // console.log(this.goodsInfo.has_many_options[i].specs); let _specs = this.data.goodsInfo.has_many_options[i].specs.split("_"); //console.log(_specs); //判断是否包含 for (let j = 0; j < _specs.length; j++) { if (_specs[j] == id) { options.push(this.data.goodsInfo.has_many_options[i]); return; } } } //关键处理方式 后期要优化 效率低 for (let m = 0; m < options.length; m++) { let _specs = options[m].specs.split("_"); for (let y = 0; y < _specs.length; y++) { if (_specs[y] != id && options[m].stock == 0) { for (let n = 0; n < specitem.length; n++) { if (_specs[y] == specitem[n].id) { specitem[n].c = true; return; } } } else if (_specs[y] != id && options[m].stock > 0) { for (let n = 0; n < specitem.length; n++) { if (_specs[y] == specitem[n].id) { specitem[n].c = false; return; } } } } } console.log(options); }, //过滤出当前层级的数据 setGroupInfo(_id) { let that = this; // console.log("组合规格id", _id); for ( let i = 0; i < this.data.info.has_one_group.has_many_options.length; i++ ) { let _optionSpecs = this.data.info.has_one_group.has_many_options[i] .option_id; //判断是否包含 if (_optionSpecs == optionsId) { // options.push(this.goodsInfo.has_many_options[i]); that.setData({ selectGroupInfo: that.data.info.has_one_group.has_many_options[i].has_many_option_level, popStock: that.data.info.has_one_group.has_many_options[i].stock }); optionsMaxCount = that.data.info.has_one_group.has_many_options[i].buy_limit == 0 ? that.data.popStock : that.data.info.has_one_group.has_many_options[i].buy_limit; //当前组合的购买限制数, 为0 时为无限制购买 this.selectGroup(); return; } } }, //选择规格后 过滤出当前层级的数据 selectGroup(_level_id) { let that = this; var selectGroupLever = []; if (this.data.goodsInfo.has_option == 1) { //有规格 selectGroupLever = this.data.selectGroupInfo.filter(function (v) { if (v.level_id == that.data.level_id) { return v; } }); console.log(selectGroupLever, '98655465445'); this.setData({ popPrice: selectGroupLever[0].group_price }); this.setData({ option_level_id: selectGroupLever[0].id }); } else { //无规格 let datas = this.data.info.has_one_group.has_many_options[0].has_many_option_level; this.setData({ selectGroupInfo: datas }); console.log(this.data.selectGroupInfo, '对象'); selectGroupLever = this.data.selectGroupInfo.filter(function (v) { if (v.level_id == that.data.level_id) { return v.group_price; } }); console.log(selectGroupLever, '东西'); this.setData({ popPrice: selectGroupLever[0].group_price }); this.setData({ option_level_id: selectGroupLever[0].id }); } }, //增加 addGoods() { console.log("增加", this.popStock, optionsMaxCount); if (this.data.goodsInfo.has_option == 1 && app._isTextEmpty(optionsId)) { console.log("请选择商品规格!"); wx.showToast({ title: '请选择商品规格!', icon: 'none' }); return; } if(!this.data.popStock){ wx.showToast({ title: '库存不足', icon: 'none' }); return; } if (this.data.goodsCount > this.data.popStock) { wx.showToast({ title: '库存不足', icon: 'none' }); this.setData({ goodsCount: this.data.popStock }); return; } if (optionsMaxCount != 0 && this.data.goodsCount >= optionsMaxCount) { wx.showToast({ title: `抱歉,该商品限购${optionsMaxCount}件`, icon: 'none' }); this.setData({ goodsCount: optionsMaxCount }); return; } let num = this.data.goodsCount; num++; this.setData({ goodsCount: num }); }, //减少 reduceGoods() { if (this.data.goodsCount == 1 || this.data.goodsCount == 0) { return; } let im = this.data.goodsCount; im--; this.setData({ goodsCount: im }); }, changeCount() { if(!this.data.popStock){ wx.showToast({ title: '库存不足', icon: 'none' }); return; } if (this.data.goodsCount > this.data.popStock) { wx.showToast({ title: '库存不足', icon: 'none' }); this.setData({ goodsCount: this.data.popStock }); return; } if (optionsMaxCount != 0 && this.data.goodsCount > optionsMaxCount) { wx.showToast({ title: '数量超出范围', icon: 'none' }); this.setData({ goodsCount: optionsMaxCount }); return; } if (this.data.goodsCount <= 0) { wx.showToast({ title: '商品数量不能小于0', icon: 'none' }); this.setData({ goodsCount: 1 }); return; } }, joinTeam() { let that = this; if (this.data.goodsInfo.has_option == 1 && app._isTextEmpty(optionsId)) { console.log("请选择商品规格!"); wx.showToast({ title: '请选择商品规格!', icon: 'none' }); return; } if(!this.data.popStock){ wx.showToast({ title: '库存不足', icon: 'none' }); return; } if (this.data.goodsCount > this.data.popStock) { wx.showToast({ title: '库存不足', icon: 'none' }); this.setData({ goodsCount: this.data.popStock }); return; } if (optionsMaxCount != 0 && this.data.goodsCount > optionsMaxCount) { wx.showToast({ title: '数量超出范围', icon: 'none' }); this.setData({ goodsCount: optionsMaxCount }); return; } if (this.data.goodsCount <= 0) { wx.showToast({ title: '商品数量不能小于0', icon: 'none' }); this.setData({ goodsCount: 1 }); return; } if (this.data.goodsInfo.has_option == 0) { optionsId = 0; } let goods = []; goods.push(this.data.goodsId); let urlStr = app.getNetAddresss("from.div-from.isDisplay"); urlStr += "&goods_ids=" + JSON.stringify(goods); app._getNetWork({ showToastIn: false, url: urlStr, success: function (resdata) { var res = resdata.data; if (res.result == 1) { // var _data = res.data; if (res.data.status && !res.data.member_status) { wx.showModal({ title: '提示', content: '购买此商品,请补充您的个人信息', success(res) { if (res.confirm) { //保税真实信息添加 wx.navigateTo({ url: '/packageB/member/address/myinfo/myinfo?tag=delta' }); } else if (res.cancel) { console.log('用户点击取消'); } } }); } else { let _store_id = 0; if(that.options.store_id&&that.options.store_id != undefined&&that.options.store_id != 0){ _store_id = that.options.store_id; } wx.navigateTo({ url: '/packageD/buy/myOrder_v2/myOrder_v2?tag=-2' + '&goodsId=' + that.data.goodsId + '&optionsId=' + optionsId + '&total=' + that.data.goodsCount + '&team_id=' + that.options.id + '&option_level_id=' + that.data.option_level_id + '&groupStoreID=' + _store_id }); } } else { wx.showToast({ icon: 'none', title: res.msg, duration: 1500 }); } }, fail: function (res) {} }); }, timeFormat(param) { // 小于10的格式化函数 return param < 10 ? '0' + param : param; }, countDown() { // 倒计时函数 // 获取当前时间,同时得到活动结束时间数组 let newTime = new Date().getTime(); let endTimeList = this.data.actEndTimeList; let countDownArr = []; // 对结束时间进行处理渲染到页面 endTimeList.forEach(o => { let endTime = new Date(o).getTime(); let obj = null; // 如果活动未结束,对时间进行处理 if (endTime - newTime > 0) { let time = (endTime - newTime) / 1000; // 获取天、时、分、秒 let day = parseInt(time / (60 * 60 * 24)); let hou = parseInt(time % (60 * 60 * 24) / 3600); let min = parseInt(time % (60 * 60 * 24) % 3600 / 60); let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60); obj = { day: this.timeFormat(day), hou: this.timeFormat(hou), min: this.timeFormat(min), sec: this.timeFormat(sec) }; this.setData({ istime: false }); } else { // 活动已结束,全部设置为'00' obj = { day: '00', hou: '00', min: '00', sec: '00' }; this.setData({ istime: true }); } countDownArr.push(obj); }); // 渲染,然后每隔一秒执行一次倒计时函数 this.setData({ countDownList: countDownArr }); setTimeout(this.countDown, 1000); }, // 去商城 goHome() { wx.reLaunch({ url: '/packageG/index/index' }); }, gotoGoods() { wx.navigateTo({ url: "/packageA/detail_v2/detail_v2?id=" + this.data.activityId +'/'+this.options.store_id+ '&name=group' }); // this.$router.push( // this.fun.getUrl("goods", { id: this.activityId }, { name: "group" }) // ); }, });