Files
sphc/pages/goodsDetails/goodsDetails.js
2024-05-06 15:34:15 +08:00

321 lines
7.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// pages/goodsDetails/goodsDetails.js
const app = getApp()
const WxParse = require('../../wxParse/wxParse.js');
Page({
data: {
ip: app.globalData.ipUrl,
contents: '', //富文本
standardNum: 1, //商品数量
minusStatus: 'disabled',
goodsList: [],
bnrUrl: [], //轮播图
specsIndex: 0, //选择规格
specs: '', //规格
integral: 0, //积分商城进入
login: true
},
// 页面加载时
onLoad: function (options) {
console.log(options.goodsId)
//商品ID进入
if (options.goodsId) {
this.setData({
goodsId: options.goodsId
})
this.goodDtail()
}
//上级id
if (options.pId) {
wx.setStorage({
data: options.pId,
key: 'pId',
})
}
//判断登录
wx.getStorage({
key: 'user',
success: s => {
this.setData({
login: true
})
},
fail: f => {
this.setData({
login: false
})
}
})
},
onShow: function () {
},
//轮播图片查看
previewImage(e) {
let imgUrls = e.currentTarget.dataset.src;
let preLunboList = this.data.bnrUrl
let preUrls = []
for (let i in preLunboList) {
preUrls.push(preLunboList[i])
}
//图片预览
wx.previewImage({
current: imgUrls, // 当前显示图片的http链接
urls: preUrls // 需要预览的图片http链接列表
})
},
// 商品详情
goodDtail() {
wx.showLoading({
title: '加载中',
mask: true,
})
wx.request({
url: app.globalData.ip + '/wisdommining/api/goods/getGoodsDetail',
method: 'GET',
header: {
"content-type": "application/x-www-form-urlencoded",
},
data: {
goodsId: this.data.goodsId,
userId: app.globalData.userId
},
success: res => {
wx.hideLoading();
console.log(res)
if (res.data.code == 1) {
let goods = res.data.value;
for (let i = 0; i < goods.wisdGoodsSpecList.length; i++) {
if (goods.wisdGoodsSpecList[i].specBulyPrice == null) {
goods.wisdGoodsSpecList[i].price = goods.wisdGoodsSpecList[i].specPrice
} else {
goods.wisdGoodsSpecList[i].price = goods.wisdGoodsSpecList[i].specBulyPrice
}
}
WxParse.wxParse('contents', 'html', goods.goodsInfo, this, 20);
this.setData({
goodsList: goods,
bnrUrl: goods.goodsBanner.split(',')
})
//积分
if (goods.goodsClassify == 2) {
this.setData({
integral: 1
})
}
} else {
wx.showModal({
title: '提示!',
content: res.data.message,
showCancel: false,
})
}
}
})
},
// 轮播
onSlideChange(e) {
this.setData({
lunboindex: e.detail.current + 1
})
},
//点击显示底部弹窗
choose: function (e) {
var choose = e.currentTarget.dataset.choose
this.setData({
choose: choose,
})
this.showModal();
},
//选择规格
showModal: function () {
// 显示遮罩层
var animation = wx.createAnimation({
duration: 200,
timingFunction: "linear",
delay: 0
})
this.animation = animation
animation.translateY(300).step()
this.setData({
animationData: animation.export(),
showModalStatus: true
})
setTimeout(function () {
animation.translateY(0).step()
this.setData({
animationData: animation.export()
})
}.bind(this), 200)
},
//隐藏对话框
hideModal(e) {
console.log(this.data.goodsList.wisdGoodsSpecList[this.data.specsIndex].specStock)
if (this.data.goodsList.wisdGoodsSpecList[this.data.specsIndex].specStock == 0) {
wx.showToast({
title: '库存不足!',
icon: 'none'
})
return
}
let hide = e.currentTarget.dataset.hide;
let goodsList = this.data.goodsList
let goods = [{
isDiscount: goodsList.isDiscount, //判断是否为会员折扣
goodsPhoto: goodsList.goodsPhoto,
goodsName: goodsList.goodsName,
goodsId: goodsList.id,
num: this.data.standardNum,
specs: goodsList.wisdGoodsSpecList[this.data.specsIndex]
}]
if (hide == 1) {
// 加入购物车
this.carAdd();
} else if (hide == 2) {
//普通商品
wx.navigateTo({
url: '/pages/cartPay/cartPay?goods=' + JSON.stringify(goods),
})
} else if (hide == 3) {
// 积分商品
wx.navigateTo({
url: '/pages/cartPay/cartPay?integral=1&goods=' + JSON.stringify(goods),
})
}
// 隐藏遮罩层
var animation = wx.createAnimation({
duration: 200,
timingFunction: "linear",
delay: 0
})
this.animation = animation
animation.translateY(300).step()
this.setData({
animationData: animation.export(),
})
setTimeout(function () {
animation.translateY(0).step()
this.setData({
animationData: animation.export(),
showModalStatus: false
})
}.bind(this), 200)
},
// 加入购物车
carAdd(e) {
wx.request({
url: app.globalData.ip + '/wisdommining/api/goods/addCart',
method: 'POST',
header: {
"content-type": "application/x-www-form-urlencoded",
"token": app.globalData.token
},
data: {
userId: app.globalData.userId,
goodsId: this.data.goodsId,
goodsNum: this.data.standardNum,
specId: this.data.goodsList.wisdGoodsSpecList[this.data.specsIndex].id,
},
success: res => {
wx.hideLoading();
console.log(res)
if (res.data.code == 1) {
wx.showToast({
title: '添加成功,快去购物车中查看~',
icon: 'none',
success: s => {
setTimeout(() => {
this.goodDtail()
}, 1000);
}
})
} else {
wx.showModal({
title: '提示!',
content: res.data.message,
showCancel: false,
})
}
}
})
},
// 输入数量
standardNum(e) {
console.log('form发生了submit事件携带数据为', e.detail.value)
var standardNum = e.detail.value;
this.setData({
standardNum: standardNum,
});
},
//点击数量减少
clickReduce: function () {
var num = this.data.standardNum;
if (num > 1) {
num--;
}
var minusStatus = num <= 1 ? 'disabled' : 'normal';
this.setData({
standardNum: num,
minusStatus: minusStatus
});
},
clickAdd: function () {
var num = this.data.standardNum;
num++;
var minusStatus = num < 1 ? 'disabled' : 'normal';
this.setData({
standardNum: num,
minusStatus: minusStatus
})
},
//选择规格
specsClick: function (e) {
let index = e.target.dataset.index;
this.setData({
specsIndex: index
})
},
// 跳转首页
toIndex() {
wx.reLaunch({
url: '/pages/index/index',
})
},
// 跳转购物车
toCar() {
wx.reLaunch({
url: '/pages/cart/cart',
})
},
//去登录
login() {
wx.navigateTo({
url: '/pages/login/login?goodsId=' + this.data.goodsId,
})
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
app.share();
return {
path: '/pages/goodsDetails/goodsDetails?goodsId=' + this.data.goodsId + '&pId=' + app.globalData.userId,
title: '三品慧采小程序等你来~'
}
}
})