Selaa lähdekoodia

feat: 商品管理规格输入增加格式限制;

ext.zhangbin71 4 viikkoa sitten
vanhempi
commit
7e908dc3d2
2 muutettua tiedostoa jossa 94 lisäystä ja 31 poistoa
  1. 2 2
      config/app.js
  2. 92 29
      pages/merchantCenters/releaseProduct.vue

+ 2 - 2
config/app.js

@@ -1,6 +1,6 @@
 // let domain = "https://www.shuibeibyg.com/front-api"; // 正式环境IP
-// let domain = "https://test.shuibeibyg.com/front-api"; // 测试环境IP
-let domain = 'http://192.168.100.199:8081' // 晋守桦IP
+let domain = "https://test.shuibeibyg.com/front-api"; // 测试环境IP
+// let domain = 'http://192.168.100.199:8081' // 晋守桦IP
 // let domain = 'http://192.168.100.246:8081' // 韩朝龙IP
 let share = "https://www.shuibeibyg.com";
 

+ 92 - 29
pages/merchantCenters/releaseProduct.vue

@@ -121,13 +121,16 @@
 							</view>
 							<view class="cate-ipt-line">
 								<up-input class="cate-ipt" style="margin-right: 16rpx;" v-model="oneTemplate.price"
-									placeholder="请输入工费" inputAlign="left" border="none" type="digit" labelWidth="">
+									placeholder="请输入工费" inputAlign="left"
+									@blur="keyupEvent('price', oneTemplate.price, 0, 1)" border="none" type="digit"
+									labelWidth="">
 									<template #suffix>
 										<text class="unit">元/g</text>
 									</template>
 								</up-input>
 								<up-input class="cate-ipt" v-model="oneTemplate.stock" placeholder="请输入库存"
-									inputAlign="left" border="none" type="number" labelWidth="">
+									inputAlign="left" @blur="keyupEvent('stock', oneTemplate.stock, 0, 1)" border="none"
+									type="number" labelWidth="">
 									<template #suffix>
 										<text class="unit">件</text>
 									</template>
@@ -135,13 +138,17 @@
 							</view>
 							<view class="cate-ipt-line">
 								<up-input class="cate-ipt" style="margin-right: 16rpx;" v-model="oneTemplate.weight"
-									placeholder="请输入重量" inputAlign="left" border="none" type="digit" labelWidth="">
+									placeholder="请输入重量" inputAlign="left"
+									@blur="keyupEvent('weight', oneTemplate.weight, 0, 1)" border="none" type="digit"
+									labelWidth="">
 									<template #suffix>
 										<text class="unit">g</text>
 									</template>
 								</up-input>
 								<up-input class="cate-ipt" v-model="oneTemplate.additionalAmount" placeholder="请输入附加金额"
-									inputAlign="left" border="none" type="number" labelWidth="">
+									inputAlign="left"
+									@blur="keyupEvent('additionalAmount', oneTemplate.additionalAmount, 0, 1)"
+									border="none" type="number" labelWidth="">
 									<template #suffix>
 										<text class="unit">元</text>
 									</template>
@@ -149,7 +156,8 @@
 							</view>
 							<view class="cate-ipt-line" style="padding: 0;">
 								<up-input class="cate-ipt" v-model="oneTemplate.barCode" placeholder="请输入商品编号"
-									inputAlign="left" border="none" type="digit" labelWidth="">
+									inputAlign="left" @blur="keyupEvent('barCode', oneTemplate.barCode, 0, 1)"
+									border="none" type="digit" labelWidth="">
 								</up-input>
 							</view>
 						</view>
@@ -180,13 +188,16 @@
 							</view>
 							<view class="cate-ipt-line">
 								<up-input class="cate-ipt" style="margin-right: 16rpx;" v-model="item.price"
-									placeholder="请输入工费" inputAlign="left" border="none" type="digit" labelWidth="">
+									placeholder="请输入工费" inputAlign="left"
+									@blur="keyupEvent('price', item.price, index, 2)" border="none" type="digit"
+									labelWidth="">
 									<template #suffix>
 										<text class="unit">元/g</text>
 									</template>
 								</up-input>
 								<up-input class="cate-ipt" v-model="item.stock" placeholder="请输入库存" inputAlign="left"
-									border="none" type="number" labelWidth="">
+									@blur="keyupEvent('stock', item.stock, index, 2)" border="none" type="number"
+									labelWidth="">
 									<template #suffix>
 										<text class="unit">件</text>
 									</template>
@@ -194,13 +205,17 @@
 							</view>
 							<view class="cate-ipt-line">
 								<up-input class="cate-ipt" style="margin-right: 16rpx;" v-model="item.weight"
-									placeholder="请输入重量" inputAlign="left" border="none" type="digit" labelWidth="">
+									placeholder="请输入重量" inputAlign="left"
+									@blur="keyupEvent('weight', item.weight, index, 2)" border="none" type="digit"
+									labelWidth="">
 									<template #suffix>
 										<text class="unit">g</text>
 									</template>
 								</up-input>
 								<up-input class="cate-ipt" v-model="item.additionalAmount" placeholder="请输入附加金额"
-									inputAlign="left" border="none" type="number" labelWidth="">
+									inputAlign="left"
+									@blur="keyupEvent('additionalAmount', item.additionalAmount, index, 2)"
+									border="none" type="number" labelWidth="">
 									<template #suffix>
 										<text class="unit">元</text>
 									</template>
