Browse Source

2024-01-09 新商品购买时限制数量

zhiquz 1 year ago
parent
commit
9fbc943047

+ 3 - 1
apis/good.js

@@ -2,4 +2,6 @@ import request from '@/common/util/request'
 
 export const _API_GoodList = data => request.get('/good_show', data) // 获取商品列别接口
 
-export const GetGoodStatus = data => request.get('/goods/GetGoodStatus', data) //购物车获取客户下单限制是否开启
+export const GetGoodStatus = data => request.get('/goods/GetGoodStatus', data) //购物车获取客户下单限制是否开启
+
+export const GetLimitGood = data => request.get('/good/LimitGood', data) // 新款商品限制数量

+ 1 - 1
common/util/index.js

@@ -1,6 +1,6 @@
 import Vue from 'vue'
 
-export const deepClone = obj => { // 深克隆方法
+export const deepClone = (obj, v) => { // 深克隆方法
 	const targetObj = obj.constructor === Array ? [] : {}
 	Object.keys(obj).forEach(keys => {
 		if (obj[keys] && typeof obj[keys] === 'object') {

+ 3 - 4
components/public/add-cart.vue

@@ -20,11 +20,11 @@
 					<view v-for="(size, index) in item.size" :key="index" class="size-item">
 						<view class="left">
 							<text>{{ size }}</text>
-							<text class="basecolor">¥{{ item.money }}/{{ item.unit }}</text>
-							<!-- <text>库存:{{ item.storage[index] }}</text> -->
+							<text class="basecolor">¥{{ item.money }}/{{ item.unit }}<text v-show="item.name == '红色贺岁款(精装版)'" class="basecolor">{{ `(剩余:${item.storage[index]}${item.unit})` }}</text></text>
+							<!-- <text></text> -->
 						</view>
 						<view v-if="+item.storage[index] > 0">
-							<NumInput :value="item.cart[index]" :args="[item.size_id[index]]" :num="item.step"
+							<NumInput :value="item.cart[index]" :args="[item.size_id[index]]" :num="item.step" :max="item.storage[index]" :name="item.name"
 								@change="numChange" />
 						</view>
 						<view v-else>暂无库存</view>
@@ -75,7 +75,6 @@
 			show(item) {
 				this.showed = true
 				this.item = item
-				console.log(item, 'item')
 			},
 			hide() {
 				this.showed = false

+ 10 - 2
components/public/num-input.vue

@@ -21,6 +21,9 @@
 <script>
 	export default {
 		props: {
+			name: {
+				type: String
+			},
 			value: {
 				type: Number,
 				default: 0
@@ -55,7 +58,9 @@
 				}
 			},
 			add() {
-				if (this.value + 1 > this.max) {
+				if (this.value + 1 > this.max && this.name == '红色贺岁款(精装版)') {
+					uni.toast('数量不能再多了')
+				} else if (this.value + 1 > 9999 && this.name != '红色贺岁款(精装版)') {
 					uni.toast('数量不能再多了')
 				} else {
 					// this.$emit('change', this.value + 1, ...this.args)
@@ -75,9 +80,12 @@
 					return
 				}
 				if (this.inputNum) {
-					if (+this.inputNum > this.max) {
+					if ((+this.inputNum > this.max) && this.name == '红色贺岁款(精装版)') {
 						this.$emit('change', this.max, ...this.args)
 						this.$emit('input', this.max, ...this.args)
+					} else if ((+this.inputNum > 9999) && this.name != '红色贺岁款(精装版)') {
+						this.$emit('change',9999, ...this.args)
+						this.$emit('input', 9999, ...this.args)
 					} else {
 						this.$emit('change', +this.inputNum, ...this.args)
 						this.$emit('input', +this.inputNum, ...this.args)

+ 7 - 7
pages/index-my/index-my.vue

@@ -99,13 +99,13 @@
 			icon: '8.png',
 			url: 'payment/payment'
 		},
-		{
-			name: '礼品兑换',
-			icon: '3.png',
-			url: 'zbs-exchange/exchange',
-			width: 39,
-			height: 37
-		},
+		// {
+		// 	name: '礼品兑换',
+		// 	icon: '3.png',
+		// 	url: 'zbs-exchange/exchange',
+		// 	width: 39,
+		// 	height: 37
+		// },
 		{
 			name: '安全管理',
 			remark: '退出、修改手机号',

+ 28 - 4
pages/place-order/place-order.vue

@@ -33,7 +33,8 @@
 		deepClone
 	} from '@/common/util/index.js'
 	import {
-		_API_GoodList
+		_API_GoodList,
+		GetLimitGood
 	} from '@/apis/good.js'
 	import AddCart from '@/components/public/add-cart.vue'
 	export default {
@@ -57,7 +58,7 @@
 				return this.$store.state.userinfo
 			},
 			list() {
-				const temp = deepClone(this.goodsList)
+				const temp = this.goodsList
 				temp.forEach(goodItem => {
 					goodItem.cart = Array(goodItem.size.length).fill(0)
 					const itemInCart = this.$store.state.cart.list.find(e => e.attr_id === goodItem.attr_id)
@@ -106,8 +107,19 @@
 				})
 				_API_GoodList().then(res => {
 					if (res.code === 200) {
+						// let arr = res.data.list
+						// if (arr.length) {
+						// 	this.goodsList = arr.map(function(item) {
+						// 	  if (item.name == '红色贺岁款(精装版)') {
+						// 	    return {...item, storage: storage}
+						// 	  } else {
+						// 	    return item
+						// 	  }
+						// 	})
+						// } else {
+						// 	this.status = '暂无商品'
+						// }
 						this.goodsList = res.data.list
-						console.log(this.goodsList)
 						if (!this.goodsList.length) {
 							this.status = '暂无商品'
 						}
@@ -127,7 +139,19 @@
 				})
 			},
 			tapShopCar(item) {
-				this.$refs.addCart.show(deepClone(item))
+				// 新款商品剩余数量
+				let storage = [0,0,0,0,0,0,0,0,0,0,0,0,0,0] // 剩余数量
+				GetLimitGood().then(res => {
+					if (res.data.length) {
+						storage = res.data.map(item => (item.limit_num - item.num)) // 剩余数量 = 限制购买数量 - 已购买数量
+					}
+					const arr = this.$store.state.cart.list
+					const i = arr.findIndex(item => item.name == '红色贺岁款(精装版)')
+					if (i != -1) {
+						this.$store.commit('cart/GETNEWGOODSNUM', storage)
+					}
+					this.$refs.addCart.show(deepClone(item))
+				})
 			}
 		}
 	}

+ 29 - 4
pages/shop-car1/shop-car1.vue

@@ -33,8 +33,9 @@
 					<view class="info">
 						<text>尺码:{{ sizeItem }}</text>
 						<text class="basecolor">¥{{ item.money }}/{{ item.unit }} </text>
+						<text class="basecolor" v-show="item.name == '红色贺岁款(精装版)'">剩余:{{ item.storage[sizeIndex] }}{{ item.unit }}</text>
 					</view>
-					<NumInput :value="item.cart[sizeIndex]" :args="[index, sizeIndex]" :num="item.step"
+					<NumInput :value="item.cart[sizeIndex]" :args="[index, sizeIndex]" :num="item.step" :max="item.storage[sizeIndex]" :name="item.name"
 						@change="numChange" />
 				</view>
 			</view>
@@ -84,7 +85,8 @@
 	} from '@/common/util/index.js'
 	import {
 		_API_GoodList,
-		GetGoodStatus
+		GetGoodStatus,
+		GetLimitGood
 	} from '@/apis/good.js'
 	import {
 		_API_GetallGift,
@@ -163,7 +165,7 @@
 			}
 		},
 		onShow() {
-			console.log(this.$store.state.cart.list)
+			this.getLimitNum()
 			const cartinfo = this.$store.state.cart.list
 			for (var i = 0; i < cartinfo.length; i++) {
 				// cartinfo[i].goods_num= sum(cartinfo[i].cart)
@@ -184,7 +186,7 @@
 				cartinfo[i].goods_num = newArr.reduce((p, e) => p + e.num, 0);
 				cartinfo[i].goods_money = cartinfo[i].goods_num * cartinfo[i].money
 
-
+				
 			}
 			console.log(cartinfo)
 			// if (this.$store.getters['cart/shopcarNum']) {
@@ -211,6 +213,20 @@
 			// 		}
 			// 	})
 			// },
+			// 新款商品剩余数量
+			getLimitNum() {
+				let storage = [0,0,0,0,0,0,0,0,0,0,0,0,0,0] // 剩余数量
+				GetLimitGood().then(res => {
+					if (res.data.length) {
+						storage = res.data.map(item => (item.limit_num - item.num)) // 剩余数量 = 限制购买数量 - 已购买数量
+					}
+					const arr = this.$store.state.cart.list
+					const i = arr.findIndex(item => item.name == '红色贺岁款(精装版)')
+					if (i != -1) {
+					    this.$store.commit('cart/GETNEWGOODSNUM', storage)
+					}
+				})
+			},
 			getAllGift() {
 				this.giftList = []
 				_API_GetallGift().then(res => {
@@ -222,9 +238,18 @@
 			numChange(value, index, sizeIndex) { // 购物车数量发生变化
 				if (value) {
 					const cartinfo = this.$store.state.cart.list
+					// const storage = cartinfo[index].storage[sizeIndex]
+					// if (value > storage) {
+					// 	uni.showToast({
+					// 		title: '库存不足!',
+					// 		icon: 'none'
+					// 	})
+					// 	return
+					// }
 					cartinfo[index].cart[sizeIndex] = value
 					cartinfo[index].goods_num = sum(cartinfo[index].cart)
 					cartinfo[index].goods_money = sum(cartinfo[index].cart) * cartinfo[index].money
+					
 					this.$store.commit('cart/COUNTCHANGE', {
 						value,
 						index,

+ 2 - 2
store/modules/app.js

@@ -7,8 +7,8 @@ export default {
 		navtabbarHeight: uni.upx2px(128), // 自定义导航栏高
 		indextabbarHeight: uni.upx2px(98), // 首页 tabbar 高
 		token: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63dNS6ztWaS1-KFbEDcfxD24VskMtL1WOR0',
+		// token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9hcGkuYXBwLmNsaXUuY2NcL2FwaVwvbG9naW5cL2dldF91c2VyX29wZW5pZCIsImlhdCI6MTcwNDc2NTIwNSwiZXhwIjoxNzA1NjI5MjA1LCJuYmYiOjE3MDQ3NjUyMDUsImp0aSI6IjQxenp2R0M4UktvQzhvbE8iLCJzdWIiOjc1NjYwLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.WBdmKKbMBiKgcx1posj_GUu7QZ_kd_RG3gsSyfTRDUY',
 	},
 	mutations: {
 		LOGIN(state, payload) { // 登录 保存 token 

+ 7 - 0
store/modules/cart.js

@@ -154,6 +154,13 @@ export default {
 			// this.commit('cart/COUNTCHANGE', { value: old, index: 0, sizeIndex: 0, del: true })
 			
 		},
+		GETNEWGOODSNUM(state, payload) {
+			state.list.forEach(v => {
+				if (v.name == '红色贺岁款(精装版)') {
+					v.storage = payload
+				}
+			})
+		},
 		DELETE(state, payload) {
 			state.list.splice(payload, 1)
 			storage(state.list)