const {
envList
} = require('../../envList.js');
const db = wx.cloud.database(); //使用API获取数据库的连接,该句执行完成后,db就代码远程的数据库
const app = getApp(); //获取小程序本身
let itemHeight = 310; //单菜品显示高度
Page({
data: {
deskId: 1,
types: [],
goods: [], //第一次从云上合出来的原始数据,这个数据不变
tempGoods: [], //页面中真正显示的菜品数据
cart: [], //购物车
goodsCount: 0,
currentId: 0,
currentType: '全部',
height: 0,
tempHeight: 0
},
async scanOperaion() {
let _self = this;
let res = await wx.scanCode({
onlyFromCamera: true
})
let deskId = res.result.split("=")[1];
_self.setData({
deskId: deskId
});
console.log(deskId);
},
onShow(){
},
onReady() {
this.popupBox = this.selectComponent("#popupBox");
},
onLoad() { //为了效率,本地只在装载程序的时候,执行一次获取数据的操作。
db.collection('types').get().then(resp => {
this.setData({
types: resp.data
});
}).catch(error => {
console.error(error.errMsg);
});
db.collection('goods').orderBy('typeId', 'asc').get()
.then(resp => {
let size = resp.data.length;
this.setData({
goods: resp.data,
tempGoods: resp.data,
height: size * itemHeight + 55,
tempHeight: size * itemHeight + 55
});
})
.catch(error => console.log(error.errMsg));
},
switchType(event) {
let currentId = event.currentTarget.dataset.typeId;
let arrays = [];
let tempHeight = 0;
if (currentId == 0) {
arrays = this.data.goods;
this.setData({
currentType: '全部',
tempHeight: this.data.height
});
} else {
arrays = this.data.goods.filter(item => item.typeId == currentId);
let currentItem = this.data.types.filter(item => currentId == item.id)[0];
this.setData({
currentType: currentItem.name
});
}
this.setData({
tempGoods: arrays,
currentId: currentId,
tempHeight: arrays.length * itemHeight + 55
});
},
searchOperation(event) {
console.log(`搜索关键字:${event.detail.value}`);
},
selectGoods(event) {
let id = event.currentTarget.dataset.id;
let parameter = {
"id": id
};
let currentItem = this.data.tempGoods.filter(item => item.id == id)[0];
parameter.imageUrl = currentItem.imageUrl;
parameter.name = currentItem.name;
parameter.real = currentItem.real;
parameter.id = currentItem.id;
parameter.typeId = currentItem.typeId;
if(currentItem.number==0){
parameter.number = 1;
}else{
parameter.number = currentItem.number;
}
this.popupBox.switch(parameter);
},
navigateOperaion(){
let date = new Date(); //生成一个时间信息
let orderTime=`${date.getMonth()+1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}`;
app.globalData.orderTime = orderTime;
app.globalData.deskId = this.data.deskId;
app.globalData.cart = this.data.cart;
wx.navigateTo({
url: `../order/order`
})
},
setShoppingCart(event) {
let _self = this;
let tempArrays = this.data.cart;
let tempGoodsArrays = this.data.tempGoods //创建一个临时数组
let currentGoods = event.detail;
//设置购物车中的数据
if (this.data.cart.find(element => element.id == currentGoods.id)) {
let current = tempArrays.filter(element => element.id == currentGoods.id)[0];
//number不可能的小于0,这里只能是大于0或者等于0。
if (currentGoods.number > 0) { //此时说明菜品还在购物车里,只是数量发生了变化
current.number = currentGoods.number;
} else {//这意味着购物车中已经没有这个菜品了,所以要进行对应删除操作
tempArrays.splice(tempArrays.findIndex(element => element.id == currentGoods.id), 1);
tempGoodsArrays.find(element => element.id == currentGoods.id).number=0; //删角标
}
} else if(currentGoods.number!=0){ //如果添加的菜品的number==0,则说明添加之后有删除了,所以不应该加入
tempArrays.push(event.detail);
}
this.setData({
cart: tempArrays,tempGoods:tempGoodsArrays
});
//往tempGoods中回写选择菜品的数量
tempArrays.forEach(goods => {
tempGoodsArrays.filter(item => item.id == goods.id).forEach(item => {
item.number = goods.number;
});
});
this.setData({
tempGoods: tempGoodsArrays
});
//计算菜品总数,使用reduce()函数实现一个累加操作
let count = this.data.cart.reduce((pre, current) => pre + current.number, 0);
this.setData({
goodsCount: count
});
//为types数组添加已选菜品数量
this.data.types.forEach(element => element.number = 0); //清空types中的数据。
let tempTypes = this.data.types;
this.data.cart.forEach(item => {
tempTypes.filter(element => item.typeId == element.id).forEach(element => {
element.number += item.number;
});
});
this.setData({
types: tempTypes
});
}
});
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于微信小程序的在线点餐小程序源码+数据库(高分毕业设计).zip 已获导师指导并通过的微信小程序高分毕业设计,微信小程序课程设计,基于微信小程序开发的,含有代码和数据库,下载即用,新手也可看懂,可作为毕业设计,课程设计和期末大作业。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 基于微信小程序的在线点餐小程序源码+数据库(高分毕业设计).zip 已获导师指导并通过的微信小程序高分毕业设计,微信小程序课程设计,基于微信小程序开发的,含有代码和数据库,下载即用,新手也可看懂,可作为毕业设计,课程设计和期末大作业。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。基于微信小程序的在线点餐小程序源码+数据库(高分毕业设计).zip 已获导师指导并通过的微信小程序高分毕业设计,微信小程序课程设计,基于微信小程序开发的,含有代码和数据库,下载即用,新手也可看懂,可作为毕业设计,课程设计和期末大作业。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。基于微信小程序的在线点餐小程序源码+数
资源推荐
资源详情
资源评论












收起资源包目录

























































共 45 条
- 1
资源评论

- APTX09222023-09-04非常有用的资源,有一定的参考价值,受益匪浅,值得下载。
- m0_744170332023-08-25内容与描述一致,超赞的资源,值得借鉴的内容很多,支持!
- Harry8072023-07-21资源值得借鉴的内容很多,那就浅学一下吧,值得下载!
- aliuyunb2023-07-06非常有用的资源,有一定的参考价值,受益匪浅,值得下载。

不安分的小女孩
- 粉丝: 3561
- 资源: 924
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