@@ -208,7 +223,8 @@
 							</view>
 							<view class="cate-ipt-line" style="padding: 0;">
 								<up-input class="cate-ipt" v-model="item.barCode" placeholder="请输入商品编号"
-									inputAlign="left" border="none" type="digit" labelWidth="">
+									inputAlign="left" @blur="keyupEvent('barCode', item.barCode, index, 2)"
+									border="none" type="digit" labelWidth="">
 								</up-input>
 							</view>
 						</view>
@@ -1031,8 +1047,8 @@
 			return false;
 		}
 	};
-	
-	const validateAttr = (item)=>{
+
+	const validateAttr = (item) => {
 		if (!item.image) {
 			uni.showToast({
 				title: '请上传商品规格图片',
@@ -1040,7 +1056,7 @@
 			});
 			return false;
 		}
-		
+
 		// 验证数字字段
 		if (!/^\d+(\.\d+)?$/.test(item.weight)) {
 			uni.showToast({
@@ -1049,7 +1065,7 @@
 			});
 			return false;
 		}
-		
+
 		if (!/^\d+(\.\d+)?$/.test(item.price)) {
 			uni.showToast({
 				title: '工费格式不正确',
@@ -1057,7 +1073,7 @@
 			});
 			return false;
 		}
-		
+
 		if (!/^\d+(\.\d+)?$/.test(item.additionalAmount)) {
 			uni.showToast({
 				title: '附加费格式不正确',
@@ -1065,7 +1081,7 @@
 			});
 			return false;
 		}
-		
+
 		if (!/^\d+$/.test(item.stock)) {
 			uni.showToast({
 				title: '库存必须是整数',
@@ -1080,7 +1096,7 @@
 			});
 			return false;
 		}
-		
+
 		return true
 	}
 	const submitForm = async () => {
@@ -1106,14 +1122,14 @@
 			submitData.specType = true;
 			submitData.isSub = false;
 			// 规格长度为0
-			console.log(attr.value,attrTable.value)
+			console.log(attr.value, attrTable.value)
 			if (!attr.value || attr.value.length <= 0 || !attrTable.value || attrTable.value.length <= 0) {
 				return Toast({
 					title: "请添加商品规格!"
 				});
 			}
 
-			const pass = true;
+			let pass = true;
 			submitData.attr = attr.value.map(item => {
 				return {
 					attrName: item.attrName,
@@ -1121,22 +1137,23 @@
 				}
 			})
 			submitData.attrValue = attrTable.value.map(item => {
-				
-				if(!validateAttr(item)){
+
+				if (!validateAttr(item)) {
 					pass = false;
 				}
-				
+
 				console.log(typeof(item.attrValue))
 				return {
 					...item,
 					id: 0,
 					productId: 0,
-					attrValue: typeof(item.attrValue)=='object'?JSON.stringify(item.attrValue):item.attrValue,
+					attrValue: typeof(item.attrValue) == 'object' ? JSON.stringify(item.attrValue) : item
+						.attrValue,
 					image: item.image || item.imgs[0]?.info?.url || item.imgs[0]?.url
 				}
 			})
 			console.dir(submitData)
-			if(!pass) return false;
+			if (!pass) return false;
 			// return false;
 			// submitData.attr = [{
 			// 	"attrName": "规格",
@@ -1171,14 +1188,14 @@
 				});
 			}
 			// 编辑时,返回上个页面
-			if(!!productId.value && !isProductCenter.value){
+			if (!!productId.value && !isProductCenter.value) {
 				uni.navigateBack()
-			}else{
+			} else {
 				uni.redirectTo({
 					url: '/pages/merchantCenters/productManagement'
 				})
 			}
-			
+
 		}
 	}
 	// 重置表单(用于新建场景)
@@ -1233,7 +1250,7 @@
 	}
 
 	const setAllAttr = () => {
-		if(!validateAttr(oneTemplate.value)) return false;
+		if (!validateAttr(oneTemplate.value)) return false;
 		uni.showModal({
 			title: '提示',
 			content: '所有商品规格将修改为当前配置',
@@ -1366,6 +1383,52 @@
 			return result;
 		}
 	}
+	// 校验输入框不能输入0,保留2位小数,库存为正整数
+	const keyupEvent = (key, val, index, num) => {
+		// console.log(key, val, index, num)
+		if (key === "barCode") return;
+		var re = /^\D*([0-9]\d*\.?\d{0,2})?.*$/;
+		switch (num) {
+			case 1:
+				if (Number(val) === 0) {
+					oneTemplate.value[key] = [
+							"stock",
+							"cost",
+							"otPrice",
+							"price",
+							"additionalAmount"
+						].includes(key) ?
+						0 :
+						0.01;
+				} else {
+					oneTemplate.value[key] =
+						key === "stock" ?
+						parseInt(val) :
+						val.toString().replace(re, "$1")
+				}
+				break;
+			case 2:
+				if (Number(val) === 0) {
+					attrTable.value[index][key] = [
+							"stock",
+							"cost",
+							"otPrice",
+							"price",
+							"additionalAmount"
+						].includes(key) ?
+						0 :
+						0.01;
+				} else {
+					attrTable.value[index][key] =
+						key === "stock" ?
+						parseInt(val) :
+						val.toString().replace(re, "$1")
+				}
+				break;
+			default:
+				break
+		}
+	}
 </script>
 
 <style scoped lang="scss">
@@ -1621,7 +1684,7 @@
 				border-radius: 16rpx;
 				padding: 16rpx;
 				margin-bottom: 16rpx;
-				
+
 				.upload-btn {
 					display: flex;
 					flex-direction: column;