ShoppingCartHome.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. import React, { Component } from 'react'
  2. import {
  3. StyleSheet,
  4. View,
  5. StatusBar,
  6. Text,
  7. TouchableOpacity,
  8. PixelRatio,
  9. PanResponder,
  10. Dimensions,
  11. ImageBackground,
  12. ScrollView,
  13. ActivityIndicator,
  14. RefreshControl,
  15. } from 'react-native'
  16. import { connect } from 'react-redux'
  17. import Checkbox from '../../components/checkbox/index'
  18. import Icon from '../../components/Iconfont/Iconfont'
  19. import { NavigationActions, createAction } from '../../utils'
  20. import { ScaleUtil } from '../../utils/utils'
  21. import ShoppingCart from './ShoppingCart'
  22. import EmptyShop from '../../static/images/Empty-shop.png'
  23. import buttonImg from '../../static/images/home-topred.png'
  24. import Toast from 'react-native-root-toast'
  25. const { width, height } = Dimensions.get('window')
  26. @connect(({ theme, shoppingcart }) => ({ ...theme, ...shoppingcart }))
  27. class ShoppingCartHome extends Component {
  28. constructor(props) {
  29. super(props)
  30. this.state = {
  31. damping: 1 - 0.6,
  32. tension: 300,
  33. edited: false,
  34. secectAll: false,
  35. change: false,
  36. Amount: 0,
  37. Loading: false,
  38. }
  39. this._this = []
  40. console.disableYellowBox = true
  41. this.panResponder = PanResponder.create({
  42. onStartShouldSetPanResponderCapture: (evt, gestureState) => {
  43. if (
  44. this &&
  45. this._this &&
  46. this._this[0] &&
  47. this._this[0].interactableElem
  48. ) {
  49. this._this[0].interactableElem.snapTo({ index: 0 })
  50. }
  51. return false
  52. },
  53. })
  54. }
  55. static navigationOptions = ({ navigation, screenProps }) => ({
  56. tabBarOnPress: obj => {
  57. obj.jumpToIndex(obj.scene.index)
  58. navigation.state.params && navigation.state.params.navigatePress()
  59. },
  60. tabBarLabel: ({ tintColor }) => (
  61. <Text
  62. style={{
  63. alignSelf: 'center',
  64. marginBottom: 2,
  65. fontSize: 10,
  66. lineHeight: 14,
  67. letterSpacing: 0.12,
  68. color: tintColor,
  69. }}
  70. >
  71. 购物车
  72. </Text>
  73. ),
  74. tabBarIcon: ({ focused, tintColor }) =>
  75. tintColor == '#333' ? (
  76. <View style={{ paddingTop: 7, paddingRight: 4 }}>
  77. <Icon name="icon-icon-gouwuche" size={28} color={tintColor} />
  78. </View>
  79. ) : (
  80. <View style={{ paddingTop: 7, paddingRight: 4 }}>
  81. <Icon
  82. name="icon-icon-gouwuchexuanzhong"
  83. size={28}
  84. color={tintColor}
  85. />
  86. </View>
  87. ),
  88. })
  89. componentDidMount = () => {
  90. this.props.navigation.setParams({
  91. navigatePress: () => this.getCartList(),
  92. })
  93. this.getCartList()
  94. }
  95. setLoading(bool) {
  96. this.setState({ Loading: bool })
  97. }
  98. getCartList() {
  99. this.props.dispatch(
  100. createAction('shoppingcart/getShoppingList')({
  101. params: {
  102. customer: CUSTOMERINFO.id,
  103. },
  104. })
  105. )
  106. }
  107. onRefresh() {
  108. this.getCartList()
  109. // delay(this.setState({ isRefreshing: false }), 500000);
  110. // this.setState({ headerView: true });
  111. }
  112. // 初始化this._this
  113. initThis() {
  114. this._this = []
  115. }
  116. calAmount(e) {
  117. let newData = JSON.parse(JSON.stringify(e))
  118. let amount = 0
  119. for (val of newData) {
  120. if (val.isChecked) {
  121. for (vasec of val.data) {
  122. amount = vasec.salePrice * vasec.orderNum + amount
  123. }
  124. } else {
  125. for (valsec of val.data) {
  126. if (valsec.isChecked) {
  127. amount = valsec.salePrice * valsec.orderNum + amount
  128. }
  129. }
  130. }
  131. }
  132. this.setState({ Amount: amount })
  133. }
  134. // 删除选中商品
  135. delSelectCart() {
  136. const { ShopList } = this.props
  137. let seletCart = []
  138. ShopList.forEach(e => {
  139. seletCart = seletCart.concat(e.data.filter(i => i.isChecked))
  140. })
  141. console.log(seletCart)
  142. if (seletCart.length > 0) {
  143. this.props.dispatch(
  144. createAction('shoppingcart/deleteShop')({
  145. params: {
  146. customer: CUSTOMERINFO.id,
  147. id: seletCart.map(e => e.id).join(",")
  148. },
  149. })
  150. )
  151. }
  152. }
  153. // 情况购物车
  154. async clearCart() {
  155. await this.props.dispatch(
  156. createAction('shoppingcart/emptyCart')({
  157. params: {
  158. customer: CUSTOMERINFO.id
  159. },
  160. })
  161. )
  162. this.setState({
  163. edited: !this.state.edited
  164. })
  165. }
  166. render() {
  167. const { appTheme, ShopList, TotalNum } = this.props
  168. return (
  169. <View
  170. {...this.panResponder.panHandlers}
  171. style={[
  172. styles.container,
  173. { backgroundColor: appTheme.backgroundColor },
  174. ]}
  175. >
  176. <StatusBar
  177. animated={true}
  178. barStyle={appTheme.barStyle}
  179. // barStyle={"dark-content"}
  180. backgroundColor={'transparent'}
  181. translucent={true}
  182. />
  183. {/* 头部 */}
  184. <View
  185. style={{
  186. height: HEADERSTYLE.height,
  187. paddingTop: HEADERSTYLE.paddingTop + 5,
  188. backgroundColor: '#fff',
  189. }}
  190. >
  191. <View
  192. style={{
  193. flex: 1,
  194. justifyContent: 'center',
  195. }}
  196. >
  197. {this.props.navigation.state.params == 'otherToShop' ? (
  198. <TouchableOpacity
  199. style={{ position: 'absolute', left: 10, paddingTop: 5 }}
  200. onPress={() => this.props.dispatch(NavigationActions.back())}
  201. >
  202. <Icon name="icon-icon-fanhui" size={24} color={'#666'} />
  203. </TouchableOpacity>
  204. ) : null}
  205. <Text
  206. style={{
  207. alignSelf: 'center',
  208. fontSize: 18,
  209. lineHeight: 25,
  210. letterSpacing: 0.22,
  211. color: '#333',
  212. }}
  213. >
  214. 购物车
  215. </Text>
  216. {ShopList && ShopList.length > 0 ? (
  217. <TouchableOpacity
  218. style={{ position: "absolute", right: 10 }}
  219. onPress={() => {
  220. this.setState({
  221. edited: !this.state.edited,
  222. secectAll: false,
  223. change: false
  224. });
  225. this.initThis();
  226. }}
  227. >
  228. <Text
  229. style={{
  230. fontSize: 14,
  231. lineHeight: 20,
  232. letterSpacing: 0.17,
  233. color: "#666"
  234. }}
  235. >
  236. {this.state.edited ? '完成' : '编辑'}
  237. </Text>
  238. </TouchableOpacity>
  239. ) : null}
  240. </View>
  241. </View>
  242. {ShopList && ShopList.length > 0 ? (
  243. <ScrollView
  244. style={{ flex: 1 }}
  245. showsHorizontalScrollIndicator={false}
  246. refreshControl={
  247. <RefreshControl
  248. refreshing={this.props.isRefreshing}
  249. onRefresh={this.onRefresh.bind(this)} //(()=>this.onRefresh)或者通过bind来绑定this引用来调用方法
  250. tintColor="red"
  251. title={this.props.isRefreshing ? '刷新中....' : '下拉刷新'}
  252. />
  253. }
  254. >
  255. <ShoppingCart
  256. onRefresh={this.onRefresh.bind(this)}
  257. pressLoading={e => this.setLoading(e)}
  258. ref={mdl => (this.mdl = mdl)}
  259. dispatch={this.props.dispatch}
  260. ShopList={ShopList}
  261. secectAll={this.state.secectAll}
  262. edited={this.state.edited}
  263. change={this.state.change}
  264. totalnum={TotalNum}
  265. secectOnchange={bool => {
  266. this.setState({ secectAll: bool, change: false })
  267. }}
  268. calAmount={e => this.calAmount(e)}
  269. addThis={e => {
  270. let ShopThis = this,
  271. itemData = []
  272. if (ShopThis._this.length == 0) {
  273. ShopThis._this.push(e)
  274. } else {
  275. for (let i in ShopThis._this) {
  276. itemData.push(ShopThis._this[i].state.itemData)
  277. }
  278. if (e && itemData.indexOf(e.state.itemData) == -1) {
  279. ShopThis._this.push(e)
  280. }
  281. }
  282. if (
  283. ShopThis._this.length == 2 &&
  284. ShopThis._this[0].interactableElem
  285. ) {
  286. ShopThis._this[0].interactableElem.snapTo({ index: 0 })
  287. ShopThis._this.splice(0, 1)
  288. }
  289. }}
  290. />
  291. </ScrollView>
  292. ) : (
  293. <ScrollView
  294. style={{ flex: 1 }}
  295. showsHorizontalScrollIndicator={false}
  296. refreshControl={
  297. <RefreshControl
  298. refreshing={this.props.isRefreshing}
  299. onRefresh={this.onRefresh.bind(this)} //(()=>this.onRefresh)或者通过bind来绑定this引用来调用方法
  300. tintColor="red"
  301. title={this.props.isRefreshing ? '刷新中....' : '下拉刷新'}
  302. />
  303. }
  304. >
  305. <ImageBackground
  306. resizeMode="contain"
  307. source={EmptyShop}
  308. style={{
  309. flex: 1,
  310. width: width - 80,
  311. height: height / 1.5,
  312. alignSelf: 'center',
  313. }}
  314. >
  315. <View
  316. style={{
  317. position: 'absolute',
  318. bottom: '15%',
  319. alignSelf: 'center',
  320. }}
  321. >
  322. <Text
  323. style={{
  324. fontSize: 14,
  325. lineHeight: 20,
  326. letterSpacing: 0.17,
  327. color: '#999',
  328. }}
  329. >
  330. 购物车是空的哦~
  331. </Text>
  332. </View>
  333. </ImageBackground>
  334. </ScrollView>
  335. )}
  336. {/* 底部 */}
  337. <View
  338. style={{
  339. height:
  340. this.props.navigation.state.params == 'otherToShop'
  341. ? 45 + HEADERSTYLE.paddingBottom
  342. : 45,
  343. paddingBottom:
  344. this.props.navigation.state.params == 'otherToShop'
  345. ? HEADERSTYLE.paddingBottom
  346. : 0,
  347. backgroundColor: '#fff',
  348. flexDirection: 'row',
  349. borderTopColor: '#EEE',
  350. borderTopWidth: 1 / PixelRatio.get(),
  351. }}
  352. >
  353. <View
  354. style={{
  355. flex: 6,
  356. flexDirection: 'row',
  357. justifyContent: 'space-between',
  358. paddingHorizontal: 10,
  359. }}
  360. >
  361. {this.state.edited ? (
  362. <View style={{ justifyContent: 'center', marginBottom: 5 }}>
  363. <Checkbox
  364. checkBoxColor={'#CCC'}
  365. checked={this.state.secectAll}
  366. size={22}
  367. label={
  368. <Text
  369. style={{
  370. fontSize: 14,
  371. lineHeight: 20,
  372. color: '#333',
  373. }}
  374. >
  375. 全选
  376. </Text>
  377. }
  378. onChange={bool =>
  379. this.setState({
  380. secectAll: bool,
  381. change: true,
  382. })
  383. }
  384. />
  385. </View>
  386. ) : null}
  387. {this.state.edited ? (
  388. <TouchableOpacity style={{ alignSelf: 'center' }}
  389. onPress={
  390. () => {
  391. if (this.toast) {
  392. Toast.hide(this.toast)
  393. }
  394. console.log(ShopList);
  395. if (!this.state.Loading && ShopList && ShopList.length > 0) {
  396. this.delSelectCart()
  397. } else {
  398. this.toast = Toast.show('购物车为空!', {
  399. position: toastHeight,
  400. })
  401. }
  402. }
  403. }
  404. >
  405. <Text
  406. style={{
  407. fontSize: 14,
  408. lineHeight: 20,
  409. color: '#333',
  410. }}
  411. >
  412. 删除选中商品
  413. </Text>
  414. </TouchableOpacity>
  415. ) : (
  416. <Text
  417. style={{
  418. alignSelf: 'center',
  419. fontSize: 14,
  420. lineHeight: 20,
  421. color: '#333',
  422. }}
  423. >
  424. 合计:<Text
  425. style={{
  426. fontSize: 16,
  427. lineHeight: 20,
  428. color: '#E14C46',
  429. }}
  430. >
  431. {CURRENCY.currencySign}
  432. {ScaleUtil(this.state.Amount, CURRENCY.currencyAmountScale)}
  433. </Text>
  434. </Text>
  435. )}
  436. </View>
  437. {this.state.edited ? (
  438. <TouchableOpacity
  439. onPress={
  440. async () => {
  441. if (this.toast) {
  442. Toast.hide(this.toast)
  443. }
  444. if (!this.state.Loading) {
  445. this.clearCart()
  446. } else {
  447. this.toast = Toast.show('购物车为空!', {
  448. position: toastHeight,
  449. })
  450. }
  451. }
  452. }
  453. style={{
  454. flex: 3,
  455. justifyContent: 'center',
  456. }}
  457. >
  458. <Text
  459. style={{
  460. alignSelf: 'center',
  461. fontSize: 14,
  462. lineHeight: 20,
  463. color: '#333',
  464. }}
  465. >
  466. 清空购物车
  467. </Text>
  468. </TouchableOpacity>
  469. ) : (
  470. <TouchableOpacity
  471. activeOpacity={this.state.Loading ? 1 : 0}
  472. onPress={() => {
  473. if (this.toast) {
  474. Toast.hide(this.toast)
  475. }
  476. if (!this.state.Loading && ShopList && ShopList.length > 0) {
  477. console.log(ShopList);
  478. this.mdl._settleTou()
  479. } else {
  480. this.toast = Toast.show('购物车为空!', {
  481. position: toastHeight,
  482. })
  483. }
  484. // ? null
  485. // : ShopList && ShopList.length > 0
  486. // ? this.mdl._settleTou()
  487. // : alert("购物车为空!")
  488. }}
  489. style={{
  490. flex: 3,
  491. backgroundColor:
  492. ShopList && ShopList.length > 0 ? '#E70013' : '#969696',
  493. justifyContent: 'center',
  494. }}
  495. >
  496. {this.state.Loading ? (
  497. <ActivityIndicator />
  498. ) : (
  499. <Text
  500. style={{
  501. alignSelf: 'center',
  502. fontSize: 14,
  503. lineHeight: 20,
  504. color: '#fff',
  505. }}
  506. >
  507. 结算
  508. </Text>
  509. )}
  510. </TouchableOpacity>
  511. )}
  512. </View>
  513. </View>
  514. )
  515. }
  516. }
  517. const styles = StyleSheet.create({
  518. container: {
  519. flex: 1,
  520. },
  521. icon: {
  522. width: 32,
  523. height: 32,
  524. },
  525. })
  526. export default ShoppingCartHome