Files
CompanyProject/衣莎项目/源码/YiShaXiYi/pages/laundry/laundry.js
2024-09-27 01:32:49 +08:00

444 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// pages/laundry/laundry.js
const app = getApp();
const regeneratorRuntime = app.loadAsync()
const model = app.loadModel("index")
const util = app.loadUtils("util")
const http = app.loadHttp()
Page({
/**
* 页面的初始数据
*/
data: {
system: app.globalData.system, //设备相关信息
pageTitle: "洗衣", //头部标题
isAccount: false, // 公众号
isBack: true,//显示返回
visible: false,//购物弹窗
isTips: true,//顶部小提示
laundryBig: [
{
id: null,
name: ""
}
],//洗衣大分类
bigSelect: null,//大分类的选中
laundrySom: [],//洗衣小分类
cart: [],//购物车数据
allPrice: 0,//全部价格
page: 1,//分页
isPage: false,
group_type: 0,//团体id
type: 1,//如果是1则是普洗2是工服
},
// 获取大分类列表
async getLaundryBig() {
let data = {
group_type: this.data.group_type,
type: this.data.type
}
let res = await http.http.request2({ url: `${http.api.shopCategory}`, method: "GET", data: data })
this.setData({
laundryBig: res.data.data,
bigSelect: this.data.bigSelect != null ? this.data.bigSelect : res.data.data[0].id
})
this.getLaundrySom()
},
// 获取小分类列表
async getLaundrySom() {
if (this.data.isPage) {
wx.showToast({
title: '已经到底了哦!',
icon: 'none',
duration: 2000
})
return
}
wx.showLoading({
title: '加载中',
})
let res = await http.http.request1({ url: `${http.api.shopProduct}/${this.data.bigSelect}`, method: "GET", data: { page: this.data.page, limit: 10 } })
let som = res.data.data.list
let laundrySom = this.data.laundrySom
if (som.length <= 0) {
wx.hideLoading()
wx.showToast({
title: '暂无数据',
icon: 'none',
duration: 2000
})
return
}
if (som.length < 10) {
this.setData({
isPage: true
})
}
som.map((v, i) => {
som[i].num = 0
})
this.setData({
laundrySom: [...laundrySom, ...som],
})
this.getCart()
wx.hideLoading()
},
// scroll-view 触底
bindscrolltolower() {
this.setData({
page: ++this.data.page
})
this.getLaundrySom()
},
//弹出层开启关闭
onClose() {
this.setData({
visible: !this.data.visible
})
},
// 打开弹出层
onOpen() {
this.setData({
visible: true
})
},
// 大分类选中
bigType(e) {
this.setData({
bigSelect: e.currentTarget.dataset.id,
page: 1,
isPage: false,
laundrySom: []
})
this.getLaundrySom()
},
// 把最新的购物车数据set到本地缓存
async setCart() {
model.storage.setStorage("cart", this.data.cart)
},
// 获取缓存中的购物车数据
async getCart() {
let cartStorage = await model.storage.getSetting("cart") || []
let laundrySom = this.data.laundrySom
laundrySom.map((v, i) => {
let m = cartStorage.filter(n => {
return v.id == n.id
})
if (m.length > 0) {
laundrySom[i] = m[0]
}
})
this.setData({
laundrySom: laundrySom,
cart: cartStorage
})
this.calComPri()
},
// 加商品方法
plusShop(e) {
let item = e.currentTarget.dataset.item //当前要添加的商品
let cart = this.data.cart //购物车
let Som = this.data.laundrySom // 商品列表
let num = ++item.num
if (cart.length == 0) {
item.num = num
this.setData({
cart: [item]
})
} else {
// 如果cart循环完还没有重复项,则把item push进去
for (let i = 0; i < cart.length; i++) {
// 点击的时候判断购物车是否有窗帘套餐
if (cart[i].id == 17018 || cart[i].id == 17019 || cart[i].id == 17020) {
if (item.id == 17018 || item.id == 17019 || item.id == 17020) {
console.log("允许继续添加跟多的窗帘商品")
} else {
model.msg.dataMsg({
content: "预售套餐不与其他衣物同时下单哦",
type: "warning"
});
Som.map((v, i) => {
Som[i].num = 0
})
this.setData({
cart: [],
laundrySom: Som
})
this.setCart()
this.calComPri()
return
}
// 如果购物车都是普通套餐
} else {
// 之后你在普通套餐的购物车中添加了窗帘淘宝
if (item.id == 17018 || item.id == 17019 || item.id == 17020) {
model.msg.dataMsg({
content: "预售套餐不与其他衣物同时下单哦",
type: "warning"
});
Som.map((v, i) => {
Som[i].num = 0
})
this.setData({
cart: [],
laundrySom: Som
})
this.setCart()
this.calComPri()
return
}
}
if (cart[i].id == item.id) {
cart[i].num = num
break;
}
if (cart.length - 1 == i) {
cart.push(item)
}
}
this.setData({
cart: cart
})
}
Som.map((v, i) => {
if (v.id == item.id) {
Som[i].num = num
}
})
this.setData({
laundrySom: Som
})
// 每当修改购物车时把最近数据放到本地缓存
this.setCart()
this.calComPri()
},
// 减商品的方法
reduceShop(e) {
let that = this
let item = e.currentTarget.dataset.item //当前要添加的商品
let cart = that.data.cart //购物车
let Som = that.data.laundrySom // 商品列表
let num = --item.num
cart.map((v, i) => {
if (v.id == item.id) {
cart[i].num = num
if (cart[i].num <= 0) {
cart.splice(i, 1)
}
}
})
Som.map((v, i) => {
if (v.id == item.id) {
Som[i].num = num
}
})
// 判断如果num减少到0时提示是否删除
if (num == 0) {
wx.showModal({
title: '删除商品',
content: '点击确定删除',
confirmText: "确认",
cancelText: "取消",
success(res) {
if (res.confirm) {
that.setData({
laundrySom: Som,
cart: cart
})
// 每当修改购物车时把最近数据放到本地缓存
that.setCart()
that.calComPri()
}
}
})
} else {
that.setData({
laundrySom: Som,
cart: cart
})
// 每当修改购物车时把最近数据放到本地缓存
that.setCart()
this.calComPri()
}
},
// 清空购物车
emptyCart() {
let laundrySom = this.data.laundrySom
let that = this
if (this.data.cart.length <= 0) {
model.msg.dataMsg({
content: "暂无商品",
type: "warning"
})
return
}
wx.showModal({
title: '清空购物车',
content: '点击确定清空',
confirmText: "确认",
cancelText: "取消",
success(res) {
if (res.confirm) {
laundrySom.map((v, i) => {
laundrySom[i].num = 0
})
that.setData({
cart: [],
laundrySom: laundrySom
})
that.setCart()
that.onClose()
that.calComPri()
// 删除成功提示用户
model.msg.dataMsg({
content: "删除成功",
type: "success"
})
}
}
})
},
// 点击确定
async _sure() {
let isCurtains = 0; // 0 没有窗帘, 1 有窗帘
let laundrySom = this.data.laundrySom
let userInfo = app.globalData.userInfo
let cart = this.data.cart
let that = this
if (cart.length > 0) {
// 只要购物车中第一条商品是窗帘其中一个
if(cart[0].id == 17018 || cart[0].id == 17019 || cart[0].id == 17020 ) {
isCurtains = 1
} else {
isCurtains = 0
}
let isLogin = await util.isLoginJump()
if (isLogin == 1) return;
} else if (cart.length <= 0) {
return
}
// if (userInfo.cupboard_num != -1) {
// wx.navigateTo({
// url: "/user/wardrobe/wardrobe"
// })
// } else {
wx.navigateTo({
url: "/user/appointment/appointment?shopCart=" + JSON.stringify(cart) + '&type=' + this.data.type + '&group_type=' + this.data.group_type + '&isCurtains=' + isCurtains
})
// }
// laundrySom.map((v, i) => {
// laundrySom[i].num = 0
// })
// that.setData({
// cart: [],
// laundrySom: laundrySom
// })
// that.setCart()
},
// 计算商品价格
calComPri() {
let cart = this.data.cart
let all = 0
cart.map((v, i) => {
all += v.num * v.price
})
this.setData({
allPrice: all.toFixed(2)
})
},
// 关闭顶部小提示
closeTips() {
this.setData({
isTips: false
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
if (options.id == "36") {
console.log("36")
// true 从公众号进入
this.setData({
isAccount: true
})
}
// 进入页面清除缓存中的购物车数据
model.storage.setStorage("cart", [])
// 如果有group_type说明是团体用户
if (!!options.group_type) {
this.setData({
group_type: options.group_type,
type: options.type
})
}
if (!!options.id) {
this.setData({
bigSelect: options.id
})
} else {
this.setData({
bigSelect: this.data.laundryBig[0].id
})
}
this.getLaundryBig()
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
this.calComPri()
// TODO 不需要重复请求
if (this.data.isAccount) {
this.getLaundryBig()
}
// this.getLaundryBig()
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
});