ShoppingCartHome.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. Amount: 0
  165. })
  166. }
  167. render() {
  168. const { appTheme, ShopList, TotalNum } = this.props
  169. return (
  170. <View
  171. {...this.panResponder.panHandlers}
  172. style={[
  173. styles.container,
  174. { backgroundColor: appTheme.backgroundColor },
  175. ]}
  176. >
  177. <StatusBar
  178. animated={true}
  179. barStyle={appTheme.barStyle}
  180. // barStyle={"dark-content"}
  181. backgroundColor={'transparent'}
  182. translucent={true}
  183. />
  184. {/* 头部 */}
  185. <View
  186. style={{
  187. height: HEADERSTYLE.height,
  188. paddingTop: HEADERSTYLE.paddingTop + 5,
  189. backgroundColor: '#fff',
  190. }}
  191. >
  192. <View
  193. style={{
  194. flex: 1,
  195. justifyContent: 'center',
  196. }}
  197. >
  198. {this.props.navigation.state.params == 'otherToShop' ? (
  199. <TouchableOpacity
  200. style={{ position: 'absolute', left: 10, paddingTop: 5 }}
  201. onPress={() => this.props.dispatch(NavigationActions.back())}
  202. >
  203. <Icon name="icon-icon-fanhui" size={24} color={'#666'} />
  204. </TouchableOpacity>
  205. ) : null}
  206. <Text
  207. style={{
  208. alignSelf: 'center',
  209. fontSize: 18,
  210. lineHeight: 25,
  211. letterSpacing: 0.22,
  212. color: '#333',
  213. }}
  214. >
  215. 购物车
  216. </Text>
  217. {ShopList && ShopList.length > 0 ? (
  218. <TouchableOpacity
  219. style={{ position: "absolute", right: 10 }}
  220. onPress={() => {
  221. this.setState({
  222. edited: !this.state.edited,
  223. secectAll: false,
  224. change: false
  225. });
  226. this.initThis();
  227. }}
  228. >
  229. <Text
  230. style={{
  231. fontSize: 14,
  232. lineHeight: 20,
  233. letterSpacing: 0.17,
  234. color: "#666"
  235. }}
  236. >
  237. {this.state.edited ? '完成' : '编辑'}
  238. </Text>
  239. </TouchableOpacity>
  240. ) : null}
  241. </View>
  242. </View>
  243. {ShopList && ShopList.length > 0 ? (
  244. <ScrollView
  245. style={{ flex: 1 }}
  246. showsHorizontalScrollIndicator={false}
  247. refreshControl={
  248. <RefreshControl
  249. refreshing={this.props.isRefreshing}
  250. onRefresh={this.onRefresh.bind(this)} //(()=>this.onRefresh)或者通过bind来绑定this引用来调用方法
  251. tintColor="red"
  252. title={this.props.isRefreshing ? '刷新中....' : '下拉刷新'}
  253. />
  254. }
  255. >
  256. <ShoppingCart
  257. onRefresh={this.onRefresh.bind(this)}
  258. pressLoading={e => this.setLoading(e)}
  259. ref={mdl => (this.mdl = mdl)}
  260. dispatch={this.props.dispatch}
  261. ShopList={ShopList}
  262. secectAll={this.state.secectAll}
  263. edited={this.state.edited}
  264. change={this.state.change}
  265. totalnum={TotalNum}
  266. secectOnchange={bool => {
  267. this.setState({ secectAll: bool, change: false })
  268. }}
  269. calAmount={e => this.calAmount(e)}
  270. addThis={e => {
  271. let ShopThis = this,
  272. itemData = []
  273. if (ShopThis._this.length == 0) {
  274. ShopThis._this.push(e)
  275. } else {
  276. for (let i in ShopThis._this) {
  277. itemData.push(ShopThis._this[i].state.itemData)
  278. }
  279. if (e && itemData.indexOf(e.state.itemData) == -1) {
  280. ShopThis._this.push(e)
  281. }
  282. }
  283. if (
  284. ShopThis._this.length == 2 &&
  285. ShopThis._this[0].interactableElem
  286. ) {
  287. ShopThis._this[0].interactableElem.snapTo({ index: 0 })
  288. ShopThis._this.splice(0, 1)
  289. }
  290. }}
  291. />
  292. </ScrollView>
  293. ) : (
  294. <ScrollView
  295. style={{ flex: 1 }}
  296. showsHorizontalScrollIndicator={false}
  297. refreshControl={
  298. <RefreshControl
  299. refreshing={this.props.isRefreshing}
  300. onRefresh={this.onRefresh.bind(this)} //(()=>this.onRefresh)或者通过bind来绑定this引用来调用方法
  301. tintColor="red"
  302. title={this.props.isRefreshing ? '刷新中....' : '下拉刷新'}
  303. />
  304. }
  305. >
  306. <ImageBackground
  307. resizeMode="contain"
  308. source={EmptyShop}
  309. style={{
  310. flex: 1,
  311. width: width - 80,
  312. height: height / 1.5,
  313. alignSelf: 'center',
  314. }}
  315. >
  316. <View
  317. style={{
  318. position: 'absolute',
  319. bottom: '15%',
  320. alignSelf: 'center',
  321. }}
  322. >
  323. <Text
  324. style={{
  325. fontSize: 14,
  326. lineHeight: 20,
  327. letterSpacing: 0.17,
  328. color: '#999',
  329. }}
  330. >
  331. 购物车是空的哦~
  332. </Text>
  333. </View>
  334. </ImageBackground>
  335. </ScrollView>
  336. )}
  337. {/* 底部 */}
  338. <View
  339. style={{
  340. height:
  341. this.props.navigation.state.params == 'otherToShop'
  342. ? 45 + HEADERSTYLE.paddingBottom
  343. : 45,
  344. paddingBottom:
  345. this.props.navigation.state.params == 'otherToShop'
  346. ? HEADERSTYLE.paddingBottom
  347. : 0,
  348. backgroundColor: '#fff',
  349. flexDirection: 'row',
  350. borderTopColor: '#EEE',
  351. borderTopWidth: 1 / PixelRatio.get(),
  352. }}
  353. >
  354. <View
  355. style={{
  356. flex: 6,
  357. flexDirection: 'row',
  358. justifyContent: 'space-between',
  359. paddingHorizontal: 10,
  360. }}
  361. >
  362. {this.state.edited ? (
  363. <View style={{ justifyContent: 'center', marginBottom: 5 }}>
  364. <Checkbox
  365. checkBoxColor={'#CCC'}
  366. checked={this.state.secectAll}
  367. size={22}
  368. label={
  369. <Text
  370. style={{
  371. fontSize: 14,
  372. lineHeight: 20,
  373. color: '#333',
  374. }}
  375. >
  376. 全选
  377. </Text>
  378. }
  379. onChange={bool =>
  380. this.setState({
  381. secectAll: bool,
  382. change: true,
  383. })
  384. }
  385. />
  386. </View>
  387. ) : null}
  388. {this.state.edited ? (
  389. <TouchableOpacity style={{ alignSelf: 'center' }}
  390. onPress={
  391. () => {
  392. if (this.toast) {
  393. Toast.hide(this.toast)
  394. }
  395. console.log(ShopList);
  396. if (!this.state.Loading && ShopList && ShopList.length > 0) {
  397. this.delSelectCart()
  398. } else {
  399. this.toast = Toast.show('购物车为空!', {
  400. position: toastHeight,
  401. })
  402. }
  403. }
  404. }
  405. >
  406. <Text
  407. style={{
  408. fontSize: 14,
  409. lineHeight: 20,
  410. color: '#333',
  411. }}
  412. >
  413. 删除选中商品
  414. </Text>
  415. </TouchableOpacity>
  416. ) : (
  417. <Text
  418. style={{
  419. alignSelf: 'center',
  420. fontSize: 14,
  421. lineHeight: 20,
  422. color: '#333',
  423. }}
  424. >
  425. 合计:<Text
  426. style={{
  427. fontSize: 16,
  428. lineHeight: 20,
  429. color: '#E14C46',
  430. }}
  431. >
  432. {CURRENCY.currencySign}
  433. {ScaleUtil(this.state.Amount, CURRENCY.currencyAmountScale)}
  434. </Text>
  435. </Text>
  436. )}
  437. </View>
  438. {this.state.edited ? (
  439. <TouchableOpacity
  440. onPress={
  441. async () => {
  442. if (this.toast) {
  443. Toast.hide(this.toast)
  444. }
  445. if (!this.state.Loading) {
  446. this.clearCart()
  447. } else {
  448. this.toast = Toast.show('购物车为空!', {
  449. position: toastHeight,
  450. })
  451. }
  452. }
  453. }
  454. style={{
  455. flex: 3,
  456. justifyContent: 'center',
  457. }}
  458. >
  459. <Text
  460. style={{
  461. alignSelf: 'center',
  462. fontSize: 14,
  463. lineHeight: 20,
  464. color: '#333',
  465. }}
  466. >
  467. 清空购物车
  468. </Text>
  469. </TouchableOpacity>
  470. ) : (
  471. <TouchableOpacity
  472. activeOpacity={this.state.Loading ? 1 : 0}
  473. onPress={() => {
  474. if (this.toast) {
  475. Toast.hide(this.toast)
  476. }
  477. if (!this.state.Loading && ShopList && ShopList.length > 0) {
  478. console.log(ShopList);
  479. this.mdl._settleTou()
  480. } else {
  481. this.toast = Toast.show('购物车为空!', {
  482. position: toastHeight,
  483. })
  484. }
  485. // ? null
  486. // : ShopList && ShopList.length > 0
  487. // ? this.mdl._settleTou()
  488. // : alert("购物车为空!")
  489. }}
  490. style={{
  491. flex: 3,
  492. backgroundColor:
  493. ShopList && ShopList.length > 0 ? '#E70013' : '#969696',
  494. justifyContent: 'center',
  495. }}
  496. >
  497. {this.state.Loading ? (
  498. <ActivityIndicator />
  499. ) : (
  500. <Text
  501. style={{
  502. alignSelf: 'center',
  503. fontSize: 14,
  504. lineHeight: 20,
  505. color: '#fff',
  506. }}
  507. >
  508. 结算
  509. </Text>
  510. )}
  511. </TouchableOpacity>
  512. )}
  513. </View>
  514. </View>
  515. )
  516. }
  517. }
  518. const styles = StyleSheet.create({
  519. container: {
  520. flex: 1,
  521. },
  522. icon: {
  523. width: 32,
  524. height: 32,
  525. },
  526. })
  527. export default ShoppingCartHome