import React, { Component } from 'react' import { StyleSheet, View, StatusBar, Text, TouchableOpacity, PixelRatio, PanResponder, Dimensions, ImageBackground, ScrollView, ActivityIndicator, RefreshControl, } from 'react-native' import { connect } from 'react-redux' import Checkbox from '../../components/checkbox/index' import Icon from '../../components/Iconfont/Iconfont' import { NavigationActions, createAction } from '../../utils' import { ScaleUtil } from '../../utils/utils' import ShoppingCart from './ShoppingCart' import EmptyShop from '../../static/images/Empty-shop.png' import buttonImg from '../../static/images/home-topred.png' import Toast from 'react-native-root-toast' const { width, height } = Dimensions.get('window') @connect(({ theme, shoppingcart }) => ({ ...theme, ...shoppingcart })) class ShoppingCartHome extends Component { constructor(props) { super(props) this.state = { damping: 1 - 0.6, tension: 300, edited: false, secectAll: false, change: false, Amount: 0, Loading: false, } this._this = [] console.disableYellowBox = true this.panResponder = PanResponder.create({ onStartShouldSetPanResponderCapture: (evt, gestureState) => { if ( this && this._this && this._this[0] && this._this[0].interactableElem ) { this._this[0].interactableElem.snapTo({ index: 0 }) } return false }, }) } static navigationOptions = ({ navigation, screenProps }) => ({ tabBarOnPress: obj => { obj.jumpToIndex(obj.scene.index) navigation.state.params && navigation.state.params.navigatePress() }, tabBarLabel: ({ tintColor }) => ( 购物车 ), tabBarIcon: ({ focused, tintColor }) => tintColor == '#333' ? ( ) : ( ), }) componentDidMount = () => { this.props.navigation.setParams({ navigatePress: () => this.getCartList(), }) this.getCartList() } setLoading(bool) { this.setState({ Loading: bool }) } getCartList() { this.props.dispatch( createAction('shoppingcart/getShoppingList')({ params: { customer: CUSTOMERINFO.id, }, }) ) } onRefresh() { this.getCartList() // delay(this.setState({ isRefreshing: false }), 500000); // this.setState({ headerView: true }); } // 初始化this._this initThis() { this._this = [] } calAmount(e) { let newData = JSON.parse(JSON.stringify(e)) let amount = 0 for (val of newData) { if (val.isChecked) { for (vasec of val.data) { amount = vasec.salePrice * vasec.orderNum + amount } } else { for (valsec of val.data) { if (valsec.isChecked) { amount = valsec.salePrice * valsec.orderNum + amount } } } } this.setState({ Amount: amount }) } // 删除选中商品 delSelectCart() { const { ShopList } = this.props let seletCart = [] ShopList.forEach(e => { seletCart = seletCart.concat(e.data.filter(i => i.isChecked)) }) console.log(seletCart) if (seletCart.length > 0) { this.props.dispatch( createAction('shoppingcart/deleteShop')({ params: { customer: CUSTOMERINFO.id, id: seletCart.map(e => e.id).join(",") }, }) ) } } // 情况购物车 async clearCart() { await this.props.dispatch( createAction('shoppingcart/emptyCart')({ params: { customer: CUSTOMERINFO.id }, }) ) this.setState({ edited: !this.state.edited, Amount: 0 }) } render() { const { appTheme, ShopList, TotalNum } = this.props return ( {/* 头部 */} {this.props.navigation.state.params == 'otherToShop' ? ( this.props.dispatch(NavigationActions.back())} > ) : null} 购物车 {ShopList && ShopList.length > 0 ? ( { this.setState({ edited: !this.state.edited, secectAll: false, change: false }); this.initThis(); }} > {this.state.edited ? '完成' : '编辑'} ) : null} {ShopList && ShopList.length > 0 ? ( this.onRefresh)或者通过bind来绑定this引用来调用方法 tintColor="red" title={this.props.isRefreshing ? '刷新中....' : '下拉刷新'} /> } > this.setLoading(e)} ref={mdl => (this.mdl = mdl)} dispatch={this.props.dispatch} ShopList={ShopList} secectAll={this.state.secectAll} edited={this.state.edited} change={this.state.change} totalnum={TotalNum} secectOnchange={bool => { this.setState({ secectAll: bool, change: false }) }} calAmount={e => this.calAmount(e)} addThis={e => { let ShopThis = this, itemData = [] if (ShopThis._this.length == 0) { ShopThis._this.push(e) } else { for (let i in ShopThis._this) { itemData.push(ShopThis._this[i].state.itemData) } if (e && itemData.indexOf(e.state.itemData) == -1) { ShopThis._this.push(e) } } if ( ShopThis._this.length == 2 && ShopThis._this[0].interactableElem ) { ShopThis._this[0].interactableElem.snapTo({ index: 0 }) ShopThis._this.splice(0, 1) } }} /> ) : ( this.onRefresh)或者通过bind来绑定this引用来调用方法 tintColor="red" title={this.props.isRefreshing ? '刷新中....' : '下拉刷新'} /> } > 购物车是空的哦~ )} {/* 底部 */} {this.state.edited ? ( 全选 } onChange={bool => this.setState({ secectAll: bool, change: true, }) } /> ) : null} {this.state.edited ? ( { if (this.toast) { Toast.hide(this.toast) } console.log(ShopList); if (!this.state.Loading && ShopList && ShopList.length > 0) { this.delSelectCart() } else { this.toast = Toast.show('购物车为空!', { position: toastHeight, }) } } } > 删除选中商品 ) : ( 合计: {CURRENCY.currencySign} {ScaleUtil(this.state.Amount, CURRENCY.currencyAmountScale)} )} {this.state.edited ? ( { if (this.toast) { Toast.hide(this.toast) } if (!this.state.Loading) { this.clearCart() } else { this.toast = Toast.show('购物车为空!', { position: toastHeight, }) } } } style={{ flex: 3, justifyContent: 'center', }} > 清空购物车 ) : ( { if (this.toast) { Toast.hide(this.toast) } if (!this.state.Loading && ShopList && ShopList.length > 0) { console.log(ShopList); this.mdl._settleTou() } else { this.toast = Toast.show('购物车为空!', { position: toastHeight, }) } // ? null // : ShopList && ShopList.length > 0 // ? this.mdl._settleTou() // : alert("购物车为空!") }} style={{ flex: 3, backgroundColor: ShopList && ShopList.length > 0 ? '#E70013' : '#969696', justifyContent: 'center', }} > {this.state.Loading ? ( ) : ( 结算 )} )} ) } } const styles = StyleSheet.create({ container: { flex: 1, }, icon: { width: 32, height: 32, }, }) export default ShoppingCartHome