商品多规格

页面效果

 

起始数据格式

attribute: [
				{
					name: '颜色',
					value: ['绿色', '黄色'],
					title: '',
					show: false
				},
				{
					name: '大小',
					value: ['大', '中'],
					title: '',
					show: false
				}
			],

要的数据格式

[
    {
        "createTime":"2023-05-27 14:39:31",
        "updateTime":"2023-05-27 14:39:31",
        "id":6339,
        "goodsId":211,
        "specificationName":"{\"颜色\":\"绿色\",\"大小\":\"大\"}",
        "specificationImages":"80",
        "scribePrice":100,
        "dealPrice":50,
        "stockNum":45,
        "specificationNumber":857,
        "position":"头部"
    },
    {
        "createTime":"2023-05-27 14:39:31",
        "updateTime":"2023-05-27 14:39:31",
        "id":6340,
        "goodsId":211,
        "specificationName":"{\"颜色\":\"绿色\",\"大小\":\"中\"}",
        "specificationImages":"80",
        "scribePrice":100,
        "dealPrice":50,
        "stockNum":45,
        "specificationNumber":857,
        "position":"头部"
    },
    {
        "createTime":"2023-05-27 14:39:31",
        "updateTime":"2023-05-27 14:39:31",
        "id":6341,
        "goodsId":211,
        "specificationName":"{\"颜色\":\"黄色\",\"大小\":\"大\"}",
        "specificationImages":"80",
        "scribePrice":100,
        "dealPrice":50,
        "stockNum":45,
        "specificationNumber":857,
        "position":"头部"
    },
    {
        "createTime":"2023-05-27 14:39:31",
        "updateTime":"2023-05-27 14:39:31",
        "id":6342,
        "goodsId":211,
        "specificationName":"{\"颜色\":\"黄色\",\"大小\":\"中\"}",
        "specificationImages":"80",
        "scribePrice":100,
        "dealPrice":50,
        "stockNum":45,
        "specificationNumber":857,
        "position":"头部"
    }
]

完整代码

<template>
	<view>
		<view class="title">
			<view class="title-left">产品规格设置</view>
			<view class="button" @tap="addAttribute()">添加规格项目</view>
		</view>
		<view class="guige" v-for="(item, index) in attribute">
			<view class="guige-name">
				<u--input placeholder="请输入规格名" border="surround" v-model="item.name"></u--input>
				<!-- <view class="btn" @tap="addName"><u-icon name="checkbox-mark" color="#2979ff" size="28"></u-icon></view> -->
			</view>
			<view class="guige-value">
				<view class="tagList">
					<u-tag v-for="tag in item.value" :text="tag" type="success" plain size="large" closable @close="close(tag, index)" :customStyle="{ width: '50rpx' }"></u-tag>
				</view>
				<view class="inputValue">
					<u--input placeholder="请输入规格值" border="surround" :customStyle="{ borderRight: 'none' }" v-model="item.title"></u--input>
					<view class="btn" @tap="addValue(index)"><u-icon name="checkbox-mark" color="#2979ff" size="28"></u-icon></view>
				</view>
			</view>
			<view class="del" @tap="delAttribute(index)"><u-icon name="trash" color="red" size="28"></u-icon></view>
		</view>
		<view class="box" v-for="item in tableData">
			<view class="box-name" style="">{{ item.specificationName }}</view>
			<view class="box-title" style="">
				<view class="btn">成交价:</view>
				<u--input placeholder="" border="surround" v-model="item.dealPrice" :customStyle="{ borderLeft: 'none' }"></u--input>
			</view>
			<view class="box-title" style="">
				<view class="btn">划线价:</view>
				<u--input placeholder="" border="surround" v-model="item.scribePrice" :customStyle="{ borderLeft: 'none' }"></u--input>
			</view>
			<view class="box-title" style="">
				<view class="btn">库存:</view>
				<u--input placeholder="" border="surround" v-model="item.stockNum" :customStyle="{ borderLeft: 'none' }"></u--input>
			</view>
			<view class="box-title" style="">
				<view class="btn">规格编号:</view>
				<u--input placeholder="" border="surround" v-model="item.specificationNumber" :customStyle="{ borderLeft: 'none' }"></u--input>
			</view>
			<view class="box-title" style="">
				<view class="btn">库存位置:</view>
				<u--input placeholder="" border="surround" v-model="item.position" :customStyle="{ borderLeft: 'none' }"></u--input>
			</view>
			<view class="box-title" style="">
				<view class="btn">图片:</view>
				<u--input placeholder="" border="surround" v-model="item.specificationImages" :customStyle="{ borderLeft: 'none' }"></u--input>
			</view>
		</view>
		<view class="footer" v-if="tableData.length"><view class="footer-c" @tap="submit()">提交</view></view>
	</view>
</template>

