OrderGifts.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. import React, { Component } from 'react'
  2. import {
  3. StyleSheet,
  4. ScrollView,
  5. View,
  6. Text,
  7. StatusBar,
  8. TouchableOpacity,
  9. PixelRatio,
  10. Dimensions,
  11. Image,
  12. SectionList,
  13. } from 'react-native'
  14. import { connect } from 'react-redux'
  15. import Checkbox from '../../../components/checkbox/index'
  16. import CountNum from '../../../components/CountNum'
  17. import Icon from '../../../components/Iconfont/Iconfont'
  18. import { NavigationActions, createAction } from '../../../utils'
  19. import { accMul, accDiv, accAdd, accSub } from '../../../utils/utils'
  20. import png from '../../../static/images/defaultimg.jpg'
  21. import { ImageBaseUrl } from '../../../utils/fetch/Fetchx'
  22. const { width, height } = Dimensions.get('window')
  23. @connect(({ theme }) => ({ ...theme }))
  24. class OrderGifts extends Component {
  25. constructor(props) {
  26. super(props)
  27. this.state = {
  28. giftProms: [],
  29. }
  30. }
  31. componentDidMount = () => {
  32. const { giftProms } = JSON.parse(
  33. JSON.stringify(this.props.navigation.state.params)
  34. )
  35. giftProms.forEach(element => {
  36. element.data.map(item => {
  37. item.orderNum = item.goodsAmout
  38. item.mainNum = item.goodsAmout
  39. })
  40. })
  41. this.setState({ giftProms: giftProms })
  42. }
  43. // 头部
  44. header() {
  45. return (
  46. <View
  47. style={{
  48. height: HEADERSTYLE.height,
  49. paddingTop: HEADERSTYLE.paddingTop + 5,
  50. backgroundColor: '#fff',
  51. borderBottomColor: '#eee',
  52. borderBottomWidth: 1 / PixelRatio.get(),
  53. }}
  54. >
  55. <View
  56. style={{
  57. flex: 1,
  58. justifyContent: 'center',
  59. }}
  60. >
  61. <TouchableOpacity
  62. style={{ position: 'absolute', left: 10, flexDirection: 'row' }}
  63. onPress={() => this.props.dispatch(NavigationActions.back())}
  64. >
  65. <Icon
  66. name="icon-icon-fanhui"
  67. size={20}
  68. color={'#666'}
  69. style={{ marginTop: 4 }}
  70. />
  71. <Text
  72. style={{
  73. fontSize: 14,
  74. lineHeight: 20,
  75. color: '#666',
  76. alignSelf: 'center',
  77. }}
  78. >
  79. 返回
  80. </Text>
  81. </TouchableOpacity>
  82. <Text
  83. style={{
  84. alignSelf: 'center',
  85. fontSize: 18,
  86. lineHeight: 25,
  87. letterSpacing: 0.19,
  88. color: '#333',
  89. }}
  90. >
  91. 赠品
  92. </Text>
  93. </View>
  94. </View>
  95. )
  96. }
  97. // 底部按钮
  98. bottomTouch() {
  99. let touchArr = [],
  100. touchAtt = [
  101. {
  102. touchName: '取消',
  103. background: '#FFF',
  104. textColor: '#666',
  105. itemonpress: () => {
  106. this.props.dispatch(NavigationActions.back())
  107. // this.state.giftProms.forEach(element => {
  108. // element.map(item => delete item.isChecked);
  109. // });
  110. // this.props.dispatch(NavigationActions.back());
  111. },
  112. },
  113. {
  114. touchName: '确定',
  115. background: '#E70013',
  116. textColor: '#FFF',
  117. itemonpress: () => {
  118. let giftReturn = []
  119. this.state.giftProms.forEach(element => {
  120. element.data.map(item => {
  121. if (
  122. item &&
  123. Object.keys(item).indexOf('isChecked') != -1 &&
  124. item.isChecked
  125. ) {
  126. delete item.isChecked
  127. delete item.editable
  128. delete item.touchSpan
  129. delete item.touchDisabledAdd
  130. item.reqOrderPromRels = [
  131. {
  132. ruleId: element.ruleId,
  133. ruleCode: element.ruleCode,
  134. ruleName: element.ruleName,
  135. activityId: element.activityId,
  136. activityCode: element.activityCode,
  137. activityName: element.activityName,
  138. giftId: item.giftId,
  139. description: element.description,
  140. combineType: element.combineType, // 组合类型 1 固定组合
  141. goodCombineNum: element.goodCombineNum, // 固定组合类商品基准数量
  142. promWay: 1, // 1 买赠 2 降价
  143. isWhole: 0,
  144. },
  145. ]
  146. giftReturn.push(item)
  147. }
  148. })
  149. })
  150. this.props.navigation.state.params.giftCb(giftReturn)
  151. this.props.dispatch(NavigationActions.back())
  152. },
  153. },
  154. ]
  155. for (let i = 0; i < touchAtt.length; i++) {
  156. touchArr.push(
  157. <TouchableOpacity
  158. onPress={() => touchAtt[i].itemonpress()}
  159. key={i}
  160. style={{
  161. flex: 1,
  162. backgroundColor: touchAtt[i].background,
  163. justifyContent: 'center',
  164. borderRightColor: '#eee',
  165. borderRightWidth: 1 / PixelRatio.get(),
  166. }}
  167. >
  168. <Text
  169. style={{
  170. fontSize: 14,
  171. lineHeight: 20,
  172. color: touchAtt[i].textColor,
  173. alignSelf: 'center',
  174. }}
  175. >
  176. {touchAtt[i].touchName}
  177. </Text>
  178. </TouchableOpacity>
  179. )
  180. }
  181. return touchArr
  182. }
  183. // 列表头部
  184. Header(section) {
  185. let promWay = section.promWay == 1 ? '买赠' : '降价'
  186. return (
  187. <View
  188. style={{
  189. marginTop: 10,
  190. backgroundColor: '#FFF',
  191. padding: 10,
  192. flexDirection: 'row',
  193. borderBottomColor: '#EEE',
  194. borderBottomWidth: 1 / PixelRatio.get(),
  195. }}
  196. >
  197. <View
  198. style={{
  199. paddingVertical: 1,
  200. paddingHorizontal: 4,
  201. borderRadius: 100,
  202. backgroundColor: '#E70013',
  203. }}
  204. >
  205. <Text style={{ fontSize: 12, lineHeight: 14, color: '#FFF' }}>
  206. {promWay}
  207. </Text>
  208. </View>
  209. <Text
  210. numberOfLines={1}
  211. style={{
  212. flex: 1,
  213. fontSize: 12,
  214. lineHeight: 17,
  215. color: '#333',
  216. marginLeft: 10,
  217. }}
  218. >
  219. {section.description}
  220. </Text>
  221. </View>
  222. )
  223. }
  224. ListItem(item) {
  225. console.log(item);
  226. return (
  227. <View
  228. style={{
  229. backgroundColor: '#FFF',
  230. flex: 1,
  231. paddingTop: 10,
  232. paddingLeft: 5,
  233. paddingRight: 10,
  234. flexDirection: 'row',
  235. }}
  236. >
  237. <Checkbox
  238. checkBoxColor={'#CCC'}
  239. checked={item.isChecked}
  240. size={28}
  241. onChange={bool => this.checkBoxCb(item, bool)}
  242. />
  243. <View
  244. style={{
  245. flex: 1,
  246. marginLeft: 6,
  247. flexDirection: 'row',
  248. borderBottomColor: '#EEE',
  249. borderBottomWidth: 1 / PixelRatio.get(),
  250. paddingBottom: 10,
  251. }}
  252. >
  253. <Image
  254. style={{ width: 80, height: 80, marginTop: 5 }}
  255. source={item.goodsImg ? { uri: ImageBaseUrl + item.goodsImg } : png}
  256. resizeMode="cover"
  257. />
  258. <View
  259. style={{
  260. flex: 1,
  261. marginLeft: 10,
  262. }}
  263. >
  264. <Text
  265. style={{ flex: 1, fontSize: 13, lineHeight: 18, color: '#333' }}
  266. >
  267. {item.goodsDisplayName}
  268. </Text>
  269. <View style={{ alignSelf: 'flex-end' }}>
  270. <CountNum
  271. defaultValue={item.orderNum}
  272. size={28}
  273. touchSpan={item.touchSpan || 1}
  274. editable={true}
  275. touchDisabledAdd={item.touchDisabledAdd}
  276. maxValue={accDiv(item.goodsAmout, item.conversionRate)}
  277. minValue={item.goodCombineNum ? item.goodCombineNum : 1}
  278. callback={nv => this.countNumCb(item, nv)}
  279. />
  280. </View>
  281. </View>
  282. </View>
  283. </View>
  284. )
  285. }
  286. // 数量变化函数
  287. countNumCb(item, nv) {
  288. let addFlag = true
  289. if (item.combineType == 1) {
  290. if (accMul(nv, item.conversionRate) > item.goodsAmout) {
  291. item.orderNum = item.goodsAmout
  292. item.mainNum = accMul(item.orderNum, item.conversionRate)
  293. } else {
  294. if (item.isChecked) {
  295. if (item.combineType && item.combineType == 1 && nv > item.orderNum) {
  296. this.state.giftProms.forEach(element =>
  297. element.data.map(value => {
  298. if (value.giftId == item.giftId) {
  299. value.orderNum = accAdd(value.orderNum, value.goodCombineNum)
  300. value.mainNum = accMul(value.orderNum, value.conversionRate)
  301. if (value.mainNum >= value.goodsAmout) {
  302. value.touchDisabledAdd = true
  303. addFlag = false
  304. }
  305. }
  306. })
  307. )
  308. } else {
  309. this.state.giftProms.forEach(element =>
  310. element.data.map(value => {
  311. if (value.giftId == item.giftId) {
  312. value.orderNum = accSub(value.orderNum, value.goodCombineNum)
  313. if (value.orderNum <= value.goodCombineNum) {
  314. value.orderNum = value.goodCombineNum
  315. }
  316. value.mainNum = accMul(value.orderNum, value.conversionRate)
  317. value.touchDisabledAdd = false
  318. }
  319. })
  320. )
  321. addFlag = false
  322. }
  323. }
  324. }
  325. }
  326. if (addFlag) {
  327. item.orderNum = nv
  328. item.mainNum = accMul(item.orderNum, item.conversionRate)
  329. }
  330. this.setState({ giftProms: this.state.giftProms })
  331. }
  332. // 勾选框变化函数
  333. checkBoxCb(item, bool) {
  334. item.isChecked = bool
  335. if (item.combineType == 1) {
  336. if (item.isChecked) {
  337. item.orderNum = item.goodCombineNum
  338. item.mainNum = accMul(item.orderNum, item.conversionRate)
  339. item.touchSpan = item.goodCombineNum
  340. item.editable = false
  341. this.forGiftProm(item, true)
  342. } else {
  343. item.orderNum = 1
  344. item.touchSpan = 1
  345. this.forGiftProm(item, false)
  346. }
  347. this.setState({ giftProms: this.state.giftProms })
  348. }
  349. }
  350. // 遍历所有商品
  351. forGiftProm(item, bool) {
  352. this.state.giftProms.forEach(element =>
  353. element.data.map(value => {
  354. if (value.giftId == item.giftId) {
  355. value.isChecked = bool
  356. value.orderNum = bool ? value.goodCombineNum || 1 : 1
  357. value.mainNum = accMul(value.orderNum, value.conversionRate)
  358. value.touchSpan = bool ? value.goodCombineNum || 1 : 1
  359. value.editable = false
  360. }
  361. })
  362. )
  363. }
  364. render() {
  365. const { appTheme } = this.props
  366. return (
  367. <View
  368. style={[
  369. styles.container,
  370. { backgroundColor: appTheme.backgroundColor },
  371. ]}
  372. >
  373. <StatusBar
  374. animated={true}
  375. barStyle={appTheme.barStyle}
  376. // barStyle={"dark-content"}
  377. backgroundColor={'transparent'}
  378. translucent={true}
  379. />
  380. {/* 头部 */}
  381. {this.header()}
  382. {/* 列表 */}
  383. <SectionList
  384. keyExtractor={(item, index) => index}
  385. extraData={this.state}
  386. renderItem={({ item, index }) => this.ListItem(item, index)}
  387. stickySectionHeadersEnabled={true}
  388. renderSectionHeader={({ section }) => this.Header(section)}
  389. sections={this.state.giftProms}
  390. />
  391. <View
  392. style={{
  393. height: 10,
  394. width: width,
  395. position: 'absolute',
  396. top: 60,
  397. backgroundColor: appTheme.backgroundColor,
  398. }}
  399. />
  400. {/* 底部 */}
  401. <View
  402. style={{
  403. height: 45,
  404. backgroundColor: '#FFF',
  405. flexDirection: 'row',
  406. }}
  407. >
  408. {this.bottomTouch()}
  409. </View>
  410. </View>
  411. )
  412. }
  413. }
  414. const styles = StyleSheet.create({
  415. container: {
  416. flex: 1,
  417. },
  418. })
  419. export default OrderGifts