Parcourir la source

【Add】优惠券去使用以及自动使用功能

liuxiangxin il y a 6 mois
Parent
commit
a57104abe9
3 fichiers modifiés avec 75 ajouts et 30 suppressions
  1. 1 0
      pages/car/index.vue
  2. 36 24
      pages/car/order.vue
  3. 38 6
      pages/product/index.vue

+ 1 - 0
pages/car/index.vue

@@ -386,6 +386,7 @@
 		}
 	}
 	.bottom_box{
+		z-index: 9;
 		left: 0rpx;
 		width: 100%;
 		height: 100rpx;

+ 36 - 24
pages/car/order.vue

@@ -153,20 +153,24 @@
 					// 结果
 					this.$http.request('api/custom_coupon/get_checked',this.requestParam).then((re)=>{
 						if( re.code == 'success' ){
+							// 赋值
+							this.couponList = re.data;
+							// 推荐使用的优惠券
+							let rebatePrice	= 0;
 							// 循环优惠券
-							for (let i in re.data) {
+							for (let i in this.couponList) {
 								/* 商品的总价格,决定是否可用 */
 								var totalPrice	 = 0.00;
 								// 循环产品,
 								for (let j in this.cartList) {
 									// 如果是商品券
-									if( re.data[i].type_id == 1 ){
+									if( this.couponList[i].type_id == 1 ){
 										// 判断商品是不是在商品范围
 										var isScope = false;
 										// 循环商品范围
-										for (let k in re.data[i].product_scope) {
+										for (let k in this.couponList[i].product_scope) {
 											// 如果存在商品范围
-											if( re.data[i].product_scope[k].product_id == this.cartList[j].product_id) isScope = true;
+											if( this.couponList[i].product_scope[k].product_id == this.cartList[j].product_id) isScope = true;
 										}
 										// 范围内的做计算
 										if( isScope ){
@@ -177,12 +181,34 @@
 										// 折扣券以及满减券的话,计算价格
 										totalPrice = this.$decimal.add(totalPrice,this.$decimal.mul(this.cartList[j].price,this.cartList[j].buy_num));
 									}
+									// 判断价格到不到限额
+									this.couponList[i].is_std = parseFloat(totalPrice.toFixed(2))  >= parseFloat(this.$decimal.mul(this.couponList[i].std_pay,1).toFixed(2));
+									// 如果可用的话,并且没有主动选择优惠券
+									if( this.couponList[i].is_std ){
+										// 优惠金额
+										let newRebatePrice	= 0.00;
+										// 计算满减价格
+										if( this.couponList[i].rebate_type == 1 ){
+											// 计算扣减数据
+											newRebatePrice 	= this.$decimal.mul(this.couponList[i].rebate,1);
+										}
+										// 计算折扣价格
+										if( this.couponList[i].rebate_type == 2 ){
+											// 折扣
+											newRebatePrice 	= this.$decimal.mul(totalPrice,this.couponList[i].rebate);
+											// 减数
+											newRebatePrice	= this.$decimal.sub(totalPrice,newRebatePrice.mul(0.1));
+										}
+										// 判断哪个比较优惠
+										if( parseFloat(rebatePrice.toFixed(2)) < parseFloat(newRebatePrice.toFixed(2)) ){
+											// 覆盖价格
+											rebatePrice			= parseFloat(newRebatePrice.toFixed(2));
+											// 选中
+											this.checkedCoupon(i);
+										}
+									}
 								}
-								// 判断价格到不到限额
-								re.data[i].is_std = totalPrice.toFixed() >= this.$decimal.mul(re.data[i].std_pay,1).toFixed();
 							}
-							// 赋值
-							this.couponList = re.data;
 						}
 					});
 					// 计算价格
@@ -191,20 +217,6 @@
 			});
 			// 地址列表
 			this.getAddrList();
-		},
-		onPullDownRefresh() {
-			// 请求列表
-			this.$http.request('api/shop_cart/check_list',this.requestParam).then((re)=>{
-				if( re.code == 'success' ){
-					// 赋值
-					this.cartList = re.data;
-					this.priceHandler();
-				}
-			});
-			uni.stopPullDownRefresh();
-		},
-		onReachBottom() {
-			
 		},
 		methods: {
 			// 地址弹出层
@@ -338,7 +350,7 @@
 						}
 					}
 					// 如果是可以用的话
-					if( parseFloat(totalPrice.toFixed())  >= parseFloat(this.$decimal.mul(this.couponList[i].std_pay,1).toFixed()) ){
+					if( parseFloat(totalPrice.toFixed(2))  >= parseFloat(this.$decimal.mul(this.couponList[i].std_pay,1).toFixed(2)) ){
 						// 扣减金额
 						let rebatePrice = 0;
 						// 满减
@@ -445,7 +457,6 @@
 		overflow: hidden;
 		margin: 0rpx auto;
 		margin-top: 20rpx;
-		margin-bottom: 160rpx;
 		background: #FFFFFF;
 		.car_item{
 			height: 170rpx;
@@ -533,6 +544,7 @@
 	}
 	.bottom_box{
 		left: 0rpx;
+		z-index: 9;
 		width: 100%;
 		height: 140rpx;
 		display: block;

+ 38 - 6
pages/product/index.vue

@@ -109,7 +109,7 @@
 		<uni-popup ref="couponPopup" type="bottom" class="popup" background-color="#FFFFFF" >
 			<view class="popup_title">优惠券</view>
 			<view class="coupon_list">
-				<view class="coupon_item" v-for="(item,index) in couponList" @click="checkedCoupon(index)" :key="index">
+				<view class="coupon_item" v-for="(item,index) in couponList" @click="checkedCoupon(index,false)" :key="index">
 					<view class="box_left">
 						<view class="rebate" v-if="item.rebate_type == 1" > ¥{{item.rebate}}</view>
 						<view class="rebate" v-if="item.rebate_type == 2" > 打 {{item.rebate}} 折</view>
@@ -347,7 +347,7 @@
 				// 如果不是0.表示两侧按钮点击,0表示输入的修改
 				if( number != 0 ) {
 					// 计算个数
-					this.quantity 		= this.quantity + number;
+					this.quantity 		= parseInt(this.quantity) + parseInt(number);
 				}
 				// 如果大于库存设置为库存
 				if( this.quantity > this.productInfo.stock ) {
@@ -497,6 +497,8 @@
 			checkCoupon(){
 				// 如果存在的话
 				if( this.couponList.length ){
+					// 推荐使用的优惠券
+					let oldrebatePrice	= 0.00;
 					let hasIsStd = false;
 					// 循环优惠券
 					for (let i in this.couponList) {
@@ -525,14 +527,44 @@
 						// 判断价格到不到限额
 						this.couponList[i].is_std	= parseFloat(totalPrice.toFixed()) >= parseFloat(this.$decimal.mul(this.couponList[i].std_pay,1).toFixed());
 						// 如果不可用的话,该项已选择则不选
-						if( !this.couponList[i].is_std && this.couponList[i].checked )	this.checkedCoupon(i);
+						if( !this.couponList[i].is_std && this.couponList[i].checked )	this.checkedCoupon(i,false);
+						// 如果可用的话,并且优惠券没有选中
+						if( this.couponList[i].is_std ){
+							// 优惠金额
+							let newRebatePrice	= 0.00;
+							// 计算满减价格
+							if( this.couponList[i].rebate_type == 1 ){
+								// 计算扣减数据
+								newRebatePrice 	= this.$decimal.mul(this.couponList[i].rebate,1);
+							}
+							// 计算折扣价格
+							if( this.couponList[i].rebate_type == 2 ){
+								// 折扣
+								newRebatePrice 	= this.$decimal.mul(totalPrice,this.couponList[i].rebate);
+								// 减数
+								newRebatePrice	= this.$decimal.sub(totalPrice,newRebatePrice.mul(0.1));
+							}
+							// 判断哪个比较优惠
+							if( parseFloat(oldrebatePrice.toFixed(2)) <= parseFloat(newRebatePrice.toFixed(2)) ){
+								// 覆盖价格
+								oldrebatePrice			= parseFloat(newRebatePrice.toFixed(2));
+								// 如果选中的是自己,不需要取消
+								if( this.customCoupon == this.couponList[i].id  && this.couponList[i].checked ){
+									// 选中
+									this.checkedCoupon(i,true);
+								}else{
+									// 选中
+									this.checkedCoupon(i,false);
+								}
+							}
+						}
 					}
 				}
 			},
 			// 选择优惠券
-			checkedCoupon(index){
-				// 单个设置选中/未选
-				this.couponList[index].checked = this.couponList[index].checked ? 0 : 1;
+			checkedCoupon(index,save_checked=false){
+				// 单个设置选中/未选(如果保持选中,则不变)
+				this.couponList[index].checked = save_checked ? this.couponList[index].checked : (this.couponList[index].checked ? 0 : 1);
 				// 循环处理
 				for (let i in this.couponList) {
 					// 有未选的就不做全选