<script>
import { insertPlugsGoodsAttributeKey, insertPlugsGoodsAttributeValue, insertPlugsGoodsSpecification, searchPlugsGoodsSpecificationByGoodsId } from '../../../../utils/api.js';
export default {
	data() {
		return {
			close3: true,
			attribute: [
				// {
				// 	name: '颜色',
				// 	value: ['绿色', '黄色'],
				// 	title: '',
				// 	show: false
				// },
				// {
				// 	name: '大小',
				// 	value: ['大', '中'],
				// 	title: '',
				// 	show: false
				// }
			],
			tableData: [],
			goodsId: ''
		};
	},
	onLoad(option) {
		console.log(option, 444);
		this.goodsId = option.id;
	},
	onShow() {
		this.getguige();
	},

	methods: {
		getguige() {
			let data = {
				goodsId: this.goodsId
			};
			searchPlugsGoodsSpecificationByGoodsId(data).then(res => {
				if (res.code == 200) {
					let a = Object.keys(res.body.specificationInfo);
					let arr = [];
					for (let item of res.body.specificationInfo) {
						let obj1 = {
							name: Object.keys(item)[0],
							value: [],
							title: '',
							show: false,
							children: Object.values(item)[0]
						};
						console.log(item, 456);
						arr.push(obj1);
					}
					arr.forEach(item=>{
						console.log(item.children,7777);
						item.children.forEach(ress=>{
							console.log(ress.attributeValue);
							item.value.push(ress.attributeValue)
						})
					})
					console.log(arr,789);
					this.attribute=arr
					this.tableData=res.body.plugsGoodsSpecificationList
				}
			});
		},

		addAttribute() {
			let obj = {
				name: '',
				value: [],
				title: '',
				show: false
			};
			this.attribute.push(obj);
		},
		addValue(index) {
			if (this.attribute[index].name == '') {
				console.log(444);
				uni.showToast({
					icon: 'none',
					title: '规格名不能为空'
				});
				return;
			}
			if (this.attribute[index].title == '') {
				console.log(444);
				uni.showToast({
					icon: 'none',
					title: '规格值不能为空'
				});
				return;
			}
			this.attribute[index].value.push(this.attribute[index].title);
			this.attribute[index].title = '';
			console.log(this.attribute[index].title);
			console.log(this.attribute[index]);

			this.getTbable();
		},
		delAttribute(index) {
			this.attribute.splice(index, 1);
			this.getTbable();
		},
		getTbable() {
			let obj = {};
			for (let sum = 0; sum < this.attribute.length; sum++) {
				obj[this.attribute[sum].name] = this.attribute[sum].value;
			}
			this.tableData = this.permutation(obj);
			let arr = [];
			this.tableData.forEach((item, index) => {
				console.log(JSON.stringify(item));
				let name = {
					specificationName: ''
				};
				name.specificationName = JSON.stringify(item);
				arr.push(name);
			});

			arr.forEach((item, index) => {
				item.scribePrice = 100;
				item.dealPrice = 50;
				item.stockNum = 45;
				item.specificationNumber = '857';
				item.specificationImages = 80;
				item.position = '头部';
				item.goodsId = this.goodsId;
			});
			this.tableData = arr;
		},
		close(tag, index) {
			console.log(tag, index);
			this.attribute[index].value.splice(this.attribute[index].value.indexOf(tag), 1);
			this.getTbable();
		},

		// 数据组合方法
		permutation(source) {
			const result = [];
			const _result = {};
			const convert = (arr, index) => {
				for (let i = 0; i < source[arr[index]].length; i++) {
					if (source[arr[index]][i]) {
						_result[arr[index]] = source[arr[index]][i];
						if (index === arr.length - 1) {
							result.push(JSON.parse(JSON.stringify(_result)));
						} else {
							convert(arr, index + 1);
						}
					}
				}
			};
			convert(Object.keys(source), 0);

			return result;
		},

		submit() {
			// console.log(this.attribute);
			// console.log(this.tableData);
			this.attribute.forEach(item => {
				let data = {
					attributeName: item.name,
					goodsId: this.goodsId
				};
				insertPlugsGoodsAttributeKey(data).then(res => {
					if (res.code == 200) {
						if (item.name === res.body.attributeName) {
							item.id = res.body.id;
						}
						item.value.forEach((ress, indexx) => {
							console.log(ress);
							let data = {
								attributeValue: ress,
								attributeKeyId: item.id
							};
							insertPlugsGoodsAttributeValue(data).then(res => {
								if (res.code == 200) {
								}
							});
						});
					}
				});
			});
			setTimeout(() => {
				insertPlugsGoodsSpecification(this.tableData).then(res => {
					if (res.code == 200) {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});

						setTimeout(() => {
							uni.navigateBack({ delta: 1 });
						}, 500);
					}
				}, 1000);
			});
		}
	}
};
</script>

<style lang="scss">
page {
	padding: 20rpx;
	box-sizing: border-box;
}
.title {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding-bottom: 20rpx;
	border-bottom: 1rpx solid #f5f5f5;
	font-size: 26rpx;
	.title-left {
		font-weight: 700;
	}
	.button {
		width: 250rpx;
		color: #fff;
		background-color: #016eff;
		height: 50rpx;
		text-align: center;
		line-height: 50rpx;
		border-radius: 20rpx;
	}
}
.guige {
	padding: 20rpx 0;
	border-bottom: 1rpx solid #f5f5f5;
	position: relative;
	.guige-name {
		display: flex;
		align-items: center;
		width: 500rpx;
		.btn {
			width: 100rpx;
			color: #fff;
			height: 70rpx;
			font-size: 26rpx;
			background-color: #efefef;
			text-align: center;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
	.guige-value {
		margin-top: 20rpx;
		.tagList {
			display: flex;
			align-items: center;
		}
		.inputValue {
			display: flex;
			align-items: center;
			width: 650rpx;
			margin-top: 20rpx;
			.btn {
				width: 100rpx;
				color: #fff;
				height: 70rpx;
				font-size: 26rpx;
				background-color: #efefef;
				text-align: center;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}
	.del {
		position: absolute;
		top: 30rpx;
		right: 25rpx;
	}
}
.box {
	.box-name {
		padding: 20rpx;
		text-align: center;
	}
	.box-title {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
		.btn {
			width: 150rpx;
			height: 70rpx;
			font-size: 26rpx;
			background-color: #efefef;
			text-align: center;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
}
.footer {
	display: flex;
	align-items: center;
	justify-content: center;
	.footer-c {
		width: 500rpx;
		background-color: #016eff;
		color: #fff;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值