// pages/details/details.js const getReq = require('./../../config.js').getReq; const app = getApp(); var WxParse = require('../../wxParse/wxParse.js'); 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: '', countTime: 50, /// 测试倒计时 basicStar: './../image/basic_star.png', activeStar: '../../image/star.png', halfStar: '../../image/half_star.png', starSrcs: [], comments: [] }, /** * 生命周期函数--监听页面加载 */ 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 // }) setInterval(() => { this.setData({ countTime: --this.data.countTime }) }, 1000); // console.log(options.goods_id); this.getDatas(options.goods_id) // this.getDatas(7636) // this.getDatas(4569) // a.lrlz.com 有多条评论 var that = this; getReq({ act: 'goods_common', op: 'detail', goods_id: options.goods_id // goods_id: 7636 // goods_id: 4569 }, function(res) { var str = res.split(/<[\/]?body>/gi)[1]; WxParse.wxParse('article', 'html', str, that); }); }, 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) { console.log(res); self.createStars(res.datas.common_info.comments_rate) self.createComments(res.datas.comments) 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 { console.log('error:', res.message); 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 }, createStars(rate) { let starSrcs = [] for(let i = 0; i < 5; i++) { if(i < Math.floor(rate)) { starSrcs.push(this.data.activeStar) continue } if(i < Math.round(rate)) { starSrcs.push(this.data.halfStar) continue } starSrcs.push(this.data.basicStar) } this.setData({ starSrcs }) }, createComments(basicComments) { let comments = [] comments = basicComments.map(item => { let addtime = item.addtime || '' if (addtime) { let date = new Date(addtime * 1000); let year = date.getFullYear(); let month = date.getMonth() + 1; let day = date.getDate(); let transform_addtime = `${year}-${month}-${day}` item['transform_addtime'] = transform_addtime } return item }) console.log(comments); this.setData({ comments }) }, allComments() { wx.navigateTo({ url: `/pages/comments/comments?common_id=${this.data.datas.comment.common_id}` }); return; }, /** * 生命周期函数--监听页面初次渲染完成 */ 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 () { let goods_id = this.data.options_goods_id let goods_name = this.data.getOneSummary.goods_mobile_name let imageUrl = this.data.imgUrls[0]; return { title: `熊猫美妆为您推荐:${goods_name}`, path: `/pages/details/details?goods_id=${goods_id}`, imageUrl: imageUrl } } })