<template>
	<view class='u-rela u-skeleton'>
		<!-- 商品信息 -->
		<view class='goods-item'>
			<u-image class='u-skeleton-rect' width='100%' height='500rpx' 
			:src='goodsInfo.cover_url'></u-image>
			<view class='name u-line-1 u-p-t-20 u-p-b-20 u-skeleton-rect'>{{goodsInfo.title}}</view>
			<view class='u-flex u-row-between'>
				<view class='price u-skeleton-rect'>¥ {{goodsInfo.price}}</view>
				<view class='sales u-skeleton-rect'>销量: {{goodsInfo.sales}}</view>
			</view>
		</view>
<pre><code>	&lt;!-- tab组件 --&gt;
	&lt;u-tabs  class='u-m-l-20 u-m-r-20 u-skeleton-rect' :list='list' 
	:is-scroll='false' :current='current' @change='changeTabs'&gt;&lt;/u-tabs&gt;
	&lt;view class='content u-m-l-20 u-m-r-20 u-p-b-80 u-skeleton-rect'&gt;
		&lt;!-- 商品详情 --&gt;
		&lt;template v-if='current === 0'&gt;
			&lt;u-parse :html='goodsInfo.details'&gt;&lt;/u-parse&gt;
		&lt;/template&gt;

		&lt;!-- 商品评论 --&gt;
		&lt;template v-if='current === 1'&gt;
			&lt;view class='comment' v-for='(res, index) in commentList' :key='res.id'&gt;
				&lt;view class='left'&gt;
					&lt;image :src='res.user.avatar_url' mode='aspectFill'&gt;&lt;/image&gt;
				&lt;/view&gt;
				&lt;view class='right'&gt;
					&lt;view class='top'&gt;
						&lt;view class='name'&gt;{{ res.user.name }}&lt;/view&gt;
					&lt;/view&gt;
					&lt;view class='content'&gt;{{ res.content }}&lt;/view&gt;
					&lt;view class='bottom'&gt;
						{{ res.created_at }}
						
					&lt;/view&gt;
				&lt;/view&gt;
			&lt;/view&gt;
		&lt;/template&gt;

		&lt;!-- 推荐商品 --&gt;
		&lt;template v-if='current === 2'&gt;
			&lt;u-row gutter='16'&gt;
				&lt;u-col span='6' v-for='(item,index) in like_goodsList' :key='index'&gt;
					&lt;goods_card :goods='item'&gt;&lt;/goods_card&gt;
				&lt;/u-col&gt;
			&lt;/u-row&gt;
		&lt;/template&gt;
	&lt;/view&gt;

	&lt;view class='navigation'&gt;
		&lt;view class='left'&gt;
			&lt;view class='item u-text-center' @click='collectionHandle()'&gt;
				&lt;u-icon name='star-fill' :size='40' :color='isCollect === 1 ? '#ed3f14' : ''&gt;&lt;/u-icon&gt;
				&lt;view class='text u-line-1' v-if='isCollect !== 1'&gt;收藏&lt;/view&gt;
				&lt;view class='text u-line-1' style='color: #ed3f14;' v-else&gt;已收藏&lt;/view&gt;
			&lt;/view&gt;
			&lt;view class='item car' @click='toCart'&gt;
				&lt;u-badge class='car-num' :count='cartCount' type='error' :offset='[-3, -6]'&gt;&lt;/u-badge&gt;
				&lt;u-icon name='shopping-cart' :size='40' :color='$u.color['contentColor']'&gt;&lt;/u-icon&gt;
				&lt;view class='text u-line-1'&gt;购物车&lt;/view&gt;
			&lt;/view&gt;
		&lt;/view&gt;
		&lt;view class='right'&gt;
			&lt;view class='cart btn u-line-1' @click='addCart'&gt;加入购物车&lt;/view&gt;
			&lt;view class='buy btn u-line-1'&gt;立即购买&lt;/view&gt;
		&lt;/view&gt;
	&lt;/view&gt;
	&lt;!--引用组件--&gt;
	&lt;u-skeleton :loading='loading' :animation='true' bgColor='#FFF'&gt;&lt;/u-skeleton&gt;
