<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>
<p>后端服务器模拟说明
内容:本文档为后端服务器模拟说明,旨在帮助开发者模拟后端服务器,实现前后端分离开发。</p>
<p>使用方法:</p>
<ol>
<li>下载并安装json-server</li>
</ol>
<p>在命令行中输入以下命令:</p>
<pre><code class="language-shell">npm install -g json-server
</code></pre>
<ol start="2">
<li>创建mock数据文件</li>
</ol>
<p>在项目根目录下创建一个<code>mock</code>文件夹,并在该文件夹下创建一个<code>db.json</code>文件,用于存储mock数据。</p>
<ol start="3">
<li>编写mock数据</li>
</ol>
<p>在<code>db.json</code>文件中编写mock数据。例如:</p>
<pre><code class="language-json">{
  &quot;goods&quot;: [
    {
      &quot;id&quot;: 1,
      &quot;title&quot;: &quot;商品1&quot;,
      &quot;cover_url&quot;: &quot;https://img.yzcdn.cn/vant/cat.jpeg&quot;,
      &quot;price&quot;: 100,
      &quot;sales&quot;: 10,
      &quot;details&quot;: &quot;&lt;p&gt;商品1详情&lt;/p&gt;&quot;,
      &quot;comments&quot;: [
        {
          &quot;id&quot;: 1,
          &quot;user&quot;: {
            &quot;name&quot;: &quot;用户1&quot;,
            &quot;avatar_url&quot;: &quot;https://img.yzcdn.cn/vant/cat.jpeg&quot;
          },
          &quot;content&quot;: &quot;评论1&quot;,
          &quot;created_at&quot;: &quot;2021-01-01 12:00:00&quot;
        },
        {
          &quot;id&quot;: 2,
          &quot;user&quot;: {
            &quot;name&quot;: &quot;用户2&quot;,
            &quot;avatar_url&quot;: &quot;https://img.yzcdn.cn/vant/cat.jpeg&quot;
          },
          &quot;content&quot;: &quot;评论2&quot;,
          &quot;created_at&quot;: &quot;2021-01-02 12:00:00&quot;
        }
      ],
      &quot;is_collect&quot;: 0
    },
    {
      &quot;id&quot;: 2,
      &quot;title&quot;: &quot;商品2&quot;,
      &quot;cover_url&quot;: &quot;https://img.yzcdn.cn/vant/cat.jpeg&quot;,
      &quot;price&quot;: 200,
      &quot;sales&quot;: 20,
      &quot;details&quot;: &quot;&lt;p&gt;商品2详情&lt;/p&gt;&quot;,
      &quot;comments&quot;: [
        {
          &quot;id&quot;: 3,
          &quot;user&quot;: {
            &quot;name&quot;: &quot;用户3&quot;,
            &quot;avatar_url&quot;: &quot;https://img.yzcdn.cn/vant/cat.jpeg&quot;
          },
          &quot;content&quot;: &quot;评论3&quot;,
          &quot;created_at&quot;: &quot;2021-01-03 12:00:00&quot;
        },
        {
          &quot;id&quot;: 4,
          &quot;user&quot;: {
            &quot;name&quot;: &quot;用户4&quot;,
            &quot;avatar_url&quot;: &quot;https://img.yzcdn.cn/vant/cat.jpeg&quot;
          },
          &quot;content&quot;: &quot;评论4&quot;,
          &quot;created_at&quot;: &quot;2021-01-04 12:00:00&quot;
        }
      ],
      &quot;is_collect&quot;: 1
    }
  ],
  &quot;users&quot;: [
    {
      &quot;id&quot;: 1,
      &quot;username&quot;: &quot;user1&quot;,
      &quot;password&quot;: &quot;123456&quot;
    },
    {
      &quot;id&quot;: 2,
      &quot;username&quot;: &quot;user2&quot;,
      &quot;password&quot;: &quot;123456&quot;
    }
  ],
  &quot;carts&quot;: [
    {
      &quot;id&quot;: 1,
      &quot;user_id&quot;: 1,
      &quot;goods_id&quot;: 1,
      &quot;count&quot;: 2
    },
    {
      &quot;id&quot;: 2,
      &quot;user_id&quot;: 1,
      &quot;goods_id&quot;: 2,
      &quot;count&quot;: 1
    }
  ]
}
</code></pre>
<ol start="4">
<li>启动json-server</li>
</ol>
<p>在命令行中进入项目根目录,并输入以下命令启动json-server:</p>
<pre><code class="language-shell">json-server --watch mock/db.json
</code></pre>
<p>其中,<code>--watch</code>参数表示监视文件变化并自动重新加载,<code>mock/db.json</code>表示mock数据文件路径。</p>
<ol start="5">
<li>修改API请求地址</li>
</ol>
<p>在API请求中将请求地址修改为<code>http://localhost:3000</code>,例如:</p>
<pre><code class="language-javascript">// 获取商品详情
async goodsInfo(id) {
  const res = await this.$http.get(`/goods/${id}`)
  return res.data
}
</code></pre>
<p>其中,<code>/goods/${id}</code>表示请求<code>http://localhost:3000/goods/${id}</code>地址的数据。</p>
<ol start="6">
<li>完成!</li>
</ol>
<p>现在您可以在前端页面中模拟后端服务器返回的数据了。</p>
商品详情页 - 优质商品,值得拥有

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

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