ShoppingCartHome.js 14 KB

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