123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- // subPackagesE/pages/giftBagSize/giftBagSize.js
- import {
- get,
- post
- } from '../../../utils/http'
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- id: '', //礼盒ID
- giftBagList: [], //礼包分配列表
- selectGiftList: [], //选中的列表
- giftBagListOne: [], //不用分配列表
- selectSum: '', //选中的次数
- num: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'],
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- this.setData({
- id: options.id
- })
- // 获取礼包列表
- this.getGiftBagList()
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- },
- // 获取礼包列表
- getGiftBagList() {
- get('v2/api/package/info', {
- id: this.data.id
- }, (res => {
- let giftBagList = [] //分配
- let giftBagListOne = [] //不分配
- res.data.items.forEach(item => {
- if (item.type == 2) {
- // 添加选择次数
- item.selectNum = 0
- item.goods_list.forEach(item1 => {
- // 添加选择状态
- item1.status = false
- // 添加每个产品的数量
- item1.num = 0
- })
- giftBagList.push(item)
- } else {
- giftBagListOne.push(item)
- }
- })
- this.setData({
- giftBagList,
- giftBagListOne,
- giftBagInfo: res.data
- })
- // 将不用分配的添加到数组里
- let selectGiftList = this.data.selectGiftList
- this.data.giftBagListOne.forEach(item => {
- item.goods_list.forEach(itemA => {
- let list = {}
- list.item_id = item.id
- list.goods_id = itemA.id
- list.num = 1
- list.goods_name = itemA.goods_name
- list.cover_url = itemA.cover_url
- selectGiftList.push(list)
- this.setData({
- selectGiftList
- })
- })
- })
- }))
- },
- // 选择规格
- selectGift(e) {
- let itemA = e.currentTarget.dataset.itema //单个产品的信息
- let item = e.currentTarget.dataset.item //整个商品分类的信息
- // 判断是否是多选
- if(item.num>1){
- return
- }
- let indexA = e.currentTarget.dataset.indexa //产品index
- let index = e.currentTarget.dataset.index //分类index
- let selectGiftList = this.data.selectGiftList //选中的商品ID列表
- let itemSum = item.num //商品分类可选的最大次数
- // 超过最大次数返回
- if (item.selectNum == itemSum && !itemA.status) {
- wx.showToast({
- title: `你已选择${itemSum}次`,
- icon: 'none'
- })
- return
- }
- // 选中改变商品状态
- let giftBagStatus = `giftBagList[${index}].goods_list[${indexA}].status`
- this.setData({
- [giftBagStatus]: !this.data.giftBagList[index].goods_list[indexA].status
- })
- // 计算是否超过选择的最大次数
- let selectNum = `giftBagList[${index}].selectNum`
- let sum = 0
- this.data.giftBagList[index].goods_list.forEach(itemB => {
- if (itemB.status) {
- sum = ++sum
- }
- })
- this.setData({
- [selectNum]: sum
- })
- //添加选中的ID到列表中
- let list = {}
- let flag = false
- let selectIndex = ''
- selectGiftList.forEach((selectItem, Index) => {
- if (selectItem.goods_id == itemA.id) {
- flag = true
- selectIndex = Index
- }
- })
- if (!flag) {
- list.item_id = item.id
- list.goods_id = itemA.id
- list.num = 1
- list.goods_name = itemA.goods_name
- list.cover_url = itemA.cover_url
- selectGiftList.push(list)
- } else {
- selectGiftList.splice(selectIndex, 1)
- }
- this.setData({
- selectGiftList
- })
- },
- // 加产品
- add(e) {
- let index = e.currentTarget.dataset.index //分类的下标A
- let indexA = e.currentTarget.dataset.indexa //产品的下标
- let itemSum = e.currentTarget.dataset.item.num //每个分类的可选总次数
- let itemSelectNum = e.currentTarget.dataset.item.selectNum //每个分类已经选择的次数
- // 如果加购产品数量大于总次数就返回
- if (itemSelectNum + 1 > itemSum) {
- return
- }
- let giftBagNum = `giftBagList[${index}].goods_list[${indexA}].num`
- let selectNum = `giftBagList[${index}].selectNum`
- this.setData({
- [giftBagNum]: this.data.giftBagList[index].goods_list[indexA].num+1,
- [selectNum]:this.data.giftBagList[index].selectNum+1
- })
- let selectGiftList =[]
- this.data.giftBagList.forEach(item=>{
- item.goods_list.forEach(itemA=>{
- if(itemA.num>0){
- let list = {}
- list.item_id = item.id
- list.goods_id = itemA.id
- list.num = itemA.num
- list.goods_name = itemA.goods_name
- list.cover_url = itemA.cover_url
- selectGiftList.push(list)
- }
- })
- })
- this.setData({
- selectGiftList
- })
- },
- // 减产品
- reduce(e) {
- let index = e.currentTarget.dataset.index //分类的下标A
- let indexA = e.currentTarget.dataset.indexa //产品的下标
- let itemSelectNum = e.currentTarget.dataset.item.selectNum //每个分类已经选择的次数
- if ( this.data.giftBagList[index].goods_list[indexA].num == 0) {
- return
- }
- let giftBagNum = `giftBagList[${index}].goods_list[${indexA}].num`
- let selectNum = `giftBagList[${index}].selectNum`
- this.setData({
- [giftBagNum]: this.data.giftBagList[index].goods_list[indexA].num-1,
- [selectNum]:this.data.giftBagList[index].selectNum-1
- })
- },
- //确认选择
- affrimSelect() {
- // 判断是否分配完毕
- let flag = false
- this.data.giftBagList.forEach(item => {
- if (item.selectNum != item.num) {
- flag = true
- }
- })
- if (flag) {
- wx.showToast({
- title: '您还有商品没有选择完毕',
- icon: 'none'
- })
- return
- }
- wx.navigateTo({
- url: `/subPackagesE/pages/giftBagOrder/giftBagOrder?productList=${JSON.stringify(this.data.selectGiftList)}&&giftID=${this.data.id}`,
- })
- }
- })
|