// pages/details/details.js const getReq = require('./../../config.js').getReq; const app = getApp(); Page({ /** * 页面的初始数据 */ data: { imgUrls: [], datas: {}, getOneSummary: [], webViewUrl: '', indicatorDots: true, vertical: false, autoplay: true, interval: 2000, duration: 500, animation_flag: false, sec_index: 0, goodsNumber: 1, cartOrBuy: '', getgift: '', options_goods_id: '' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { let webViewUrl = `https://passport.lrlz.com/mobile/index.php?act=goods_common&op=detail&goods_id=${options.goods_id}&client_type=ios` this.setData({ webViewUrl, options_goods_id: options.goods_id }) this.getDatas(options.goods_id) }, getDatas(goods_id) { wx.showLoading({ title: '加载中', }) var self = this getReq({ act: 'goods_common', op: 'index', goods_id }, function (res) { wx.hideLoading() if (res.code == 200) { let oneSummary = self.getOneSummary(res.datas.summary, goods_id) let getgift = self.getgift(res.datas.summary, goods_id) let differencePrice = (oneSummary.goods_price - oneSummary.bonus_price).toFixed(2) oneSummary = Object.assign({}, { differencePrice }, oneSummary) self.setData({ datas: res.datas, getOneSummary: oneSummary, imgUrls: res.datas.common_info.images, getgift }) } else { wx.showToast({ icon: 'none', title: res.message, duration: 1500 }) } }) }, getOneSummary(sumarys, goods_id) { let getOneSummary = sumarys.filter((item, index) => { return item.goods_id == goods_id }) return getOneSummary[0] }, secSku(e) { let goodsId = e.currentTarget.dataset.goodsid let sec_index = e.currentTarget.dataset.secindex if (this.data.sec_index == sec_index) { return } let getOneSummary = this.getOneSummary(this.data.datas.summary, goodsId) let getgift = this.getgift(this.data.datas.summary, goodsId) if (getOneSummary.goods_storage < 1) { wx.showToast({ title: '客官!暂时没有库存!', icon: 'none', duration: 1500 }) return } this.setData({ sec_index, getOneSummary, goodsNumber: 1, getgift }) }, goodsNumHandle(e) { let type = e.currentTarget.dataset.type if (type == 'minus') { if (this.data.goodsNumber <= 1) { wx.showToast({ title: '客官!不能再少了!', icon: 'none', duration: 1500 }) return } else { this.setData({ goodsNumber: --this.data.goodsNumber }) } } else { if (this.data.goodsNumber >= this.data.getOneSummary['goods_storage']) { wx.showToast({ title: '客官!只有这么多了!', icon: 'none', duration: 1500 }) return } else { this.setData({ goodsNumber: ++this.data.goodsNumber }) } } }, checkSubmit() { if (!app.globalData.userInfo) { wx.navigateTo({ url: '/pages/login/login' }); return; } let cartOrBuy = this.data.cartOrBuy this.setData({ animation_flag: false }) if (cartOrBuy == 'isCart') { wx.showLoading({ title: '加载中', }) getReq({ act: 'cart', op: 'addex', quantity: this.data.goodsNumber, goods_id: this.data.getOneSummary.goods_id }, function (res) { wx.hideLoading() if (res.code == 200) { wx.showToast({ icon: 'none', title: '添加成功!', duration: 1500 }) } else { wx.showToast({ icon: 'none', title: res.message, duration: 1500 }) } }) } else if (cartOrBuy == 'isBuy') { if (this.data.getOneSummary.goods_storage < 1) { wx.showToast({ title: '客官!暂时没有库存!', icon: 'none', duration: 1500 }) return } wx.navigateTo({ url: `/pages/confirmOrder/confirmOrder?goods_id=${this.data.getOneSummary.goods_id}&iscart=0&num=${this.data.goodsNumber}` }) } else { return } }, toWebView() { let webViewUrl = encodeURIComponent(this.data.webViewUrl) wx.navigateTo({ url: `/pages/webView/webView?url=${webViewUrl}` }) }, animation_flag(e) { let cartOrBuy = e.currentTarget.dataset.cartorbuy || 'none' let flag = e.currentTarget.dataset.flag let animation_flag = flag == 'true' ? true : false this.setData({ animation_flag, cartOrBuy }) }, getgift(sumarys, goods_id) { let goods = this.getOneSummary(sumarys, goods_id) let giftSummary = '' if (goods.have_gift) { giftSummary = this.getGiftSummary(sumarys, goods.gifts) } return giftSummary }, getGiftSummary(sumarys, gifts) { let arr = [] gifts.map(item => { sumarys.filter(sum => { if (item.gift_goods_id == sum.goods_id) { arr.push({ sum, gifts: item }) } }) }) return arr }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { if (app.globalData.fcodeErr) { wx.showToast({ icon: 'none', title: app.globalData.fcodeErr, duration: 1500 }) app.globalData.fcodeErr = '' this.setData({ animation_flag: false }) setTimeout(() => { this.getDatas(this.data.options_goods_id) }, 1500) } if (app.globalData.backLogin) { this.getDatas(this.data.getOneSummary.goods_id); } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })