// pages/goods-details/index.js var app = getApp() var WxParse = require('../../wxParse/wxParse.js') Page({ /** * 页面的初始数据 */ data: { hideShopPopup: true, //规格弹出框 indicatorDors: true, autoplay: true, interval: 4000, duration: 500, goodsDetail:{}, //商品详情对象 buyNumMax:0, //最大购买数量 buyNumMin: 0, //最小购买数量 buyNumber: 0, //单个购买数量 shopNum: 0, //全部购买数量 propertyChildIds: '',//规格属性id propertyChildNames: '',//规格属性名 selectSizePrice: 0,//单个选择商品价格 selectSize: '选择: ',//选择规格 hasMoreSelect: false,//是否有多种规格可选 shopCarInfo: {},//设置在缓存的订单信息 canSubmit: false,//是否能添加到订单 imgurl: app.data.resturl, comments: [], id:'', zanstatus: 'N', znum: 0 }, onLoad:function(options){ this.setData({ id:options.id }) }, /** * 生命周期函数--监听页面加载 */ onShow: function (options) { var that = this //加载缓存,取得订单信息 wx.getStorage({ key: 'shopCarInfo', success:function(res){ // console.log(`initshopCarInfo:${res.data}`) that.setData({ shopCarInfo: res.data, shopNum: res.data.shopNum }) } }) let id = this.data.id; let openid = app.globalData.openid; //请求该商品详细数据 wx.request({ url: app.data.resturl + '/ginfo.action', data: { id: id, openid:openid }, success:function(res){ // console.log(res.data.data) let selectSizeTemp = '' that.setData({ goodsDetail: res.data.dinfo, znum: res.data.dinfo.zan, zanstatus: res.data.dinfo.praiseStatus, comments: res.data.dlist, buyNumber:1 }) WxParse.wxParse('article', 'html', res.data.dinfo.goodsMiaoshu, that, 5) } }) }, addShopCar: function() { if (this.data.goodsDetail.properties && !this.data.canSubmit){ this.bindGuiGeTap() return } let shopCarMap = {} shopCarMap.goodsId = this.data.goodsDetail.goodsId; shopCarMap.pic = this.data.imgurl + this.data.goodsDetail.goodsPic; shopCarMap.name = this.data.goodsDetail.goodsName; shopCarMap.label = this.data.goodsDetail.goodsName; shopCarMap.price = this.data.goodsDetail.goodsShichangjia shopCarMap.left = '' shopCarMap.active = true shopCarMap.number = this.data.buyNumber var shopCarInfo = this.data.shopCarInfo if (!shopCarInfo.shopNum){ shopCarInfo.shopNum = 0 } if(!shopCarInfo.shoplist){ shopCarInfo.shoplist= [] } var hasSameGoodsIndex = -1 for (var i = 0; i < shopCarInfo.shoplist.length;i++){ var tamShopCarMap = shopCarInfo.shoplist[i] if (tamShopCarMap.goodsId === shopCarMap.goodsId){ shopCarMap.number = shopCarMap.number + tamShopCarMap.number hasSameGoodsIndex = i break } } shopCarInfo.shopNum = shopCarInfo.shopNum + this.data.buyNumber if (hasSameGoodsIndex > -1) { shopCarInfo.shoplist.splice(hasSameGoodsIndex, 1, shopCarMap) } else{ shopCarInfo.shoplist.push(shopCarMap) } this.setData({ shopCarInfo: shopCarInfo, shopNum: shopCarInfo.shopNum }) this.closePopupTap() // wx.showToast({ // title: '加入购物车成功', // icon: 'sucess', // duration: 2000 // }) wx.setStorage({ key: 'shopCarInfo', data: shopCarInfo }) }, bindGuiGeTap(){ this.setData({ hideShopPopup: false }) }, closePopupTap(){ this.setData({ hideShopPopup: true }) }, numJiaTap(){ let buyNumber = this.data.buyNumber if (buyNumber < this.data.buyNumMax){ buyNumber++ } console.log(buyNumber) this.setData({ buyNumber: buyNumber }) }, numJianTap() { let buyNumber = this.data.buyNumber if (buyNumber > this.data.buyNumMin) { buyNumber-- } this.setData({ buyNumber: buyNumber }) }, goShopCar() { wx.reLaunch({ //url: '../shop-cart/index' url: '../to-pay-order/index' }) }, tobuy(){ if (this.data.goodsDetail.properties && !this.data.canSumbit){ this.bindGuiGeTap() return } if (this.data.buyNumber<1){ wx.showModal({ title: '提示', concent:'暂时缺货哦~', showCancel:fasle }) return } this.addShopCar() this.goShopCar() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, zan: function () { let openid = app.globalData.openid; if (openid == '') { wx.redirectTo({ url: '../../pages/login/login', }) return; } let id = this.data.goodsDetail.goodsId; let zstatus = this.data.zanstatus; let zcount = this.data.znum; let types = 1; if (zstatus == 'Y') { types = 2; } let param = "tcid=" + id + "&uid=" + openid + "&types=" + types; //console.log(param); let that = this; wx.request({ url: app.data.resturl + '/praise.action', header: { 'content-type': 'application/x-www-form-urlencoded' }, method: 'post', data: param, success: function (resp) { let rescode = resp.data.code; if (rescode == 200) { if (zstatus == 'N') { zstatus = 'Y'; zcount = zcount + 1; } else { zstatus = 'N'; zcount = zcount - 1; if (zcount < 0) { zcount = 0; } } that.setData({ zanstatus: zstatus, znum: zcount }); } } }) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })