uni-app商城(八)商品详情页

1.定义全局数据共享store

import Vue from 'vue'
import Vuex from 'vuex'
import moduleCart from '@/store/cart.js'
Vue.use(Vuex)
const store=new Vuex.Store({
	modules:{
		//挂载购物车的vuex模块,模块内成员的访问路径调整为m_cart
		//购物车模块中cart数组访问路径是m_cart/cart
		'm_cart':moduleCart
	}
})
export default store

2.把store挂载到vue

import App from './App'
// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
import store from '@/store/store.js'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
	...App,
	store
})
...

3.store —cart.js

export default {
	//为当前模块开启命名空间
	namespaced: true,
	state: () => ({
		// 购物车存储的数据,每个商品的数据对象
		cart: JSON.parse(uni.getStorageSync('cart') || '[]')
	}),
	//模块mutations方法
	mutations: {
		addToCart(state, goods) {
			const findRes = state.cart.find(x => x.id === goods.id);

			if (!findRes) {
				//如果购物车没有这个商品,直接push
				state.cart.push(goods);
			} else {
				//否则更新数量
				findRes.count++;
			}
			this.commit('m_cart/saveToStorage')
		},
		//游客保存购物车到本地
		saveToStorage(state) {
			uni.setStorageSync('cart', JSON.stringify(state.cart))
		}
	},
	//模块getters属性
	getters: {
		//统计商品数量累加
		total(state) {
			let c = 0;
			state.cart.forEach(goods => c += goods.count)
			return c
		}
	}
}

4.详情页面代码

<template>
	<view class="goods-detail-container">
		<!-- 轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item,i) in goodsInfo.picsList" :key="i">
				<image :src="item" @click="preview(i)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息 -->
		<view class="goods-info-box">
			<!-- 商品价格 -->
			<view class="goods-price">¥{{goodsInfo.price}}</view>
			<view class="goods-info-body">
				<!-- 商品名称 -->
				<view class="goods-name">{{goodsInfo.name}}</view>
				<!-- 收藏 -->
				<view class="favi">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="yf">快递:免运费</view>
		</view>

		<!-- 详情 -->
		<!-- <rich-text :nodes="goodsInfo.introduce"></rich-text> -->
		<view v-for="(item) in 20">xxxxxxxxxxxxxxxxxxxxxxxxxxxx{{item}}</view>

		<!-- 底部商品导航组件 -->
		<view class="goods-nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex';

	export default {
		computed: {
			//映射m_cart模块中total方法
			...mapState('m_cart', ['cart']),
			...mapGetters('m_cart', ['total'])
		},
		watch: {
			// total(newVal) {
			// 	const findRes = this.options.find(x => x.text === '购物车');
			// 	if (findRes) {
			// 		findRes.info = newVal;
			// 	}
			// }
			total: {
				handler(newVal) {
					const findRes = this.options.find(x => x.text === '购物车');
					if (findRes) {
						findRes.info = newVal;
					}
				},
				//初次加载完毕后立即调用
				immediate: true
			}
		},
		data() {
			return {
				goodsInfo: {},
				options: [{
					icon: 'headphones',
					text: '客服'
				}, {
					icon: 'shop',
					text: '店铺',
					info: 0,
					infoBackgroundColor: '#007aff',
					infoColor: "white"
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: 'linear-gradient(90deg, #1E83FF, #0053B8)',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: 'linear-gradient(90deg, #60F3FF, #088FEB)',
						color: '#fff'
					}
				]
			}
		},
		onLoad(options) {
			const id = options.id;
			this.getGoodsDetail(id);
		},
		methods: {
			//映射m_cart模块中addToCart方法
			...mapMutations('m_cart', ['addToCart']),
			getGoodsDetail(id) {
				uni.request({
					url: uni.$baseUrl + '/api/Home/GetGoodsDetail',
					data: {
						"id": 1019
					}
				}).then((res) => {
					if (res.data.code != 200) return uni.$showMsg();
					this.goodsInfo = res.data.data;
				});
			},
			//轮播图预览
			preview(i) {
				uni.previewImage({
					current: i,
					urls: this.goodsInfo.picsList.map(x => x)
				})
			},
			//跳转到购物车
			onClick(e) {
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			//加入购物车
			buttonClick(e) {
				if (e.content.text === '加入购物车') {
					const goods = {
						id: this.goodsInfo.id,
						name: this.goodsInfo.name,
						price: this.goodsInfo.price,
						count: 1,
						logo: this.goodsInfo.logo,
						state: true
					}
					//通过this调用映射过来的addToCart方法,把商品信息对象存储到购物车
					this.addToCart(goods);
				}
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 750rpx;
		image {
			width: 100%;
			height: 100%;
		}
	}
	.goods-info-box {
		padding: 10px;
		padding-right: 0;
		.goods-price {
			color: #c00000;
			font-size: 18px;
			margin: 10px 0;
		}
		.goods-info-body {
			display: flex;
			justify-content: space-between;
			.goods-name {
				font-size: 13px;
				margin-right: 10px;
			}
			.favi {
				width: 120px;
				font-size: 12px;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				border-left: 1px solid #efefef;
				color: gray;
			}
		}
		.yf {
			font-size: 12px;
			color: gary;
			margin: 10px 0;
		}
	}
	.goods-nav {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
	.goods-detail-container {
		padding-bottom: 50px;
	}
</style>

5.效果示例

在这里插入图片描述

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值