&lt;/view&gt;
</code></pre>
</template>
<script>
	export default {
		data() {
			return {
				goodsInfo: {}, // 商品信息
				list: [{
					name: '商品详情'
				}, {
					name: '商品评论',
					count: 0,
				}, {
					name: '推荐商品'
				}], // tab列表
				current: 0, //当前选中的tab选项
				commentList: [], // 评论列表
				isCollect: 0, // 是否收藏
				like_goodsList: [], // 推荐商品列表
				loading: false, // 信息加载
				cartCount: 0,// 购物车数量
				goodsId: null,//商品Id
			}
		},
		onLoad(options) {
			// options是页面跳转时的参数
			this.goodsId = options.id
			// 初始化页面数据
			this.getGoodsDetail()
			// 初始化购物车
			this.getCartsCount()
		},
		methods: {
			// 获取商品信息
			async getGoodsDetail() {
				// this.loading = true
				const res = await this.$u.api.goodsInfo(this.goodsId)
				console.log(res.goods);
				// this.loading = false
				// // console.log(res)
				// 获取评论数,用于显示在tab上
				this.list[1].count = res.goods.comments.length
				// // 获取商品信息
				this.goodsInfo = res.goods
				this.isCollect = res.goods.is_collect
				// // 评论列表
				this.commentList = res.goods.comments
				console.log(this.commentList);
				// // 推荐商品列表
				this.like_goodsList = res.like_goods
			},
			// 切换tab选项
			changeTabs(index) {
				this.current = index
			},
			// 点击收藏以及取消
			async collectionHandle() {
				if (!this.$u.utils.isLogin()) return;
				// 请求收藏API
				await this.$u.api.goodsCollect(this.goodsId)
				if (this.isCollect === 0) {
					this.isCollect = 1
					this.$u.toast('收藏成功')
				} else {
					this.isCollect = 0
					this.$u.toast('取消收藏成功')
				}
			},
			
			// 加入购物车
			async addCart(){
				if(!this.$u.utils.isLogin())return
				if(this.$u.utils.isLogin()){
					const params = {
						goods_id: this.goodsId
					}
					
					await this.$u.api.cartAdd(params)
					// 更新购物车数量
					this.getCartsCount()
					this.$u.toast('添加购物车成功')
				}

			},
			// 获取购物车内数量,必须登录,不然就不显示数量
			async getCartsCount(){
				const token = this.vuex_token
				if(!token){
					return
				}else{
					const res = await this.$u.api.cartList()
					console.log(res)
					this.cartCount = res.data.length
				}
			},
			
			// 点击购物车跳转到购物车页面
			toCart(){
				if(this.$u.utils.isLogin()){
					this.$u.route({
						// 挑战到tabbar用到switchTab
						type: 'switchTab',
						url:'pages/cart/cart'
					})
				}
			}
		}
	}
</script>
<style lang='scss' scoped>
	// 商品信息
	.goods-item {
		padding: 20rpx;
		margin-bottom: 20rpx; 
		// 标题
		.name {
			font-size: 36rpx;
			font-weight: bold;
			margin-top: 20rpx;
			margin-bottom: 20rpx;
			width: 100%;
		}

		// 价格
		.price {
			width: 40%;
			color: red;
			padding-left: 20rpx;
			font-weight: bold;
			font-size: 32rpx;
		}

		// 销量
		.sales {
			width: 40%;
			color: #888;
			font-weight: bold;
			font-size: 32rpx;
			padding-right: 20rpx;
		}
	}

	.content {
		min-height: 360rpx;
	}

	// 底部
	.navigation {
		display: flex;
		// margin-top: 100rpx;
		border: solid 2rpx #f2f2f2;
		background-color: #ffffff;
		padding: 16rpx 0;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;

		.left {
			display: flex;
			font-size: 20rpx;
			margin-right: 80rpx;

			.item {
				margin: 0 30rpx;

				&.car {
					text-align: center;
					position: relative;

					.car-num {
						position: absolute;
						top: -10rpx;
						right: -10rpx;
					}
				}
			}
		}

		.right {
			display: flex;
			font-size: 28rpx;
			align-items: center;

			.btn {
				line-height: 66rpx;
				padding: 0 30rpx;
				border-radius: 36rpx;
				color: #ffffff;
			}

			.cart {
				background-color: #ed3f14;
				margin-right: 30rpx;
			}

			.buy {
				background-color: #ff7900;
			}
		}
	}
</style>

原文地址: https://www.cveoy.top/t/topic/oys6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录