// pages/card/index.js
Page({
/**
* 页面的初始数据
*/
data: {
cartArray:[],
totalPrice:"0.00",
totalCount:0,
selectAll:false,
startX:0,
startY:0
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
addCount(e){
const index = e.currentTarget.dataset.index;
const arr = this.data.cartArray;
arr[index].total += 1;
// console.log(arr[index].select);
if(arr[index].select){
let totalMonay = Number(this.data.totalPrice);
totalMonay += Number(arr[index].price);
this.setData({
totalPrice:String(totalMonay.toFixed(2))
})
}
this.setData({
cartArray:arr
})
wx.setStorage({
data: arr,
key: 'cartInfo',
})
},
subtractCount(e){
const index = e.currentTarget.dataset.index;
const arr = this.data.cartArray;
if(arr[index].total>1){
arr[index].total -= 1;
this.setData({
cartArray:arr
})
if(arr[index].select){
let totalMonay = Number(this.data.totalPrice);
totalMonay -= Number(arr[index].price);
this.setData({
totalPrice:String(totalMonay.toFixed(2))
})
}
wx.setStorage({
data: arr,
key: 'cartInfo',
})
}
},
switchShop(e){
console.log(e)
const index = e.currentTarget.dataset.index;
const arr = this.data.cartArray;
//跳转
wx.navigateTo({
url: '/pages/shopDetail/index?id='+arr[index].id,
})
},
selectShop(e){
// console.log(e)
const index = e.currentTarget.dataset.index;
const arr = this.data.cartArray;
arr[index].select = !arr[index].select;
if(!arr[index].select){
this.setData({
selectAll:false
})
}
//选中商品的总价格
let total = this.data.cartArray[index].total * Number(this.data.cartArray[index].price);
//当前总价
let totalMonay = Number(this.data.totalPrice);
let totalCount = this.data.totalCount;
// console.log(total);
if(arr[index].select){
totalMonay += total;
totalCount++
}
else{
totalMonay -= total;
totalCount--;
}
this.setData({
totalPrice:String(totalMonay.toFixed(2)),
totalCount:totalCount,
cartArray:arr
})
},
selectAll(){
let all = this.data.selectAll;
let arr = this.data.cartArray;
arr.forEach((el) => {
el.select=!all
})
if(!all){
let total = () => {
let totalMoney = 0;
arr.forEach(el=>{
totalMoney += el.total*Number(el.price)
})
return totalMoney;
}
// console.log(total())
this.setData({
totalPrice:total(),
totalCount:arr.length
})
// console.log(1)
}
else{
console.log(2)
this.setData({
totalPrice:"0.00",
totalCount:0
})
}
this.setData({
cartArray:arr,
selectAll:!all
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
let _this = this;
wx.getStorage({
key: 'cartInfo',
success: (result) => {
const arr = result.data;
arr.forEach((el) => {
el.select = false;
el.isTouchMove = false;
})
_this.setData({
cartArray:arr,
totalPrice:"0.00",
selectAll:false,
totalCount:0
})
}
})
//判断当前storage中有没有商品
wx.getStorage({
key: 'cartInfo',
success(res){
const arr = res.data;
console.log(arr)
arr.length > 0 ?
//显示右上角文本
wx.setTabBarBadge({
index: 2,
text: String(arr.length)
}):
wx.removeTabBarBadge({
index: 2,
})
}
})
},
//触摸的初始位置
touchstart(e){
const cartArray = this.data.cartArray;
//同时重置所有删除按钮
// cartArray.forEach((el)=>{
// el.isTouchMove = false;
// });
//记录下初始点的位置
this.setData({
cartArray:cartArray,
startX:e.changedTouches[0].clientX,
startY:e.changedTouches[0].clientY
})
},
//移动位置
trouchmove(e){
// console.log(e);
//开始坐标
let startX = this.data.startX,
startY = this.data.startY;
//移动坐标
let moveX = e.changedTouches[0].clientX,
moveY = e.changedTouches[0].clientY;
// console.log(startX,startY);
// console.log(moveX,moveY);
//获得角度
let angel = this.angel({x: startX, y: startY},{x: moveX,y: moveY});
let cartArray = this.data.cartArray;
let index = e.currentTarget.dataset.index;
//如果滑动角度大于30度,那么认为不是正常的需要显示删除的角度
if(angel>30) return;
//移动的x轴大于初始的,那么代表右滑
if(moveX>startX){
cartArray[index].isTouchMove = false;
}
else{
cartArray[index].isTouchMove = true;
}
this.setData({
cartArray:cartArray
})
},
//计算移动位置和起始位置的角度
angel(start, end){
let _x = end.x - start.x;
let _y = end.y - start.y;
return 360 * Math.atan(_y/_x) / (2 * Math.PI);
},
deleteShop(e){
const id = e.currentTarget.dataset.id;
const index = e.currentTarget.dataset.index;
const _this = this;
const cartArray = this.data.cartArray;
console.log(e)
//返回的数组是没有
let newArr = cartArray.filter((el)=>{
return el.id !== id
})
// console.log(newArr);
wx.setStorage({
data: newArr,
key: 'cartInfo',
})
//更新商品列表
_this.setData({
cartArray:newArr
})
//更新总价和数量
_this.updata(newArr);
//更新tab右上角数字
newArr.length > 0 ?
//显示右上角文本
wx.setTabBarBadge({
index: 2,
text: String(newArr.length)
}):
wx.removeTabBarBadge({
index: 2,
})
},
//更新数据,假如当前的总价处于选择状态,那么删除商品的同时总价和数量应该有所变化
updata(cartArray){
// let cartArray = this.data.cartArray;
let total = 0,
count = 0
cartArray.forEach((el)=>{
if(el.select){
total += Number(el.price)*el.total;
count++;
}
})
this.setData({
totalPrice:String(total.toFixed(2)),
totalCount:count
})
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
最新微信小程序商城源码 (116个子文件)
.DS_Store 8KB
.DS_Store 6KB
jd1.jpg 97KB
jd3.jpg 61KB
jd4.jpg 52KB
jd2.jpg 43KB
jd5.jpg 38KB
ad-big.jpg 20KB
ad-0.jpg 20KB
ad-1.jpg 17KB
ad-right.jpg 14KB
ad-left.jpg 14KB
quick2.jpg 12KB
quick3.jpg 7KB
quick6.jpg 7KB
quick5.jpg 6KB
quick4.jpg 5KB
quick1.jpg 4KB
index.js 7KB
index.js 5KB
index.js 3KB
index.js 2KB
index.js 2KB
index.js 2KB
index.js 1KB
index.js 1KB
app.js 1KB
index.js 701B
urlconfig.js 679B
util.js 472B
index.js 459B
index.js 362B
project.config.json 1KB
app.json 1KB
sitemap.json 191B
index.json 145B
index.json 111B
index.json 91B
index.json 91B
index.json 65B
index.json 65B
index.json 65B
index.json 48B
index.json 48B
index.json 48B
index.json 27B
shop_cart.png 57KB
crab.png 40KB
miphone.png 24KB
microwave.png 15KB
cooker.png 14KB
usb.png 14KB
router.png 14KB
shaver.png 13KB
game.png 13KB
phone.png 12KB
briefs.png 11KB
milk.png 11KB
headset.png 10KB
logo5.png 9KB
logo2.png 8KB
logo4.png 8KB
huawei.png 7KB
logo3.png 7KB
logo6.png 6KB
logo8.png 6KB
logo1.png 6KB
logo10.png 6KB
logo7.png 6KB
laundry.png 6KB
logo9.png 6KB
cart-hl.png 5KB
home-hl.png 5KB
explore.png 5KB
explore-hl.png 5KB
me.png 5KB
home.png 5KB
me-hl.png 5KB
category-hl.png 4KB
cart.png 4KB
category.png 4KB
power.png 3KB
unselect.png 2KB
selected.png 2KB
bej.png 2KB
arrowright.png 1KB
aky.9.png 540B
ati.png 503B
ath.png 483B
ahw.png 473B
ahv.png 472B
ai4.png 370B
ai3.png 350B
index.wxml 3KB
index.wxml 2KB
index.wxml 2KB
index.wxml 1KB
index.wxml 1015B
index.wxml 875B
index.wxml 834B
共 116 条
- 1
- 2
weizunde
- 粉丝: 8
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于STM32F103C8T6单片机蓄电池在线监测系统主板硬件(原理图+PCB)工程文件.zip
- mysql大纲资料.txt
- c++大纲资料.txt
- 效率工具bat脚本实现日志提取
- MyBatis 中动态 SQL 的示例
- STM8L101F3P6单片机+CC1100模块433M遥控器设计硬件(原理图+PCB)工程文件.zip
- 上传下载铁人下载系统 Liuxing 1.0-liuxing1.0.rar
- 南京邮电大学数学实验实力雄厚,凭借其优秀的师资力量、丰富的实践教学资源和卓越的科研成果,成为国内一流的数学实验教学和科研基地
- 【火爆朋友圈的今天吃什么源码 v1.0】随机的为用户带来每一天的用餐选择和推荐.rar
- MPU6050中文版数据手册
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0