ShoppingCart.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. import React, { Component } from 'react'
  2. import {
  3. View,
  4. Image,
  5. Text,
  6. PixelRatio,
  7. SectionList,
  8. TouchableOpacity,
  9. ImageBackground,
  10. } from 'react-native'
  11. import CountNum from '../../components/CountNum'
  12. import Checkbox from '../../components/checkbox/index'
  13. import png from '../../static/images/defaultimg.jpg'
  14. import Row from './SwipeList'
  15. import Collapsible from 'react-native-collapsible'
  16. import Icon from '../../components/Iconfont/Iconfont'
  17. import dropDown from '../../static/images/shop-dropdown.png'
  18. import { NavigationActions, createAction } from '../../utils'
  19. import { ScaleUtil, accMul } from '../../utils/utils'
  20. import { ImageBaseUrl } from '../../utils/fetch/Fetchx'
  21. import Toast from 'react-native-root-toast'
  22. class ShoppingCart extends Component {
  23. constructor(props) {
  24. super(props)
  25. this.state = {
  26. // data: this.props.ShopList,
  27. sectionActive: 'aaasd',
  28. reflish: true,
  29. deleteId: '',
  30. selectSup: '',
  31. clickNum: 0,
  32. }
  33. }
  34. checkStockNum(data) {
  35. lessStock = []
  36. data.map(item => {
  37. if (item.stock < 1) {
  38. lessStock.push(item.goodsDisplayName)
  39. }
  40. })
  41. return lessStock
  42. }
  43. async _settleTou() {
  44. let newShopData = JSON.parse(JSON.stringify(this.props.ShopList)),
  45. editData = [],
  46. // 供应商或销售商
  47. SaleOrSupplier = {}
  48. const cartIds = []
  49. for (val of newShopData) {
  50. for (valSec of val.data) {
  51. if (valSec.isChecked) {
  52. editData.push(valSec)
  53. }
  54. }
  55. }
  56. if (this.toast) {
  57. Toast.hide(this.toast)
  58. }
  59. if (editData && editData.length > 0) {
  60. if (this.checkStockNum(editData).length > 0) {
  61. let lessStockName = this.checkStockNum(editData).join()
  62. this.toast = Toast.show(
  63. `您所选的商品: ${lessStockName}\n库存不足,请重新选择!`,
  64. { position: toastHeight }
  65. )
  66. } else {
  67. // 测试时 如果没有价格 加个 editData[0].salePrice =100
  68. for (data of editData) {
  69. cartIds.push(data.id)
  70. delete data.isChecked
  71. data.id = null
  72. data.promPrice = data.salePrice
  73. data.dealPrice = data.salePrice
  74. data.isGift = 0
  75. if (!data.goodsName) {
  76. data.goodsName = data.goodsDisplayName
  77. } else if (!data.goodsDisplayName) {
  78. data.goodsDisplayName = data.goodsName
  79. }
  80. // data.currency = currency.id;
  81. // data.currencySign = currency.currencySign;
  82. // data.currencyPriceScale = currency.currencyPriceScale || 2; // 币种单价精度
  83. // data.currencyAmountScale = currency.currencyAmountScale || 2; // 币种金额精度
  84. // data.stock = data.stockNum;
  85. data.mainNum = accMul(data.orderNum, data.conversionRate)
  86. data.amount = accMul(data.salePrice, data.orderNum)
  87. data.dealAmount = accMul(data.salePrice, data.orderNum)
  88. }
  89. SaleOrSupplier.SaleOrSupplierId =
  90. editData[0].saleOrgId || editData[0].supplierId
  91. SaleOrSupplier.SaleOrSupplierName =
  92. editData[0].saleOrgName || editData[0].supplierName
  93. SaleOrSupplier.SaleOrSupplierCode =
  94. editData[0].saleOrgCode || editData[0].supplierCode
  95. SaleOrSupplier.isPrimaryChannel = editData[0].isPrimaryChannel
  96. this.props.pressLoading(true)
  97. // 促销服务请求
  98. await this.props.dispatch(
  99. createAction('orderedit/getProm')({
  100. promInfo: {
  101. customerId: CUSTOMERINFO.id,
  102. saleOrgId: SaleOrSupplier.SaleOrSupplierId,
  103. reqOrderItems: editData,
  104. isPrimaryChannel: SaleOrSupplier.isPrimaryChannel,
  105. },
  106. })
  107. )
  108. this.props.pressLoading(false)
  109. this.props.dispatch(
  110. NavigationActions.navigate({
  111. routeName: 'OrderEdit',
  112. params: {
  113. ShopData: editData,
  114. SaleOrSupplier: SaleOrSupplier,
  115. cartId: cartIds,
  116. },
  117. })
  118. )
  119. }
  120. } else {
  121. this.toast = Toast.show('请选择商品再结算!', { position: toastHeight })
  122. }
  123. }
  124. componentWillReceiveProps(nextProps) {
  125. if (nextProps.edited !== this.props.edited) {
  126. for (val of this.props.ShopList) {
  127. val.isChecked = false
  128. for (valSec of val.data) {
  129. valSec.isChecked = false
  130. }
  131. }
  132. }
  133. if (nextProps.change) {
  134. if (nextProps.secectAll) {
  135. for (let val of this.props.ShopList) {
  136. val.isChecked = true
  137. val.data.map(item => {
  138. item.isChecked = true
  139. })
  140. }
  141. this.setState({ reflish: !this.state.reflish })
  142. } else {
  143. for (let val of this.props.ShopList) {
  144. val.isChecked = false
  145. val.data.map(item => {
  146. item.isChecked = false
  147. })
  148. }
  149. this.setState({ reflish: !this.state.reflish })
  150. }
  151. }
  152. }
  153. Header(section) {
  154. return (
  155. <View>
  156. <View
  157. style={{
  158. height: 10,
  159. backgroundColor: '#F5F5F5',
  160. borderBottomColor: '#ddd',
  161. borderBottomWidth: 1 / PixelRatio.get(),
  162. }}
  163. />
  164. <View
  165. style={{
  166. backgroundColor: '#FAFAFA',
  167. flexDirection: 'row',
  168. justifyContent: 'space-between',
  169. paddingRight: 10,
  170. paddingLeft: 5,
  171. paddingBottom: 5,
  172. }}
  173. >
  174. <Checkbox
  175. checked={section.isChecked}
  176. size={30}
  177. onChange={bool => {
  178. if (this.props.edited) {
  179. // 编辑状态下
  180. section.isChecked = bool
  181. if (section.isChecked) {
  182. for (let item of section.data) {
  183. item.isChecked = true
  184. }
  185. } else {
  186. for (let item of section.data) {
  187. item.isChecked = false
  188. }
  189. }
  190. if (
  191. this.props.ShopList.every(items => items.isChecked == true)
  192. ) {
  193. this.props.secectOnchange(true)
  194. } else {
  195. this.props.secectOnchange(false)
  196. }
  197. } else {
  198. // 非编辑状态下
  199. if (
  200. this.props.ShopList.some(
  201. item =>
  202. Object.keys(item).indexOf('isChecked') !== -1 &&
  203. item.isChecked == true
  204. )
  205. ) {
  206. for (val of this.props.ShopList) {
  207. val.isChecked = false
  208. for (secVal of val.data) {
  209. secVal.isChecked = false
  210. }
  211. }
  212. section.isChecked = bool
  213. } else {
  214. for (val of this.props.ShopList) {
  215. for (valsec of val.data) {
  216. if (valsec.isChecked) {
  217. if (valsec.saleOrgId !== section.saleOrgId) {
  218. valsec.isChecked = false
  219. }
  220. }
  221. }
  222. }
  223. section.isChecked = bool
  224. }
  225. this.props.calAmount(this.props.ShopList)
  226. }
  227. if (section.isChecked) {
  228. for (let item of section.data) {
  229. item.isChecked = true
  230. }
  231. } else {
  232. for (let item of section.data) {
  233. item.isChecked = false
  234. }
  235. }
  236. this.setState({
  237. selectSup: section.saleOrgId,
  238. })
  239. }}
  240. label={
  241. <View
  242. style={{
  243. justifyContent: 'center',
  244. paddingBottom: 4,
  245. marginLeft: 5,
  246. }}
  247. >
  248. <Text
  249. style={{
  250. fontSize: 14,
  251. lineHeight: 20,
  252. letterSpacing: 0.17,
  253. color: '#333',
  254. }}
  255. >
  256. {section.saleOrgName}
  257. </Text>
  258. </View>
  259. }
  260. />
  261. <TouchableOpacity
  262. onPress={() =>
  263. this.state.sectionActive !== section.saleOrgName
  264. ? this.setState({ sectionActive: section.saleOrgName })
  265. : this.setState({ sectionActive: 'aaaasd' })
  266. }
  267. style={{ alignSelf: 'center' }}
  268. >
  269. <Icon name="icon-icon-gouwuguolvpaixu" size={24} color="#999" />
  270. </TouchableOpacity>
  271. <View
  272. style={{
  273. position: 'absolute',
  274. top: 28,
  275. right: 3,
  276. backgroundColor: 'transparent',
  277. }}
  278. >
  279. <Collapsible
  280. collapsed={this.state.sectionActive !== section.saleOrgName}
  281. >
  282. <ImageBackground
  283. source={dropDown}
  284. resizeMode="stretch"
  285. style={{
  286. width: 130,
  287. paddingTop: 10,
  288. }}
  289. >
  290. <View
  291. style={{
  292. paddingVertical: 15,
  293. alignSelf: 'center',
  294. }}
  295. >
  296. <TouchableOpacity
  297. onPress={() => {
  298. section.data.sort((a, b) => a.goodsCode - b.goodsCode)
  299. this.setState({ sectionActive: 'aaasd' })
  300. }}
  301. style={{
  302. flexDirection: 'row',
  303. }}
  304. >
  305. <Text
  306. style={{ fontSize: 12, lineHeight: 17, color: '#333' }}
  307. >
  308. 按编码正序
  309. </Text>
  310. <Icon
  311. name="icon-icon-paixutubiao"
  312. style={{
  313. marginLeft: 10,
  314. transform: [{ rotate: '90deg' }],
  315. alignSelf: 'center',
  316. }}
  317. size={18}
  318. color={'#E70013'}
  319. />
  320. </TouchableOpacity>
  321. <TouchableOpacity
  322. onPress={() => {
  323. section.data.sort((a, b) => b.goodsCode - a.goodsCode)
  324. this.setState({ sectionActive: 'aaasd' })
  325. }}
  326. style={{
  327. marginTop: 14,
  328. flexDirection: 'row',
  329. }}
  330. >
  331. <Text
  332. style={{ fontSize: 12, lineHeight: 17, color: '#333' }}
  333. >
  334. 按编码倒序
  335. </Text>
  336. <Icon
  337. name="icon-icon-paixutubiao"
  338. style={{
  339. marginLeft: 10,
  340. alignSelf: 'center',
  341. transform: [{ rotate: '-90deg' }],
  342. }}
  343. size={18}
  344. color={'#E70013'}
  345. />
  346. </TouchableOpacity>
  347. </View>
  348. <TouchableOpacity
  349. onPress={() => {
  350. section.data.sort((a, b) => a.creationTime - b.creationTime)
  351. this.setState({ sectionActive: 'aaasd' })
  352. }}
  353. style={{
  354. paddingTop: 10,
  355. paddingBottom: 15,
  356. borderTopColor: '#EEE',
  357. borderTopWidth: 1 / PixelRatio.get(),
  358. }}
  359. >
  360. <Text
  361. style={{
  362. fontSize: 12,
  363. lineHeight: 17,
  364. color: '#999',
  365. alignSelf: 'center',
  366. }}
  367. >
  368. 取消排序
  369. </Text>
  370. </TouchableOpacity>
  371. </ImageBackground>
  372. </Collapsible>
  373. </View>
  374. </View>
  375. </View>
  376. )
  377. }
  378. ListItem(item, index) {
  379. return (
  380. <View
  381. style={{
  382. paddingBottom: 10,
  383. borderBottomWidth: 2,
  384. borderBottomColor: '#f5f5f5',
  385. }}
  386. >
  387. <Row
  388. item={item}
  389. itemData={item.id}
  390. customerId={item.customerId}
  391. dispatch={this.props.dispatch}
  392. addThis={e => this.props.addThis && this.props.addThis(e)}
  393. deleteTouch={e => {
  394. this.props.onRefresh()
  395. this.setState({ deleteId: e })
  396. }}
  397. >
  398. <View
  399. style={{
  400. flex: 1,
  401. flexDirection: 'row',
  402. paddingLeft: 6,
  403. paddingRight: 10,
  404. paddingTop: 10,
  405. }}
  406. >
  407. <Checkbox
  408. checkBoxColor={'#CCC'}
  409. checked={item.isChecked}
  410. size={28}
  411. onChange={bool => {
  412. if (this.props.edited) {
  413. // 编辑状态
  414. item.isChecked = bool
  415. for (let val of this.props.ShopList) {
  416. if (item.saleOrgId == val.saleOrgId) {
  417. if (val.data.every(item => item.isChecked == true)) {
  418. val.isChecked = true
  419. } else {
  420. val.isChecked = false
  421. }
  422. }
  423. }
  424. if (
  425. this.props.ShopList.every(
  426. itemsec => itemsec.isChecked == true
  427. )
  428. ) {
  429. this.props.secectOnchange(true)
  430. } else {
  431. this.props.secectOnchange(false)
  432. }
  433. } else {
  434. // 费编辑状态下
  435. if (this.state.clickNum == 0) {
  436. item.isChecked = bool
  437. }
  438. if (item.saleOrgId == this.state.selectSup) {
  439. item.isChecked = bool
  440. } else {
  441. for (val of this.props.ShopList) {
  442. val.isChecked = false
  443. for (secVal of val.data) {
  444. secVal.isChecked = false
  445. }
  446. }
  447. item.isChecked = bool
  448. }
  449. for (let valth of this.props.ShopList) {
  450. if (item.saleOrgName == valth.saleOrgName) {
  451. if (valth.data.every(item => item.isChecked == true)) {
  452. valth.isChecked = true
  453. } else {
  454. valth.isChecked = false
  455. }
  456. }
  457. }
  458. this.props.calAmount(this.props.ShopList)
  459. }
  460. this.setState({
  461. clickNum: this.state.clickNum + 1,
  462. selectSup: item.saleOrgId,
  463. })
  464. }}
  465. />
  466. <View
  467. style={{
  468. flex: 1,
  469. flexDirection: 'row',
  470. paddingLeft: 6,
  471. paddingBottom: 3,
  472. borderBottomColor: '#f5f5f5',
  473. borderBottomWidth: 1,
  474. }}
  475. >
  476. <Image
  477. style={{ width: 80, height: 80, marginTop: 5 }}
  478. source={{ uri: ImageBaseUrl + item.goodsImg } || png}
  479. resizeMode="cover"
  480. />
  481. <View style={{ flex: 1, marginLeft: 10 }}>
  482. <Text
  483. style={{
  484. fontSize: 13,
  485. lineHeight: 18,
  486. color: '#333',
  487. marginBottom: 3,
  488. }}
  489. >
  490. {item.goodsDisplayName}
  491. </Text>
  492. <Text
  493. style={{
  494. fontSize: 12,
  495. lineHeight: 17,
  496. color: '#999',
  497. marginBottom: 3,
  498. }}
  499. >
  500. 编码:{item.goodsCode}
  501. </Text>
  502. <Text
  503. style={{
  504. fontSize: 12,
  505. lineHeight: 17,
  506. color: '#999',
  507. marginBottom: 3,
  508. }}
  509. >
  510. {/* 型号:{item.model?(item.model.length>10?item.model.substr(0,10)+"...":item.model):""} */}
  511. 型号:{item.model?item.model:""}
  512. </Text>
  513. <Text
  514. style={{
  515. fontSize: 12,
  516. lineHeight: 17,
  517. color: '#999',
  518. marginBottom: 3,
  519. }}
  520. >
  521. 规格:{item.specification?(item.specification.split("/")[0]+"/"+item.mainNumUnitName):""}
  522. </Text>
  523. <Text
  524. style={{
  525. fontSize: 12,
  526. lineHeight: 17,
  527. color: '#999',
  528. marginBottom: 3,
  529. }}
  530. >
  531. 颜色:{item.baseGoodsOptValue}
  532. </Text>
  533. <View
  534. style={{
  535. flex: 1,
  536. flexDirection: 'row',
  537. justifyContent: 'space-between',
  538. }}
  539. >
  540. {/* <Text style={{ fontSize: 12, lineHeight: 17, color: '#999' }}>
  541. 库存:{item.stock} 件
  542. </Text> */}
  543. <Text
  544. style={{
  545. fontSize: 12,
  546. lineHeight: 17,
  547. color: '#333',
  548. }}
  549. >
  550. {CURRENCY.currencySign}
  551. {item.salePrice || item.basePrice}/件
  552. </Text>
  553. </View>
  554. </View>
  555. </View>
  556. </View>
  557. </Row>
  558. <View
  559. style={{
  560. flexDirection: 'row',
  561. paddingLeft: 33,
  562. paddingTop: 10,
  563. paddingRight: 10,
  564. }}
  565. >
  566. <CountNum
  567. defaultValue={item.orderNum}
  568. size={30}
  569. fontSize={18}
  570. callback={nv => {
  571. item.mainNum = accMul(item.orderNum, item.conversionRate)
  572. this.props.dispatch(
  573. createAction('shoppingcart/editShop')({
  574. item: item,
  575. cbNumber: nv,
  576. })
  577. )
  578. this.props.calAmount(this.props.ShopList)
  579. }}
  580. />
  581. <View
  582. style={{
  583. flex: 1,
  584. paddingLeft: 10,
  585. flexDirection: 'row',
  586. justifyContent: 'space-between',
  587. }}
  588. >
  589. <Text
  590. style={{
  591. fontSize: 12,
  592. lineHeight: 17,
  593. color: '#666',
  594. alignSelf: 'center',
  595. }}
  596. >
  597. 主数量:{accMul(item.orderNum, item.conversionRate)}
  598. {item.mainNumUnit}
  599. </Text>
  600. <Text
  601. style={{
  602. fontSize: 12,
  603. lineHeight: 17,
  604. color: '#666',
  605. alignSelf: 'center',
  606. }}
  607. >
  608. 金额:<Text style={{ fontSize: 14, color: '#E14C46' }}>
  609. {CURRENCY.currencySign}
  610. {item.salePrice
  611. ? accMul(item.orderNum, item.salePrice)
  612. : accMul(item.orderNum, item.basePrice || 0)}
  613. </Text>
  614. </Text>
  615. </View>
  616. </View>
  617. </View>
  618. )
  619. }
  620. render() {
  621. extradata = { ...this.state, ...this.props }
  622. return (
  623. <View
  624. style={{
  625. flex: 1,
  626. backgroundColor: '#fff',
  627. marginBottom: 2,
  628. }}
  629. >
  630. <SectionList
  631. keyExtractor={(item, index) => index}
  632. extraData={extradata}
  633. renderSectionHeader={({ section }) => this.Header(section)}
  634. renderItem={({ item, index }) => this.ListItem(item, index)}
  635. stickySectionHeadersEnabled={true}
  636. sections={this.props.ShopList}
  637. />
  638. </View>
  639. )
  640. }
  641. }
  642. export default ShoppingCart