优化 购物车数量请求

This commit is contained in:
fanxiaoqian
2019-08-30 15:38:51 +08:00
parent 859c6616b6
commit 89ed703b97
5 changed files with 56 additions and 22 deletions

View File

@@ -29,7 +29,7 @@
"list": []
},
"miniprogram": {
"current": 10,
"current": 11,
"list": [
{
"id": 0,
@@ -105,6 +105,14 @@
"id": -1,
"name": "pages/recommend/list",
"pathName": "pages/recommend/list",
"query": "",
"scene": null
},
{
"id": -1,
"name": "pages/item/espier-detail",
"pathName": "pages/item/espier-detail",
"query": "id=118",
"scene": null
}
]

View File

@@ -2,8 +2,6 @@ import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { connect } from '@tarojs/redux'
import { AtTabBar } from 'taro-ui'
import req from '@/api/req'
import api from '@/api'
import { navigateTo, getCurrentRoute } from '@/utils'
import S from '@/spx'
import { getTotalCount } from '@/store/cart'
@@ -11,8 +9,6 @@ import { getTotalCount } from '@/store/cart'
@connect(({ tabBar,cart }) => ({
tabBar: tabBar.current,
cartCount: cart.cartCount
}),(dispatch) => ({
onUpdateCartCount: (count) => dispatch({ type: 'cart/updateCount', payload: count })
}))
export default class TabBar extends Component {
static options = {
@@ -124,13 +120,6 @@ export default class TabBar extends Component {
return
}
try {
const { item_count } = await api.cart.count({shop_type: 'distributor'})
updateCartCount(item_count)
this.props.onUpdateCartCount(item_count)
} catch (e) {
console.error(e)
}
}
handleClick = (current) => {

View File

@@ -14,7 +14,8 @@ import './goods.scss'
onFastbuy: (item) => dispatch({ type: 'cart/fastbuy', payload: { item } }),
onAddCart: (item) => dispatch({ type: 'cart/add', payload: { item } }),
onAddFav: ({ item_id }) => dispatch({ type: 'member/addFav', payload: { item_id } }),
onDelFav: ({ item_id }) => dispatch({ type: 'member/delFav', payload: { item_id } })
onDelFav: ({ item_id }) => dispatch({ type: 'member/delFav', payload: { item_id } }),
onUpdateCartCount: (count) => dispatch({ type: 'cart/updateCount', payload: count })
}))
export default class WgtGoods extends Component {
static options = {
@@ -146,13 +147,22 @@ export default class WgtGoods extends Component {
Taro.showToast({
title: '成功加入购物车',
icon: 'success'
})
})
this.fetchCartcount()
} catch (error) {
console.log(error)
}
}
}
async fetchCartcount() {
try {
const { item_count } = await api.cart.count({shop_type: 'distributor'})
this.props.onUpdateCartCount(item_count)
} catch (e) {
console.error(e)
}
}
render () {
const { info } = this.props

View File

@@ -16,9 +16,11 @@ import { resolveFavsList } from './item/helper'
import './home/index.scss'
@connect(({ cart }) => ({
list: cart.list,
cartIds: cart.cartIds
cartIds: cart.cartIds,
cartCount: cart.cartCount
}), (dispatch) => ({
onUpdateLikeList: (show_likelist) => dispatch({ type: 'cart/updateLikeList', payload: show_likelist }),
onUpdateLikeList: (show_likelist) => dispatch({ type: 'cart/updateLikeList', payload: show_likelist }),
onUpdateCartCount: (count) => dispatch({ type: 'cart/updateCount', payload: count })
}))
@connect(store => ({
store
@@ -89,7 +91,8 @@ export default class HomeIndex extends Component {
}, () => {
this.fetchInfo()
})
}
}
this.fetchCartcount()
}
onShareAppMessage (res) {
@@ -101,7 +104,14 @@ export default class HomeIndex extends Component {
path: '/pages/index'
}
}
async fetchCartcount() {
try {
const { item_count } = await api.cart.count({shop_type: 'distributor'})
this.props.onUpdateCartCount(item_count)
} catch (e) {
console.error(e)
}
}
async fetchInfo () {
const url = '/pageparams/setting?template_name=yykweishop&version=v1.0.1&page_name=index'
const info = await req.get(url)

View File

@@ -5,9 +5,14 @@ import { GoodsItem, SpCheckbox, GoodsBuyPanel } from '@/components'
import { classNames, formatTime, pickBy } from '@/utils'
import S from '@/spx'
import api from '@/api'
import { connect } from '@tarojs/redux'
import './package-item.scss';
@connect(({ cart }) => ({
cart,
}), (dispatch) => ({
onUpdateCartCount: (count) => dispatch({ type: 'cart/updateCount', payload: count })
}))
export default class PackageItem extends Component {
static options = {
addGlobalClass: true
@@ -169,15 +174,27 @@ export default class PackageItem extends Component {
activity_id: packageId,
activity_type: 'package',
distributor_id
}
const res = await api.cart.add(query)
}
const res = await api.cart.add(query)
if (res) {
Taro.showToast({
title: '成功加入购物车',
icon: 'success'
})
})
this.fetchCartcount()
}
}
async fetchCartcount() {
try {
const { item_count } = await api.cart.count({shop_type: 'distributor'})
this.props.onUpdateCartCount(item_count)
} catch (e) {
console.error(e)
}
}
countPackageTotal () {
const { selection, packagePrices, mainItem } = this.state