H5端齐采药项目,uniapp框架
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

937 lines
22 KiB

import nsLoginVue from "../../../../components/ns-login/ns-login.vue";
import cartVue from "../../cart.vue";
import util from "../../../../common/js/util";
// import Big from 'big.js'
export default {
data() {
return {
token: '',
cartData: [], // 购物车
cartArray1: [],
cartArray: [],
cartlength: 0,
cartBoolen: false,
checkAll: true,
cartCheckList: [],
totalPrice: '0.00',
siteGoodsCount: 0,
totalCount: 0,
modifyFlag: false,
isSub: false,
invalidGoods: [], // 失效商品集合
isIphoneX: false, //判断手机是否是iphoneX以上,
cartBottom: '56px',
isAction: false, // 是否操作删除
goodsSkuDetail: null,
discount: {},
manjian: {},
receiveSub: false,
discountPopupShow: false,
showPopup: false,
// 购物车选中商品id
carts_goods_id: undefined,
// 删除弹出框
showOverlay: false,
swipeActionOptions: [{
text: '收藏',
style: {
backgroundColor: '#3c9cff'
}
}, {
text: '删除',
style: {
backgroundColor: '#f56c6c'
}
}],
swipeActionOptions1: [{
text: '已收藏',
style: {
backgroundColor: '#fcbd71',
paddingRight: "8px"
}
}, {
text: '删除',
style: {
backgroundColor: '#f56c6c'
}
}],
order_limit_start_money: 0,
};
},
onLoad() {
uni.hideTabBar();
console.log('页面 Load');
console.log(this.$refs.discountPopup);
// 监听同步购物车
uni.$on('syncCartList', () => {
try {
this.$nextTick(() => {
this.$refs.goodrecommend.syncCartList();
})
} catch (e) {
//TODO handle the exception
// this.init();
console.log(e);
}
})
},
mounted() {
if (uni.getStorageSync('token')) {
this.init();
}
},
onPullDownRefresh() {
this.$nextTick(() => {
uni.stopPullDownRefresh();
this.init();
this.$refs.goodrecommend.isAll = true;
this.$refs.goodrecommend.init();
this.$refs.goodrecommend.getLikeList();
uni.hideTabBar();
})
},
onShow() {
// console.log(uni.$currentMethod, 'uni.$currentMethod');
this.isAction = false;
// if (uni.$currentMethod !== 'navigateBack') {
try {
this.$nextTick(() => {
this.$refs.goodrecommend.syncCartList();
})
} catch (e) {
//TODO handle the exception
// this.init();
console.log(e);
}
// };
// console.log('页面 Show');
// console.log(this.$refs.discountPopup.showPopup);
},
onReady() {
if (!uni.getStorageSync('token')) {
if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
};
console.log('页面 Ready');
console.log(this.$refs.discountPopup);
},
onUnload: function() {
console.log('页面 Unload');
console.log(this.$refs.discountPopup);
},
computed: {
storeToken() {
return this.$store.state.token;
},
},
watch: {
storeToken: function(nVal, oVal) {
this.getCartData();
},
'siteItem.cartList'() {
this.watchCart()
},
cartlength: function(nVal, oVal) {
if (nVal == this.cartArray1.length) {
this.handleCartData(this.cartArray)
}
},
'$store.state.cartNumber': {
handler: function(newVal, oldVal) {
if (uni.getStorageSync('token')) {
this.$api.sendRequest({
url: '/api/cart/goodslists',
success: res => {
if (res.code >= 0) {
this.token = uni.getStorageSync('token');
if (res.data.length) {
this.handleCartData(res.data);
} else {
this.cartData = [];
}
} else {
this.token = '';
}
},
fail: res => {}
});
}
}
}
},
methods: {
init() {
if (uni.getStorageSync('token')) {
this.cartlength = 0;
this.cartArray1 = [];
this.cartArray = [];
this.getCartData();
this.configInit();
this.calculationTotalPrice();
this.discountCalculate();
this.getCartNumber();
this.receiveSub = false;
} else {
this.token = '';
this.cartData = [];
}
this.isIphoneX = this.$util.uniappIsIPhoneX();
if (this.$util.uniappIsIPhone11()) {
this.cartBottom = '90px';
}
uni.$on('cartBottomShow', data => {
this.cartBottomShow = data;
});
uni.$on('refresh', (data) => {
console.log(`refresh`, data);
if (data) {
this.getCartData();
this.$refs.goodrecommend.isAll = true;
this.$refs.goodrecommend.init();
this.$refs.goodrecommend.getLikeList();
this.cartBottomShow = true;
}
})
},
showStartMoney(num1, num2) {
let distance;
distance = Math.round(num1 * 10000 - num2 * 10000) / 10000;
return parseFloat(distance.toFixed(2));
},
configInit() {
this.$api.sendRequest({
url: '/api/config/init',
success: res => {
this.order_limit_start_money = Number(res.data.order_limit_start_money);
console.log(this.order_limit_start_money);
},
fail: res => {}
})
},
watchCart() {
this.$refs.actionItem.forEach(ref => {
ref.$watch('status', (val) => {})
})
},
initNum(item) {
let res = Number(item.num)
let max_buy = Number(item.max_buy) > 0 && Number(item.max_buy) < Number(item.stock) ? Number(item
.max_buy) :
Number(item.stock);
max_buy = max_buy == 0 ? 1 : Number(item.max_buy);
if (res > max_buy && !res) res = max_buy;
return res;
},
/**
* 获取购物车数据
*/
getCartData() {
// this.cartData = []
this.$api.sendRequest({
url: '/api/cart/goodslists',
success: res => {
if (res.code !== 0)
this.token = '';
this.token = uni.getStorageSync('token');
this.cartArray = res.data
this.handleCartData(this.cartArray)
},
complete: () => {
if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
}
});
},
collectORdel(e) {
// index 0收藏 1删除 name当前选中行下标
let collectStatus = this.cartData[0].cartList[e.name].is_collect;
this.carts_goods_id = this.cartData[0].cartList[e.name].cart_id;
// 收藏
if (e.index == 0) {
if (collectStatus == 0) {
let {
goods_id,
sku_id,
sku_name,
sku_price,
sku_image,
} = {
...this.cartData[0].cartList[e.name]
}
uni.showModal({
title: '提示',
content: '确定收藏该商品吗?',
cancelText: "取消", // 取消按钮的文字
confirmText: "确认", // 确认按钮的文字
showCancel: true, // 是否显示取消按钮,默认为 true
confirmColor: '#f55850',
cancelColor: '#39B54A',
success: (res) => {
if (res.confirm) {
this.$api.sendRequest({
url: '/api/goodscollect/add',
data: {
goods_id,
sku_id,
sku_name,
sku_price,
sku_image,
},
success: res => {
this.$util.showToast({
title: res.message
});
this.cartlength = 0;
this.cartArray1 = [];
this.cartArray = [];
this.init();
this.$refs.goodrecommend.isAll = true;
this.$refs.goodrecommend.init();
this.$refs.goodrecommend.getLikeList();
},
})
//点击确定之后执行的代码
} else if (res.cancel) {
}
}
})
}
}
// 删除
else if (e.index == 1) {
uni.showModal({
title: '提示',
content: '确定删除该商品吗?',
cancelText: "取消", // 取消按钮的文字
confirmText: "确认", // 确认按钮的文字
showCancel: true, // 是否显示取消按钮,默认为 true
confirmColor: '#f55850',
cancelColor: '#39B54A',
success: (res) => {
if (res.confirm) {
this.$api.sendRequest({
url: '/api/cart/delete',
data: {
cart_id: this.carts_goods_id
},
success: res => {
this.$util.showToast({
title: res.message
});
this.cartlength = 0;
this.cartArray1 = [];
this.cartArray = [];
this.init();
this.$refs.goodrecommend.isAll = true;
this.$refs.goodrecommend.init();
this.$refs.goodrecommend.getLikeList();
},
}); //点击确定之后执行的代码
} else if (res.cancel) {
}
}
})
}
},
/**
* 处理购物车数据结构
*/
handleCartData(data) {
this.invalidGoods = [];
// 保存一下之前的购物车状态
const copyCartData = util.deepClone(this.cartData)
this.cartData = [];
var temp = {};
for (let i in data) {
const item = data[i];
if (Number(item.goods_state) !== 1) {
this.invalidGoods.push(item);
continue;
}
// 如果最小限购超出库存则该商品失效
if (item.min_buy > 0 && item.min_buy > item.stock) {
this.invalidGoods.push(item);
continue;
}
// item.checked = false; //二期需求购物车默认未选中
item.checked = (copyCartData.length > 0 && copyCartData[0].cartList.find(copyItem => {
return item.cart_id === copyItem.cart_id
})?.checked) || false
item.edit = false;
if (temp['site_' + item.site_id] != undefined) {
temp['site_' + item.site_id].cartList.push(item);
} else {
temp['site_' + item.site_id] = {
siteId: item.site_id,
siteName: item.site_name,
edit: false,
checked: true,
cartList: [item]
};
}
}
// this.cartData = [];
Object.keys(temp).forEach(key => {
this.cartData.push(temp[key]);
});
this.calculationTotalPrice();
if (this.cartData.length) {
this.cartData[0].cartList.forEach(v => {
if (v.sku_spec_format) {
v.sku_spec_format = JSON.parse(v.sku_spec_format);
} else {
v.sku_spec_format = [];
}
});
}
console.log(this.cartData, `this.cartData11111111111`);
this.cartBottomShow = true;
if (this.invalidGoods.length) {
this.invalidGoods.forEach(v => {
if (v.sku_spec_format) {
v.sku_spec_format = JSON.parse(v.sku_spec_format);
} else {
v.sku_spec_format = [];
}
});
}
},
/**
* 单选
* @param {Object} index
*/
singleElection(siteIndex, index) {
this.cartData[siteIndex].cartList[index].checked = !this.cartData[siteIndex].cartList[index]
.checked;
this.calculationTotalPrice();
},
/**
* 店铺全选
* @param {Object} checked
*/
siteAllElection(checked, index) {
this.cartData[index].checked = checked;
this.cartData[index].cartList.forEach(item => {
item.checked = checked;
});
this.calculationTotalPrice();
},
/**
* 全选
*/
allElection(checked) {
if (typeof checked == 'boolean') {
this.checkAll = checked;
} else {
this.checkAll = !this.checkAll;
}
if (this.cartData.length) {
this.cartData.forEach(siteItem => {
siteItem.checked = this.checkAll;
siteItem.cartList.forEach(item => {
item.checked = this.checkAll;
});
});
}
console.log(this.cartData);
this.calculationTotalPrice();
},
/**
* 计算购物车总价
*/
calculationTotalPrice() {
this.cartCheckList = []
if (this.cartData.length) {
let totalPrice = 0,
totalCount = 0,
siteGoodsCount = 0,
siteAllElectionCount = 0;
this.cartData.forEach(siteItem => {
siteItem.cartList.forEach(item => {
if (item.checked) {
this.cartCheckList.push(item)
siteGoodsCount += 1;
totalCount += parseInt(item.num);
if (Number(item.member_price) > 0 && Number(item.member_price) <
Number(item
.discount_price)) {
totalPrice += item.member_price * item.num;
} else {
totalPrice += item.discount_price * item.num;
}
}
});
if (siteItem.cartList.length == siteGoodsCount) {
siteItem.checked = true;
siteAllElectionCount += 1;
} else {
siteItem.checked = false;
}
});
this.totalPrice = totalPrice.toFixed(2);
this.totalPrice = Number(this.totalPrice);
this.siteGoodsCount = siteGoodsCount;
this.totalCount = totalCount;
this.checkAll = this.cartData.length == siteAllElectionCount;
} else {
this.totalPrice = '0.00';
this.totalCount = 0;
}
this.modifyFlag = false;
this.discountCalculate();
},
collectCart() {
let arr = [];
this.cartCheckList.forEach(item => {
if (!item.is_collect) {
arr.push(item)
}
})
if (!arr.length) {
this.$util.showToast({
title: '选中商品已全部收藏'
});
return
}
uni.showModal({
title: '提示',
content: '确定要收藏这些商品吗?',
success: res => {
if (res.confirm) {
arr.forEach(item => {
this.$api.sendRequest({
url: '/api/goodscollect/add',
data: {
goods_id: item.goods_id,
sku_id: item.sku_id,
sku_name: item.sku_name,
sku_price: item.sku_price,
sku_image: item.sku_image,
},
success: res => {
this.$util.showToast({
title: res.message
});
this.cartlength = 0;
this.cartArray1 = [];
this.cartArray = [];
this.init()
this.$refs.goodrecommend.isAll = true;
this.$refs.goodrecommend.init();
this.$refs.goodrecommend.getLikeList();
}
})
})
}
}
})
},
/**
* 删除购物车
* @param {Object} siteIndex
* @param {Object} cartIndex
*/
deleteCart(tag, siteIndex, cartIndex) {
var cart_id = [];
if (tag == 'all') {
for (let i = 0; i < this.cartData.length; i++) {
for (let j = 0; j < this.cartData[i].cartList.length; j++) {
if (this.cartData[i].cartList[j].checked) cart_id.push(this.cartData[i].cartList[j]
.cart_id);
}
}
} else {
cart_id.push(this.cartData[siteIndex].cartList[cartIndex].cart_id);
}
if (cart_id.length == 0) {
this.$util.showToast({
title: '请选择要删除的商品'
});
return;
}
uni.showModal({
title: '提示',
content: '确定要删除这些商品吗?',
success: res => {
if (res.confirm) {
cart_id = cart_id.toString();
this.calculationTotalPrice();
this.getCartNumber();
this.$api.sendRequest({
url: '/api/cart/delete',
data: {
cart_id
},
success: res => {
if (res.code >= 0) {
if (tag == 'all') {
for (var i = 0; i < this.cartData.length; i++) {
for (var j = 0; j < this.cartData[i]
.cartList
.length; j++) {
var item = this.cartData[i].cartList[j];
if (item.checked) {
this.cartData[i].cartList.splice(j,
1);
j = -1;
}
}
if (this.cartData[i].cartList.length == 0) {
this.cartData.splice(i, 1);
i = -1;
}
}
} else {
this.cartData[siteIndex].cartList.splice(
cartIndex, 1);
if (this.cartData[siteIndex].cartList.length ==
0) this
.cartData.splice(siteIndex, 1);
}
this.init();
this.$refs.goodrecommend.isAll = true;
this.$refs.goodrecommend.init();
this.$refs.goodrecommend.getLikeList();
} else {
this.$util.showToast({
title: res.message
});
}
}
});
}
}
});
},
/**
* 变更购物车数量
* @param {Object} params
*/
cartNumChange(e, params) {
if (isNaN(e.value)) return;
let data = this.cartData[params.siteIndex].cartList[params.cartIndex],
max_buy = data.is_limit && data.max_buy > 0 && data.max_buy < data.stock ? data.max_buy : data
.stock,
min_buy = data.min_buy > 0 ? data.min_buy : 1;
if (e.value > max_buy) e.value = max_buy;
if (e.value < min_buy) e.value = min_buy;
this.modifyFlag = true;
this.$api.sendRequest({
url: '/api/cart/edit',
data: {
num: e.value,
cart_id: this.cartData[params.siteIndex].cartList[params.cartIndex].cart_id
},
success: res => {
if (res.code >= 0) {
this.cartData[params.siteIndex].cartList[params.cartIndex].num = e.value;
this.$store.dispatch('getCartNumber');
this.discountCalculate();
// this.init()
// this.$refs.goodrecommend.isAll = true;
// this.$refs.goodrecommend.init();
// this.$refs.goodrecommend.getLikeList();
} else {
this.$util.showToast({
title: res.message
});
}
}
});
},
/**
* 结算
*/
settlement() {
uni.removeStorageSync('buyer_message');
if (this.totalCount > 0) {
let cart_ids = [];
console.log(this.cartData);
this.cartData.forEach(siteItem => {
siteItem.cartList.forEach(item => {
if (item.checked && item.num) {
cart_ids.push(item.cart_id);
}
});
});
if (this.discount.coupon_info && this.discount.coupon_info.receive_type == 'wait') this
.receiveCoupon(
this.discount.coupon_info.coupon_type_id, false);
if (this.isSub) return;
this.isSub = true;
uni.removeStorageSync('delivery');
uni.setStorage({
key: 'orderCreateData',
data: {
cart_ids: cart_ids.toString()
},
success: () => {
this.$util.redirectTo('/page_goods/payment/payment');
this.isSub = false;
}
});
}
},
/**
* 清空失效商品
*/
clearInvalidGoods() {
uni.showModal({
title: '提示',
content: '确定要清空这些商品吗?',
success: res => {
if (res.confirm) {
var cart_ids = [];
this.invalidGoods.forEach(goodsItem => {
cart_ids.push(goodsItem.cart_id);
});
if (cart_ids.length) {
this.$api.sendRequest({
url: '/api/cart/delete',
data: {
cart_id: cart_ids.toString()
},
success: res => {
if (res.code >= 0) {
this.invalidGoods = [];
this.init();
} else {
this.$util.showToast({
title: res.message
});
}
}
});
}
}
}
});
},
imageError(siteIndex, cartIndex) {
this.cartData[siteIndex].cartList[cartIndex].sku_image = this.$util.getDefaultImage().goods;
this.$forceUpdate();
},
toGoodsDetail(item) {
this.$util.redirectTo('/page_goods/detail/detail', {
sku_id: item.sku_id,
goods_id: item.goods_id
});
},
// 购物车数量
getCartNumber() {
if (uni.getStorageSync('token')) {
this.$store.dispatch('getCartNumber');
this.resetEditStatus();
this.$refs.goodrecommend.init();
this.$refs.goodrecommend.getLikeList();
this.discountCalculate()
}
},
goodsLimit(event, index) {
let data = this.cartData[0].cartList[index];
if (event.type == 'plus') {
if (data.max_buy > 0 && data.max_buy < data.stock) {
this.$util.showToast({
title: '该商品每人限购' + data.max_buy + '件'
});
} else {
this.$util.showToast({
title: '库存不足'
});
}
} else {
this.$util.showToast({
title: '最少购买' + event.value + '件'
});
}
},
toLogin() {
this.$refs.login.open();
},
// 重置编辑状态
resetEditStatus() {
if (this.cartData.length) {
for (var i = 0; i < this.cartData[0].cartList.length; i++) {
this.cartData[0].cartList[i].edit = false;
}
this.$forceUpdate();
}
},
changeAction() {
this.isAction = !this.isAction;
this.resetEditStatus();
},
selectSku(data) {
let goodsSkuDetail = this.$util.deepClone(data);
if (goodsSkuDetail.goods_spec_format) goodsSkuDetail.goods_spec_format = JSON.parse(goodsSkuDetail
.goods_spec_format);
this.goodsSkuDetail = goodsSkuDetail;
console.log(this.goodsSkuDetail);
setTimeout(() => {
this.$refs.selectSku.show('confirm', (sku_id, num) => {
this.$api.sendRequest({
url: '/api/cart/editcartsku',
data: {
cart_id: data.cart_id,
sku_id: sku_id,
num: num
},
success: res => {
if (res.code >= 0) {
this.invalidGoods = [];
this.cartlength = 0;
this.cartArray1 = [];
this.cartArray = [];
// this.getCartData();
this.init()
} else {
this.$util.showToast({
title: res.message
});
}
}
});
}, this.goodsSkuDetail);
})
},
toggleDiscountPopup() {
console.log(this.$refs.discountPopup);
// this.$nextTick(() => {
if (this.$refs.discountPopup.showPopup) this.$refs.discountPopup.close();
else this.$refs.discountPopup.open();
this.discountPopupShow = !this.discountPopupShow;
// })
},
/**
* 优惠信息计算
*/
discountCalculate() {
let skuIds = [];
this.manjian = {}
this.cartData.forEach(siteItem => {
siteItem.cartList.forEach(item => {
if (item.checked && item.num) {
skuIds.push({
sku_id: item.sku_id,
num: item.num
});
}
});
});
if (!skuIds.length) {
this.discount = {};
return;
}
this.$api.sendRequest({
url: '/api/cartcalculate/calculate',
data: {
sku_ids: JSON.stringify(skuIds)
},
success: res => {
this.discount = res.data;
if (res.code >= 0 && res.data && (res.data.coupon_money > 0 || res.data
.promotion_money > 0)) {
let manjian = {};
res.data.goods_list.forEach(item => {
if (item.promotion && item.promotion.manjian) {
manjian['sku_' + item.sku_id] = JSON.parse(item.promotion
.manjian
.rule_json);
}
})
Object.assign(this.manjian, manjian);
}
}
})
},
/**
* 领取优惠券
* tips 失败时是否提示
* @param {Object} couponTypeId
*/
receiveCoupon(couponTypeId, tips = true) {
if (this.receiveSub) return;
this.receiveSub = true;
console.log(couponTypeId, `receiveSubreceiveSubreceiveSub`);
this.$api.sendRequest({
url: '/coupon/api/coupon/receive',
data: {
coupon_type_id: couponTypeId,
get_type: 2 //获取方式:1订单2.直接领取3.活动领取
},
success: res => {
console.log(res);
if (res.code == 0) {
this.$set(this.discount.coupon_info, 'receive_type', '');
} else {
if (tips) this.$util.showToast({
title: res.message
});
this.receiveSub = false;
}
this.init();
this.$refs.goodrecommend.isAll = true;
this.$refs.goodrecommend.init();
this.$refs.goodrecommend.getLikeList();
}
});
},
moneyFormat(money) {
if (isNaN(parseFloat(money))) return money;
return parseFloat(money).toFixed(2);
},
refreshSkuDetail(goodsSkuDetail) {
this.goodsSkuDetail = goodsSkuDetail;
}
},
onHide() {
this.isAction = false;
}
}