<template>
	<!--components/shopping-cart/shopping-cart.wxml-->
	<view class="contact ddflex" id="contact" @tap="jumpCart('/pages/cart/cart')">
		<image src="../../static/pages/images/cart.png"></image>
		<text class="dots-cart dflex" v-if="count > 0">{{count}}</text>
	</view>
</template>

<script>
	const app = getApp();
	const req = require("../../utils/request");

	export default {
		data() {
			return {
				count: ""
			};
		},

		components: {},
		props: {
			//是否更新
			isUpdate: {
				type: Boolean,
				default: false
			}
		},
		watch: {
			"isUpdate": function(isUpdate) {
				// console.log('需要更新消息', isUpdate);
				if (!this.isReady) return;
				this.getCount();
			}
		},

		mounted() {
			// console.log('ready');
			this.isReady = true;
			this.getCount();
		},

		methods: {
			getCount() {
				req.getRequest('/api/purchase/count', {
					purchaseType: 1
				}, data => {
					this.setData({
						count: data
					});
					// console.log(this);
				});
			},

			goshopCar() {
				uni.navigateTo({
					url: '/pages/cart/cart'
				});
			},
			jumpCart(url) {
				if (req.getStorage('cartTab')) {
					uni.$emit('tabClick', 'cart');
				} else {
					// app.globalData.openPage(url)
					uni.navigateTo({
						url: url
					});
				}
			}

		}
	};
</script>
<style>
	@import "./index.css";
</style>