import {showToast} from '../../utils/asyncWx'
Page({
data: {
address:{},
cart:[],
allSelected:false,
totalPrice:0,
totalNum:0
},
onShow() {
//获取缓存中的收货地址数据
let address = wx.getStorageSync('address');
//获取缓存中的购物车数据
let cart = wx.getStorageSync('cart') || [];
//单独设置address
this.setData({
address
});
this.setCart(cart);
},
handleChooseAddress() {
//调用微信内置的api获取收货地址
wx.chooseAddress({
success: (result) => {
wx.setStorageSync('address', result)
},
})
},
//改变select的选项
handleChangeSelect(e) {
//通过点击的商品,把该商品的data和缓存中的值改变
//1.取出商品的id值
let {id} = e.currentTarget.dataset;
//找到数组中的该商品,然后修改
let {cart} = this.data;
let currentGood = cart.find((item) => id === item.goods_id);
//将商品的选择状态修改
currentGood.isSelected = !currentGood.isSelected;
//写回data数据中
// this.setData({
// cart
// });
console.log(cart);
//设置缓存
wx.setStorageSync('cart', cart);
this.setCart(cart);
},
//
handleAllSelect() {
// console.log('hello world');
//先拿到全选的值
let {cart,allSelected} = this.data;
allSelected = !allSelected;
//改变商品数组中所有的选中状态
cart.forEach(item => item.isSelected = allSelected);
this.setData({
cart
});
this.setCart(cart);
},
//加减数量
handleItemNumbleEdit(e) {
//取出goods_id 和 type
const {id:goods_id,type} = e.currentTarget.dataset;
let {cart} = this.data;
//找到需要修改的商品
var currentGood = cart.find(item => item.goods_id === goods_id);
let goodIndex = cart.findIndex(item => item.goods_id === goods_id);
if(currentGood.num === 1 && type === -1) {
// wx.showToast({
// title: '不能在减了哦',
// duration: 2000
// })
wx.showModal({
title: '删除',
content: '确定要删除该商品吗',
success: res => {
if (res.confirm) {
cart.splice(goodIndex,1);
//写回data
this.setCart(cart);
//写回缓存
wx.setStorageSync('cart', cart);
} else if (res.cancel) {
currentGood.num = 1;
console.log('用户点击取消')
}
}
})
}else {
currentGood.num += type;
//写回data
this.setCart(cart);
//写回缓存
wx.setStorageSync('cart', cart);
}
},
//结算功能实现
async handlePay() {
const {address,totalNum} = this.data;
//1.判断地址存不存在
if(!address.userName) {
await showToast('请填写你的收货地址')
return;
}
//判断是否选购了商品
// if(totalNum == 0) {
// await showToast('请先去选购商品');
// return;
// }
//如果走到这里说明选了商品和地址
wx.navigateTo({
url: '/pages/pay/index',
})
},
//设置购物车状态 包括钱,数量,
setCart(cart) {
//计算是不是全选
let allSelected = cart.length ==0 ? false : true;
//计算总价格
let totalPrice = 0;
let totalNum = 0;
cart.forEach(item => {
if(item.isSelected) {
totalPrice += item.goods_price * item.num;
totalNum += item.num;
}else {
allSelected = false;
}
});
this.setData({
cart,
allSelected,
totalPrice,
totalNum
})
}
})
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
jingpinyougjou.zip (80个子文件)
jingpinyougjou
heima-yougou
icons
_myCart.png 2KB
_me.png 3KB
home.png 2KB
myCart.png 2KB
_home.png 2KB
_classify.png 1KB
me.png 3KB
classify.png 2KB
app.js 342B
components
SearchInput
SearchInput.js 231B
SearchInput.wxml 123B
SearchInput.wxss 267B
SearchInput.json 48B
Tabs
Tabs.wxss 246B
Tabs.js 384B
Tabs.wxml 368B
Tabs.json 48B
project.private.config.json 2KB
project.config.json 2KB
utils
util.js 460B
asyncWx.js 851B
sitemap.json 191B
styles
iconfont.wxss 5KB
normal.wxss 206B
pages
goods_list
index.wxml 898B
index.js 2KB
index.wxss 536B
index.json 242B
pay
index.wxml 2KB
index.js 2KB
index.wxss 2KB
index.json 65B
goods_detail
index.wxml 2KB
index.js 2KB
index.wxss 1KB
index.json 71B
collect
index.wxml 70B
index.js 845B
index.wxss 30B
index.json 27B
login
index.wxml 105B
index.js 180B
index.wxss 45B
index.json 65B
search
index.wxml 68B
index.js 844B
index.wxss 29B
index.json 27B
user
index.wxml 2KB
index.js 161B
index.wxss 2KB
index.json 65B
auth
index.wxml 105B
index.js 714B
index.wxss 27B
index.json 65B
index
index.wxml 1KB
index.js 1008B
index.wxss 586B
index.json 136B
order
index.wxml 697B
index.js 1KB
index.wxss 187B
index.json 115B
feedback
index.wxml 72B
index.js 846B
index.wxss 31B
index.json 27B
cart
index.wxml 3KB
index.js 4KB
index.wxss 2KB
index.json 68B
category
index.wxml 1KB
index.js 2KB
index.wxss 833B
index.css 104B
index.json 136B
app.json 1KB
app.wxss 92B
request
index.js 629B
共 80 条
- 1
编程ID
- 粉丝: 7w+
- 资源: 471
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页