// pages/classify/classify.js
var temp_number=null;
var app= getApp();
Page({
/**
* 页面的初始数据
*/
data: {
//动画效果
animationData:{},
//是否显示底部自定义弹框
showModalStatus:false,
//物品的数量(用于显示上面的小数字)
food_number: app.globalData.food_number,
//物品的总价
food_price_sum: app.globalData.food_price_sum,
//购物车
shopping_car: app.globalData.shopping_car,
//商品菜单
menu_list:[
{id:"0",title:"零食",statue:"true"}
, { id: "1", title: "烟酒", statue: "false" }
, { id: "2",title: "化妆品", statue: "false" }
, { id: "3",title: "生活用品", statue: "false" }
, { id: "4",title: "鲜果", statue: "false" }
, { id: "5",title: "蔬菜", statue: "false" }
, { id: "6",title: "其他", statue: "false" }],
//商品
food_list: [{ id: "0", food:[{
index:0,
title:"麻辣条",
price:15,
icon_url:"https://img.alicdn.com/imgextra/i3/725677994/TB2vlyknb5YBuNjSspoXXbeNFXa_!!725677994.jpg_430x430q90.jpg"
}, {
index: 1,
title: "卫龙",
price: 15,
icon_url: "https://img.alicdn.com/imgextra/i3/725677994/TB2vlyknb5YBuNjSspoXXbeNFXa_!!725677994.jpg_430x430q90.jpg"
}, {
index: 2,
title: "麻辣条",
price: 15,
icon_url: "https://img.alicdn.com/imgextra/i3/725677994/TB2vlyknb5YBuNjSspoXXbeNFXa_!!725677994.jpg_430x430q90.jpg"
}]
}, {
id: "1", food: [{
index: 0,
title: "麻辣条",
price: 15,
icon_url: "https://img.alicdn.com/imgextra/i3/725677994/TB2vlyknb5YBuNjSspoXXbeNFXa_!!725677994.jpg_430x430q90.jpg"
}, {
index: 1,
title: "麻辣条",
price: 15,
icon_url: "https://img.alicdn.com/imgextra/i3/725677994/TB2vlyknb5YBuNjSspoXXbeNFXa_!!725677994.jpg_430x430q90.jpg"
}, {
index: 2,
title: "麻辣条",
price: 15,
icon_url: "https://img.alicdn.com/imgextra/i3/725677994/TB2vlyknb5YBuNjSspoXXbeNFXa_!!725677994.jpg_430x430q90.jpg"
}, {
index: 3,
title: "麻辣条",
price: 15,
icon_url: "https://img.alicdn.com/imgextra/i3/725677994/TB2vlyknb5YBuNjSspoXXbeNFXa_!!725677994.jpg_430x430q90.jpg"
}]
}],
page:0
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
changes_menu:function(e){
let goPage = e.currentTarget.id;
var that = this;
var st = that.data.menu_list[goPage].statue
var up = "menu_list[" + goPage + "].statue";//先用一个变量,把(info[0].gMoney)用字符串拼接起来
if (st==‘false‘){
if (temp_number!=null){
var tm = "menu_list[" + temp_number + "].statue"
that.setData({
[tm]: "false",
[up]: "true",
page: goPage
})
}else{
var tt = "menu_list[" + 0 + "].statue"
that.setData({
[tt]: "false",
[up]: "true",
page: goPage
})
}
}
temp_number = goPage;
},
//添加商品到购物车
add_shop_car:function(e){
//单个商品数量
let itemNum = 1;
this.setData({
//所有商品的总数量
food_number: parseInt(this.data.food_number)+1,
//求出商品的总和 直接复制给data
food_price_sum:this.data.food_price_sum + parseInt(e.currentTarget.dataset.price)
})
// 商品名是否相同判断,不重复添加同名商品
if (this.data.shopping_car.length > 0) {
let isHave = this.data.shopping_car.findIndex(item => item.name == e.target.dataset.name);
if (isHave != -1) {
var num_temp = "shopping_car[" + isHave + "].num";
var num_temp_value = parseInt(this.data.shopping_car[isHave].num) + 1;
var price_temp = "shopping_car[" + isHave + "].price";
this.setData({
[num_temp]: num_temp_value,
[price_temp]: parseInt(e.currentTarget.dataset.price),
})
} else {
this.data.shopping_car.push({
//商品名称
name: e.currentTarget.dataset.name,
//商品总价格
price: parseInt(e.currentTarget.dataset.price) * itemNum ,
//商品数量
num: itemNum,
})
this.setData({
shopping_car: this.data.shopping_car,
})
}
} else {
this.data.shopping_car.push({
//商品名称
name: e.currentTarget.dataset.name,
//商品价格
price: parseInt(e.currentTarget.dataset.price) * itemNum,
//商品数量
num: itemNum,
})
this.setData({
shopping_car: this.data.shopping_car,
})
}
this.frunsh_data();
},
shopping_cart:function(){
this.setData({
showModalStatus: true
})
},//显示对话框
showModal: function () {
// 显示遮罩层
var animation = wx.createAnimation({
duration: 2000,
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: function () {
// 隐藏遮罩层
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)
},
shopping_add:function(e){
if (this.data.shopping_car.length > 0) {
let isHave = this.data.shopping_car.findIndex(item => item.name == e.target.dataset.name);
if (isHave != -1) {
var num_temp = "shopping_car[" + isHave + "].num";
var num_temp_value = parseInt(e.target.dataset.num) + 1;
var price_temp = "shopping_car[" + isHave + "].price";
var price_temp_value = parseInt(e.target.dataset.price)
this.setData({
[num_temp]: num_temp_value,
[price_temp]: price_temp_value,
food_number: parseInt(this.data.food_number) + 1,
food_price_sum: this.data.food_price_sum + parseInt(e.target.dataset.price)
})
}
}
this.frunsh_data();
},
shopping_subtract:function(e){
var that = this;
let isHave = this.data.shopping_car.findIndex(item => item.name == e.target.dataset.name);
let num_temp = "shopping_car[" + isHave + "].num";
let num_temp_value = parseInt(e.target.dataset.num) - 1;
let price_temp = "shopping_car[" + isHave + "].price";
let price_temp_value = parseInt(e.target.dataset.price)
if (this.data.shopping_car.length > 0) {
if (isHave != -1) {
this.setData({
[num_temp]: num_temp_value,
[price_temp]: price_temp_value,
food_number: parseInt(this.data.food_number) - 1,
food_price_sum: this.data.food_price_sum - parseInt(e.target.dataset.price)
})
}
}
if (num_temp_value == 0) {
shopping_car: this.data.shopping_car.splice(isHave, 1)
}
if (parseInt(this.data.food_number) <= 0){
this.setData({
showModalStatus:false,
food_price_sum: 0,
})
}
this.frunsh_data();
},
switch_shopping_car: function (e) {
this.frunsh_data();
wx.switchTab({
url: ‘../shoppingCar/shoppingCar‘,
})
console.log(app.globalData.shopping_car)
},
frunsh_data:function(){
// 菜单对象
app.globalData.shopping_car = this.data.shopping_car;
//物品的总价
app.globalData.food_price_sum = this.data.food_price_sum;
//物品的数量
app.globalData.food_number = this.data.food_number;
wx.setStorageSync(‘shopping_car‘, this.data.shopping_car);
wx.setStorageSync(‘food_price_sum‘, this.data.food_price_sum );
wx.setStorageSync(‘food_number‘, this.data.food_number);
}
})