ShoppingCart.js 20 KB

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