import { request } from '../../request/index'
/**
* 点击轮播图预览大图功能
* * 给轮播图绑定点击事件
* * 调用了小程序的API previewImage
*/
/**
* 点击加入购物车
* * 绑定点击事件
* * 获取缓存中的购物车数据 数组格式存储
* * 判断 当前商品是否存在于购物车
* * 已经存在修改商品数据 执行购物车数量++ 重新把购物车数组填充回缓存中
* * 不存在于购物车的数组中 直接给购物车数组添加一个新元素,带上购买数量属性
* * 弹出提示
*/
/**
* 商品收藏
* 1.页面onShow的时候,加载缓存中的商品收藏的数据
* 2.判断当前商品是否收藏
* * 是:改变页面图标
* * 不是:.....
* 3.点击商品收藏按钮
* * 判断该商品是否存在于缓存数组中
* * 存在的话,把该商品删除
* * 不存在,把商品添加到收藏数组中,存入到缓存中
*/
// pages/goods_detail/index.js
Page({
/**
* 页面的初始数据
*/
data: {
goodsDeatilData: {},
isCollect: false
},
goodsInfo: {},
async getGoodsDetailData(id) {
const res = await request({
url: "/goods/detail",
data: id
})
this.goodsInfo = {
goods_name: res.goods_name,
goods_price: res.goods_price,
pics: res.pics,
goods_id: id.goods_id,
num: 0,
isChecked: true
}
this.setData({
goodsDeatilData: {
goods_name: res.goods_name,
goods_price: res.goods_price,
// iphone部分手机不识别webp图片格式
goods_introduce: res.goods_introduce.replace(/\.webp/g, ".jpg"),
pics: res.pics
}
})
},
onShow() {
let pages = getCurrentPages();
let currentPage = pages[pages.length - 1]
console.log(currentPage)
let options = currentPage.options
this.getGoodsDetailData(options)
// 1.获取缓存中的购物车数据
let collect = wx.getStorageSync("collect") || [];
// 2.判断该商品是否存在于缓存数组中
// some() 方法用于检测数组中的元素是否满足指定条件(函数提供)
let isCollect = collect.some(item => {
return item.goods_id === options.goods_id
})
this.setData({
goodsInfo: options.goods_id,
isCollect
})
},
// 点击轮播图放大预览
handlePreviewImg(e) {
const { index } = e.currentTarget.dataset
// 1.先构造要预览的图片数组
const urls = this.data.goodsDeatilData.pics.map(item => {
return item.pics_mid
})
wx.previewImage({
current: urls[index], // 当前显示图片的http链接
urls // 需要预览的图片http链接列表
})
},
// 点击加入购物车
handleCartAdd() {
let cart = wx.getStorageSync("cart") || [];
let index = cart.findIndex(item => {
return item.goods_id === this.goodsInfo.goods_id
})
if (index === -1) {
// 不存在
this.goodsInfo.num = 1
cart.push(this.goodsInfo)
wx.showToast({
title: '添加购物车成功',
icon: 'success',
mask: true
})
} else {
// 存在
cart[index].num++;
wx.showToast({
title: '商品数量+1',
icon: 'success',
mask: true
})
}
wx.setStorageSync("cart", cart);
},
// 收藏事件
handleCollect() {
// 1.获取缓存中的购物车数据
let collect = wx.getStorageSync("collect") || [];
// 2.判断该商品是否存在于缓存数组中
let index = collect.findIndex(item => {
return item.goods_id === this.goodsInfo.goods_id
})
if (index === -1) {
// 表示商品不存在于缓存数组中
collect.push(this.goodsInfo)
wx.showToast({
title: '收藏成功',
mask: true
});
} else{
// 表示商品存在于缓存数组中,删除该商品
//splice修改原数组
collect.splice(index,1)
wx.showToast({
title: '取消收藏',
mask: true
});
}
//把数组存入到缓存中
wx.setStorageSync("collect", collect);
this.setData({
isCollect:!this.data.isCollect
})
}
})
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
一个原生微信小程序项目-优购商城源码分享 项目简介: 该商城是基于原生微信小程序来实现的。 已完成功能: 登录授权 商品添加进购物车、购物车删除商品 展示个人中心、首页、商品详情 搜索商品 收藏商品 订单查询 一个原生微信小程序项目-优购商城源码分享 项目简介: 该商城是基于原生微信小程序来实现的。 已完成功能: 登录授权 商品添加进购物车、购物车删除商品 展示个人中心、首页、商品详情 搜索商品 收藏商品 订单查询 一个原生微信小程序项目-优购商城源码分享 项目简介: 该商城是基于原生微信小程序来实现的。 已完成功能: 登录授权 商品添加进购物车、购物车删除商品 展示个人中心、首页、商品详情 搜索商品 收藏商品 订单查询 一个原生微信小程序项目-优购商城源码分享 项目简介: 该商城是基于原生微信小程序来实现的。 已完成功能: 登录授权 商品添加进购物车、购物车删除商品 展示个人中心、首页、商品详情 搜索商品 收藏商品 订单查询 一个原生微信小程序项目-优购商城源码分享 项目简介: 该商城是基于原生微信小程序来实现的。 已完成功能: 登录授权 商品添加进购物车
资源推荐
资源详情
资源评论
收起资源包目录
ZZ-Shop-master.zip (85个子文件)
ZZ-Shop-master
sitemap.json 191B
pages
collect
index.less 937B
index.wxml 954B
index.js 802B
index.json 116B
index.wxss 988B
cart
cart.wxml 2KB
index.less 27B
index.wxml 224B
cart.less 4KB
cart.wxss 4KB
cart.js 4KB
cart.json 68B
index.js 1KB
index.json 68B
category
index.less 1004B
index.wxml 1KB
index.js 3KB
index.json 130B
index.wxss 1KB
index
index.less 868B
index.wxml 1KB
index.js 2KB
index.json 130B
index.wxss 916B
order
index.less 2KB
index.wxml 1KB
index.js 2KB
index.json 116B
index.wxss 2KB
user
index.less 4KB
index.wxml 4KB
index.js 634B
index.json 103B
index.wxss 4KB
pay
index.less 2KB
index.wxml 1KB
index.js 2KB
index.json 70B
index.wxss 2KB
search
index.less 1KB
index.wxml 873B
index.js 1KB
index.json 70B
index.wxss 1KB
goods_detail
index.less 1KB
index.wxml 2KB
index.js 4KB
index.json 71B
index.wxss 1KB
goods_list
index.less 665B
index.wxml 890B
index.js 2KB
index.json 237B
index.wxss 684B
app.json 1KB
project.config.json 2KB
assets
img
cart1.gif 5KB
cart.gif 75KB
search.jpg 7KB
avatar.jpg 15KB
wxss
iconfont.wxss 5KB
iconfont1.wxss 11KB
app.js 321B
utils
asyncWX.js 806B
app.wxss 488B
components
SearchInput
SearchInput.json 48B
SearchInput.js 231B
SearchInput.wxml 148B
SearchInput.less 316B
SearchInput.wxss 314B
TabBar
TabBar.wxss 331B
TabBar.js 461B
TabBar.json 48B
TabBar.wxml 370B
TabBar.less 331B
request
index.js 671B
icons
category.png 2KB
category-o.png 3KB
home.png 2KB
my.png 3KB
home-o.png 3KB
cart.png 2KB
cart-o.png 3KB
my-o.png 3KB
共 85 条
- 1
资源评论
- sdfghjklsss2024-02-07发现一个宝藏资源,资源有很高的参考价值,赶紧学起来~
白话机器学习
- 粉丝: 8175
- 资源: 7687
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功