123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- const app = getApp()
- const api = require('../../../request/api');
- const http = require('../../../request/http');
- const utils = require('../../../utils/util.js');
- const storeKeys = require('../../../utils/storageKeys.js');
- Page({
- data: {
- isLoading: true, //是否处于加载
- isLogOn: true, //是否已登录
- statusBarHeight: app.globalData.statusBarHeight,
- isAllSelect: false, //是否全选
- cartList: [], //购物车总数据
- validData: [], //购物车有效数据
- rulesSkuList: [], //弹出规格选择数组
- rulesSkuObj: {}, //弹出规则选中数据
- rulesIndex: 0, //当前正在选规则数据的索引
- showRulesPopup: false, //是否显示规则选择
- isShowD: false,
- showDetailsPopup:false, //是否显示查看明细Popup
- selectedCartList:[], //已勾选的数据
- userCouponItem:null, //可用优惠卷
- isEdit:false, //是否处于编辑状态
- isAllDelete:false, //是否全选删除
- showDeleteModal: false, //是否显示确认删除弹窗
- isShowDetails: false, //是否显示查看明细入口
- totalprice: "0.00", //合计价格
- activityPrice: 0, //活动优惠金额
- totalReduce:0, //共减金额
- finalPrice:0, //最终合计金额
- showStockUnModal: false, // 是否线上库存不足弹窗
- },
- onShow() {
- if (!utils.getStorageSync(storeKeys.TOKEN)) {
- this.setData({
- isLogOn: false
- })
- app.changeTabBarBadge();
- } else {
- this.setData({
- isLogOn: true,
- isEdit:false,
- isAllDelete:false
- })
- this.onPullDownRefresh();
- }
- },
- onPullDownRefresh: function () {
- wx.stopPullDownRefresh();
- this.getCartData();
- },
- // 获取购物车数据
- getCartData() {
- // wx.showLoading({
- // title: "加载中"
- // });
- http.request({
- url: api.URL + '/api/cart/list',
- method: 'GET',
- token: utils.getStorageSync(storeKeys.TOKEN),
- success: (res) => {
- let data = res.data.data;
- wx.hideLoading();
- //可用优惠卷
- let userCouponItem = null;
- //最终价格
- let finalPrice = data.sum_total_price;
- //活动优惠金额
- let activityPrice = data.activity_discount_total_price || 0;
- //共减金额
- let totalReduce = activityPrice;
- if(finalPrice > 0){
- finalPrice = parseFloat(parseFloat(finalPrice)-parseFloat(activityPrice)).toFixed(2)
- if(data.user_coupon && data.user_coupon.length > 0){
- //第一条数据为最优优惠卷
- userCouponItem = data.user_coupon[0];
- finalPrice = parseFloat(parseFloat(finalPrice)-parseFloat(userCouponItem.reduced_price)).toFixed(2)
- totalReduce = parseFloat(parseFloat(totalReduce)+parseFloat(userCouponItem.reduced_price)).toFixed(2)
- }
- }
- let isShowDetails = false;
- if((activityPrice && activityPrice > 0) || userCouponItem){
- isShowDetails = true;
- }
-
-
- this.setData({
- cartList: data.list,
- totalprice: data.sum_total_price,
- finalPrice,
- userCouponItem,
- isShowDetails,
- totalReduce,
- activityPrice,
- isLoading: false
- })
- //有效数据
- let validData = [];
- let selectedCartList = [];
- data.list && data.list.forEach((item, index) => {
- this.selectComponent("#action" + index).closeButtonGroup();
- //商品状态err_status 0正常 1库存不足 2商品失效
- if (item.goods.status == 10 && item.err_status == 0) {
- validData.push(item)
- }
- })
- //处理全选
- let isAllSelect = validData.length > 0 ? true : false;
- validData.forEach((item, index) => {
- this.selectComponent("#action" + index).closeButtonGroup();
- if (!item.selected) {
- isAllSelect = false;
- }else{
- selectedCartList.push(item)
- }
- })
-
- this.setData({
- isAllSelect,
- validData,
- selectedCartList,
- isAllDelete:false
- })
- app.changeTabBarBadge(data.cartTotal);
- },
- error: (res) => {
- wx.hideLoading();
- utils.toast(res.data.message)
- },
- })
- },
- navigateTo(e) {
- if (!this.data.isShowD) {
- app.navigateTo(e);
- }
- },
- changeEdit(){
- this.setData({
- isEdit: !this.data.isEdit
- })
- },
- // 回到首页
- goHome() {
- wx.switchTab({
- url: '/pages/tabBar/index/index'
- })
- },
- // 全部商品
- goAllGoods() {
- wx.switchTab({
- url: '/pages/tabBar/allGoods/allGoods'
- })
- },
-
- //商品勾选/取消勾选 -- 结算
- changeCartSelect(e) {
- let {
- index
- } = e.currentTarget.dataset;
- let item = this.data.cartList[index];
- //已下架
- if (item.goods.status == 20 || item.err_status == 1 || item.err_status == 2) {
- return;
- }
- let cartIds = [];
- let selected = item.selected == 1 ? 0 : 1
- cartIds.push(item.id)
- this.goodsSelect(cartIds, selected);
- },
- //商品勾选/取消勾选 -- 删除
- changeCartDelete(e){
- let { index } = e.currentTarget.dataset;
- let item = this.data.cartList[index];
- this.setData({
- [`cartList[${index}].isDelete`]: !item.isDelete
- })
- this.changeAllDelete();
- },
- //全选 -- 删除
- allDelete(){
- let isAllDelete = !this.data.isAllDelete;
- let cartList = this.data.cartList;
- for(let i = 0; i<cartList.length; i++){
- cartList[i].isDelete = isAllDelete
- }
- this.setData({
- cartList,
- isAllDelete
- })
- },
- //处理全选
- changeAllDelete(){
- let isAllDelete = true;
- this.data.cartList.forEach((item) => {
- if (!item.isDelete) {
- isAllDelete = false;
- }
- })
- this.setData({isAllDelete})
- },
- //批量删除
- deleteCart: utils.throttle(function (e) {
- let cartIds = [];
- this.data.cartList && this.data.cartList.forEach((item) => {
- if (item.isDelete) {
- cartIds.push(item.id)
- }
- })
- if (cartIds.length <= 0) {
- utils.toast("请勾选需要删除的商品")
- return;
- }
- this.setData({
- showDeleteModal:true
- })
- }),
- //确定批量删除
- confirmDelete: utils.throttle(function (e) {
- let cartIds = [];
- this.data.cartList && this.data.cartList.forEach((item)=>{
- if(item.isDelete){
- cartIds.push(item.id)
- }
- })
- let data = {
- cartIds
- }
- wx.showLoading({
- title: "加载中"
- });
- http.request({
- url: api.URL + '/api/cart/delete',
- method: 'POST',
- data,
- token: utils.getStorageSync(storeKeys.TOKEN),
- success: (res) => {
- this.getCartData();
- this.hideDeleteModal()
- },
- error: (res) => {
- wx.hideLoading();
- utils.toast(res.data.message)
- },
- })
- }),
- //取消批量删除
- hideDeleteModal(){
- this.setData({
- showDeleteModal:false
- })
- },
- //执行商品勾选/取消勾选
- goodsSelect(cartIds, selected) {
- let data = {
- cartIds, //购物车ID集
- selected //勾选状态 0不勾选 1勾选
- }
- // wx.showLoading({
- // title: "加载中"
- // });
- http.request({
- url: api.URL + '/api/cart/goods_select',
- method: 'POST',
- data,
- token: utils.getStorageSync(storeKeys.TOKEN),
- success: (res) => {
- this.getCartData();
- },
- error: (res) => {
- wx.hideLoading();
- utils.toast(res.data.message)
- },
- })
- },
- //增加/删减商品数量
- changeCartNumber: utils.throttle(function (e) {
- let {
- index,
- state
- } = e.currentTarget.dataset;
- let cartList = this.data.cartList;
- let item = cartList[index];
- let goodsNum = item.goods_num;
- // state:1增加 0删减
- if (state == 1) {
- if (goodsNum >= item.goods.skuInfo.stock_num) {
- utils.toast("库存不足")
- return;
- }
- goodsNum++
- } else {
- if (goodsNum <= 1) {
- utils.toast("不能再减了~");
- return;
- }
- goodsNum--;
- }
- let data = {
- "goodsId": item.goods_id, //商品ID
- "goodsSkuId": item.goods_sku_id, //商品规格ID
- "goodsNum": goodsNum //修改后的商品规格数量,不能小于1
- }
- // wx.showLoading({
- // title: "加载中"
- // });
- http.request({
- url: api.URL + '/api/cart/upd',
- method: 'POST',
- data,
- token: utils.getStorageSync(storeKeys.TOKEN),
- success: (res) => {
- this.getCartData();
- },
- error: (res) => {
- wx.hideLoading();
- utils.toast(res.data.message)
- },
- })
- }, 800),
- //删除购物车数据
- cartDelete(e) {
- let index = e.currentTarget.dataset.index;
- let item = this.data.cartList[index];
- let data = {
- cartIds: [item.id]
- }
- // wx.showLoading({
- // title: "加载中"
- // });
- http.request({
- url: api.URL + '/api/cart/delete',
- method: 'POST',
- data,
- token: utils.getStorageSync(storeKeys.TOKEN),
- success: (res) => {
- this.getCartData();
- },
- error: (res) => {
- wx.hideLoading();
- utils.toast(res.data.message)
- },
- })
- },
- //全选
- allSelect: utils.throttle(function (e) {
- let cartIds = [];
- let selected = this.data.isAllSelect ? 0 : 1;
- this.data.cartList.forEach((item) => {
- if (item.goods.status == 10 && item.err_status == 0) {
- cartIds.push(item.id)
- }
- })
- if (cartIds.length <= 0) {
- return;
- }
- this.goodsSelect(cartIds, selected);
- }, 800),
- //结算
- settlement() {
- let cartIds = "";
- this.data.cartList.forEach((item, index) => {
- if (item.selected == 1) {
- cartIds += index < this.data.cartList.length - 1 ? item.id + ',' : item.id;
- }
- })
- if (!cartIds) {
- utils.toast("您还没有选择哦~")
- return;
- }
- let data = {
- mode: "cart",
- cartIds
- };
- let url = "/pages/cart/pages/confirmOrder/confirmOrder?data=" + JSON.stringify(data);
- app.navigateToUrl(url);
- },
- //底部显示规则选择
- showRulesPopup(e) {
- let index = e.currentTarget.dataset.index;
- let item = this.data.cartList[index];
- if (item.err_status == 1) {
- return;
- }
- this.data.rulesIndex = index;
- wx.showLoading({
- title: "加载中"
- });
- http.request({
- url: api.URL + '/api/goods/' + item.goods.goods_id,
- method: 'GET',
- token: utils.getStorageSync(storeKeys.TOKEN),
- success: (res) => {
- wx.hideLoading();
- let skuList = res.data.data.skuList;
- //目前选中的规则
- let skuId = item.goods.skuInfo.goods_sku_id;
- let rulesSkuObj = {};
- if (skuList) {
- for (let i = 0; i < skuList.length; i++) {
- if (skuList[i].stock_num < 1 || !skuList[i].stock_num) {
- skuList[i].lack = true;
- }
- if (skuId == skuList[i].goods_sku_id) {
- rulesSkuObj = skuList[i];
- }
- }
- }
- this.setData({
- showRulesPopup: true,
- rulesSkuList: skuList,
- rulesSkuObj
- })
- },
- error: (res) => {
- wx.hideLoading();
- utils.toast(res.data.message)
- },
- })
- },
- //规格切换
- changeSpec(e) {
- let index = e.currentTarget.dataset.index;
- let item = this.data.rulesSkuList[index];
- if (item.lack) {
- utils.toast("库存不足")
- return;
- }
- this.setData({
- rulesSkuObj: item
- })
- },
- //确认更改规则
- confirmBuyPopup() {
- let cartItem = this.data.cartList[this.data.rulesIndex];
- let cartId = cartItem.id;
- let goodsId = cartItem.goods.goods_id;
- let goodsSkuId = this.data.rulesSkuObj.goods_sku_id;
- let data = {
- cartId, //购物车ID
- goodsId, //商品ID
- goodsSkuId //商品规格ID
- }
- // wx.showLoading({
- // title: "加载中"
- // });
- http.request({
- url: api.URL + '/api/cart/updSku',
- method: 'POST',
- data,
- token: utils.getStorageSync(storeKeys.TOKEN),
- success: (res) => {
- this.getCartData();
- this.hideRulesPopup()
- },
- error: (res) => {
- wx.hideLoading();
- utils.toast(res.data.message)
- },
- })
- },
- //底部隐藏规则选择
- hideRulesPopup() {
- this.setData({
- showRulesPopup: false
- })
- },
- changeTap(e) {
- this.data.isShowD = e.detail
- },
- //显示查看明细
- showDetailsPopup(){
- let showDetailsPopup = !this.data.showDetailsPopup;
- this.setData({
- showDetailsPopup
- })
- },
- //隐藏查看明细
- hideDetailsPopup(){
- this.setData({
- showDetailsPopup: false
- })
- },
- onHide(){
- this.hideDetailsPopup()
- }
- })
|