Browse Source

bug 修复

lilei 4 years ago
parent
commit
1563ff842c

+ 1 - 6
components/uni-cart-fix/uni-cart-fix.vue

@@ -31,12 +31,7 @@
 			},
 			computed: {
 				count() {
-					let arr = this.$store.state.vuex_cartList || []
-					let count = 0
-					arr.map(item=>{
-						count = count + item.shoppingCartGoodsList.length
-					})
-					return count
+					return this.$store.state.vuex_cartNums
 				},
 			},
 			methods: {

+ 28 - 16
components/uni-coods/uni-coods.vue

@@ -30,6 +30,9 @@
 </template>
 
 <script>
+	import {
+		addGoodsToCart
+	} from '@/api/shoppingCart.js'
 	export default {
 		props: {
 			imgHeight: {
@@ -66,27 +69,36 @@
 			}
 		},
 		methods: {
-			// 判断当前商品库存是否够
-			hasZgKucun(data){
+			// 添加购物车
+			addGoodToCart(item){
+				uni.showLoading({
+					mask: true,
+					title: "加入购物车中..."
+				})
+				addGoodsToCart(item).then(res => {
+					if(res.status == 200){
+						// 刷新购物车
+						if(!item.hasCunzai){
+							this.$store.state.vuex_cartNums = this.$store.state.vuex_cartNums + 1
+						}
+						uni.showToast({
+							icon: 'none',
+							title:'加入购物车成功'
+						})
+					}else{
+						uni.hideLoading()
+					}
+				})
+			},
+			// 判断当前商品是否已存在购物车中
+			hasCunzai(data){
 				let list = this.$store.state.vuex_cartList
 				let row = list.find(item=> item.goodsTypeNo == data.goodsTypeNo)
-				if(row){
-					let good = row.shoppingCartGoodsList.find(item => item.goodsNo == data.goodsNo)
-					return !good ? true : good.buyQty < data.inventoryQty
-				}else{
-					return true
-				}
+				return row
 			},
 			// 加入购物车
 			addCart(item) {
-				if(this.hasZgKucun(item)){
-					uni.$emit("addCart",{goodsNo:item.goodsNo,buyQty:1,time:new Date().getTime()})
-				}else{
-					uni.showToast({
-						icon: 'none',
-						title: '此商品库存不足'
-					})
-				}
+				this.addGoodToCart({goodsNo:item.goodsNo,buyQty:1,hasCunzai:this.hasCunzai(item)})
 			},
 			// 商品详情
 			toDetail(item){

+ 51 - 17
pages/cart/cart.vue

@@ -118,6 +118,11 @@
 	</view>
 </template>
 <script>
+	import {
+		getCartList,
+		deleteGoodsFormCart,
+		updateGoodsBuyQty
+	} from '@/api/shoppingCart.js'
 	export default {
 		data() {
 			return {
@@ -136,6 +141,7 @@
 			};
 		},
 		computed: {
+			// 用户信息
 			userData(){
 				return this.$store.state.vuex_userData
 			},
@@ -173,24 +179,28 @@
 				return total
 			},
 		},
-		onUnload() {
-			uni.$off('getCarListSuccess')
-		},
 		onLoad() {
+			// 获取购物车数据
 			this.loading = true
-			uni.$emit('getCartList')
-			// 刷新列表成功
-			uni.$on('getCarListSuccess',item => {
-				this.cartList = JSON.parse(JSON.stringify(this.$store.state.vuex_cartList))
-				this.loading = false
-				console.log(this.isEdit,'this.isEdit')
-				// 默认全选
-				if(!this.isEdit){
-					this.checkAllChange()
-				}
-			})
+			this.getCartList()
 		},
 		methods: {
+			// 查询购物车
+			getCartList(){
+				getCartList({}).then(res => {
+					if(res.status == 200){
+						this.$u.vuex('vuex_cartList', res.data)
+						this.cartList = JSON.parse(JSON.stringify(res.data))
+						uni.$emit('getCarCount')
+						// 默认全选
+						if(!this.isEdit){
+							this.checkAllChange()
+						}
+					}
+					this.loading = false
+				})
+			},
+			// 去选商品
 			toBuy(){
 				uni.switchTab({
 					url:"/pages/goods/index"
@@ -320,7 +330,7 @@
 						if(a.length==0){
 							this.$u.vuex("vuex_toOrderList",goods)
 							uni.redirectTo({
-								url:"/pages/toOrder/index"
+								url:"/pages/toOrder/index?orderForm=SHOPPING_CAR"
 							})
 						}else{
 							this.showMsg = true
@@ -339,11 +349,25 @@
 					})
 				}
 			},
+			// 删除购物车商品
+			delGoodFormCart(ids){
+				uni.showLoading({
+					mask: true,
+					title: "正在删除..."
+				})
+				deleteGoodsFormCart({idList:ids}).then(res => {
+					if(res.status == 200){
+						// 刷新购物车
+						this.getCartList()
+					}
+					uni.hideLoading()
+				})
+			},
 			// 删除商品
 			delGoods(){
 				let ids = this.getCheckGoods(0)
 				if(ids.length){
-					uni.$emit("delCartGood", ids)
+					this.delGoodFormCart(ids)
 				}else{
 					uni.showToast({
 						title:"请选择要删除的商品",
@@ -351,6 +375,16 @@
 					})
 				}
 			},
+			// 更新商品数量
+			updateGoodsBuyQty(item){
+				uni.showLoading({
+					mask: true,
+					title: "更新数量中..."
+				})
+				updateGoodsBuyQty(item).then(res => {
+					uni.hideLoading()
+				})
+			},
 			// 更改商品数量
 			goodsNumsChange(val,id){
 				console.log(val,id)
@@ -359,7 +393,7 @@
 				let item = this.cartList[cindex]
 				let row = item.shoppingCartGoodsList[index]
 				row.buyQty = val
-				uni.$emit("updateGoodsBuyQty",{id:row.id,buyQty:row.buyQty})
+				this.updateGoodsBuyQty({id:row.id,buyQty:row.buyQty})
 			}
 		},
 	}

+ 0 - 3
pages/goods/goods.vue

@@ -37,9 +37,6 @@
 			})
 			this.getGoods()
 		},
-		onUnload() {
-			uni.$off('addCart')
-		},
 		// 下拉刷新
 		onPullDownRefresh() {
 			console.log('refresh')

+ 41 - 8
pages/goods/goodsDetail.vue

@@ -60,6 +60,9 @@
 	import {
 		getGoodsDetail
 	} from '@/api/goods.js'
+	import {
+		addGoodsToCart
+	} from '@/api/shoppingCart.js'
 	import jyfParser from "@/components/jyf-parser/jyf-parser";
 	export default {
 		components: {
@@ -85,12 +88,8 @@
 		onLoad(opts) {
 			console.log(opts)
 			this.id = opts.id
-			this.goldLimit = opts.goldLimit
 			this.getDetail()
 		},
-		onUnload() {
-			uni.$off('addCart')
-		},
 		methods: {
 			// 查详情
 			getDetail(){
@@ -98,6 +97,7 @@
 					console.log(res)
 					if(res.status == 200){
 						this.goodContent = res.data
+						this.goldLimit = res.data.goodsType.goldLimit
 						let arr=res.data.images.split(',')
 						arr.map(item => {
 							this.imageList.push({image:item})
@@ -105,11 +105,44 @@
 					}
 				})
 			},
+			// 添加购物车
+			addGoodToCart(item){
+				uni.showLoading({
+					mask: true,
+					title: "加入购物车中..."
+				})
+				addGoodsToCart(item).then(res => {
+					if(res.status == 200){
+						// 刷新购物车
+						if(!item.hasCunzai){
+							this.$store.state.vuex_cartNums = this.$store.state.vuex_cartNums + 1
+						}
+						uni.showToast({
+							icon: 'none',
+							title:'加入购物车成功'
+						})
+					}else{
+						uni.hideLoading()
+					}
+				})
+			},
+			// 判断当前商品是否已存在购物车中
+			hasCunzai(data){
+				let list = this.$store.state.vuex_cartList
+				let row = list.find(item=> item.goodsTypeNo == data.goodsTypeNo)
+				return row
+			},
 			// 加入购物车
-			addCart(){
-				let _this = this
+			addCart() {
 				let item = this.goodContent
-				uni.$emit("addCart",{goodsNo:item.goodsNo,buyQty:_this.nums})
+				if(this.nums * item.sellGold >= this.goldLimit){
+					this.addGoodToCart({goodsNo:item.goodsNo,buyQty:this.nums,hasCunzai:this.hasCunzai(item)})
+				}else{
+					uni.showToast({
+						icon: 'none',
+						title: `此商品需满${this.goldLimit}乐豆才能购买`
+					})
+				}
 			},
 			// 立即下单
 			toOrder(){
@@ -117,7 +150,7 @@
 				if(this.goodContent.sellGold * this.nums >= this.goldLimit){
 					this.$u.vuex("vuex_toOrderList",[{id:this.id,buyQty:this.nums,goodsNo:this.goodContent.goodsNo,goods:this.goodContent}])
 					uni.redirectTo({
-						url:"/pages/toOrder/index"
+						url:"/pages/toOrder/index?orderForm=ORDER_NOW"
 					})
 				}else{
 					uni.showToast({

+ 0 - 3
pages/goods/index.vue

@@ -42,9 +42,6 @@
 		onLoad() {
 			this.pageInit()
 		},
-		onUnload() {
-			uni.$off('addCart')
-		},
 		// 下拉刷新
 		onPullDownRefresh() {
 			console.log('refresh')

+ 11 - 52
pages/index/index.vue

@@ -89,17 +89,9 @@
 			uni.$on("getCartList",item =>{
 				this.getCartList()
 			})
-			// 加入购物车
-			uni.$on("addCart",item =>{
-				this.addGoodToCart(item)
-			})
-			// 删除购物车的商品
-			uni.$on("delCartGood",item => {
-				this.delGoodFormCart(item)
-			})
-			// 更新购物车商品数量
-			uni.$on("updateGoodsBuyQty",item=>{
-				this.updateGoodsBuyQty(item)
+			// 更新购物车数量
+			uni.$on('getCarCount',item => {
+				this.getCarCount()
 			})
 			// 查询用户信息
 			uni.$on("getUserInfo",item=>{
@@ -192,54 +184,21 @@
 			// 查询购物车
 			getCartList(){
 				getCartList({}).then(res => {
-					console.log(res,'getCartList----')
 					if(res.status == 200){
 						this.$u.vuex('vuex_cartList', res.data)
-						uni.$emit('getCarListSuccess')
+						this.getCarCount()
 					}
 					uni.hideLoading()
 				})
 			},
-			// 添加购物车
-			addGoodToCart(item){
-				uni.showLoading({
-					mask: true,
-					title: "加入购物车中..."
-				})
-				addGoodsToCart(item).then(res => {
-					if(res.status == 200){
-						// 刷新购物车
-						this.getCartList()
-						uni.hideLoading()
-					}else{
-						uni.hideLoading()
-					}
-				})
-			},
-			// 删除购物车
-			delGoodFormCart(ids){
-				uni.showLoading({
-					mask: true,
-					title: "删除中..."
-				})
-				deleteGoodsFormCart({idList:ids}).then(res => {
-					if(res.status == 200){
-						// 刷新购物车
-						this.getCartList()
-					}else{
-						uni.hideLoading()
-					}
-				})
-			},
-			// 更新商品数量
-			updateGoodsBuyQty(item){
-				uni.showLoading({
-					mask: true,
-					title: "更新数量中..."
-				})
-				updateGoodsBuyQty(item).then(res => {
-					uni.hideLoading()
+			// 计算购物车数量
+			getCarCount(){
+				let arr = this.$store.state.vuex_cartList || []
+				let count = 0
+				arr.map(item=>{
+					count = count + item.shoppingCartGoodsList.length
 				})
+				this.$u.vuex('vuex_cartNums', count)
 			},
 			// 更多商品
 			toGoods(clsId){

+ 5 - 3
pages/toOrder/index.vue

@@ -123,15 +123,16 @@
 				showSetPswModal: false,  // 设置支付密码弹窗
 				showInputPsw: false, // 打开输入密码弹窗
 				showLeavePsw: false, // 打开确定放弃弹窗
-				payBtn: false
+				payBtn: false,
+				orderForm: '' // 商品下单来源,商品详情或购物车
 			};
 		},
 		onShow() {
 			this.getLocation()
 		},
-		onLoad() {
+		onLoad(opts) {
+			this.orderForm = opts.orderForm
 			this.goodsList = this.$store.state.vuex_toOrderList
-			// console.log(this.goodsList,'llllllllllll')
 			// 监听设置密码是否成功
 			uni.$once('setPswSuccess', this.setPsw)
 		},
@@ -251,6 +252,7 @@
 						receiverName: this.userInfo.receiverName,
 						receiverPhone: this.userInfo.receiverPhone,
 						orderGoodsList: orderGoodsList,
+						orderForm: this.orderForm
 					}
 					saveOrder(params).then(res=>{
 						console.log(res,'rrrrrrr')

+ 2 - 2
store/index.js

@@ -19,8 +19,7 @@ let saveStateKeys = [
 	'vuex_storeId',
 	'vuex_bizId',
 	'vuex_orderNo',
-	'vuex_orderStatus',
-	'vuex_cartNums'
+	'vuex_orderStatus'
 ];
 
 // 保存变量到本地存储中
@@ -105,6 +104,7 @@ const store = new Vuex.Store({
 		vuex_orderStatus: lifeData.vuex_orderStatus ? lifeData.vuex_orderStatus : '',// 订单状态
 		// 购物车及下单
 		vuex_cartList: [],
+		vuex_cartNums: 0,
 		vuex_OrderAddress: {}, // 地址
 		vuex_toOrderList: [],
 		// 如果无需保存到本地永久存储,无需lifeData.xx方式