Grid.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. import React, { Children } from 'react'
  2. import {
  3. StyleSheet,
  4. RefreshControl,
  5. View,
  6. Text,
  7. Dimensions,
  8. FlatList,
  9. Image,
  10. TouchableOpacity,
  11. PixelRatio,
  12. ActivityIndicator,
  13. } from 'react-native'
  14. import PropTypes from 'prop-types'
  15. import Icon from './Iconfont/Iconfont'
  16. import { ScaleUtil } from '../utils/utils'
  17. import { ImageBaseUrl } from '../utils/fetch/Fetchx'
  18. import { createAction } from '../utils'
  19. const { width, height } = Dimensions.get('window')
  20. export default class Grid extends React.Component {
  21. constructor(props) {
  22. super(props)
  23. this.state = {}
  24. }
  25. static propTypes = {
  26. titleName: PropTypes.string,
  27. titleColor: PropTypes.string,
  28. titleIcon: PropTypes.string,
  29. nameKey: PropTypes.string,
  30. pictureKey: PropTypes.string,
  31. priceKey: PropTypes.string,
  32. keyext: PropTypes.string,
  33. data: PropTypes.array,
  34. shoppingCart: PropTypes.func,
  35. imageTouch: PropTypes.func,
  36. moreTouch: PropTypes.func,
  37. titleBool: PropTypes.bool,
  38. listRowBool: PropTypes.bool,
  39. homeLIstBool: PropTypes.bool,
  40. keyextstr: PropTypes.string, //暂时
  41. SearchVal: PropTypes.object, //查询条件
  42. shopListIds: PropTypes.array,
  43. }
  44. static defaultProps = {
  45. titleName: '热卖促销',
  46. titleColor: '#333',
  47. titleIcon: 'icon-icon-jianjinzhishiqi',
  48. data: [],
  49. nameKey: 'name',
  50. pictureKey: 'pictureUrl',
  51. priceKey: 'salePrice',
  52. keyext: 'code',
  53. titleBool: true,
  54. listRowBool: false,
  55. homeLIstBool: false,
  56. viewWid: {
  57. width: width / 2 - 4,
  58. // height: width / 2 + 81
  59. },
  60. imgWid: {
  61. width: width / 2 - 4,
  62. height: width / 2 - 4,
  63. },
  64. shoppingCart: () => {},
  65. imageTouch: () => {},
  66. moreTouch: () => {},
  67. SearchVal: {},
  68. shopListIds: [],
  69. }
  70. _renderItemCol(item) {
  71. let gouwucheColor = '#666'
  72. if (this.props.shopListIds.indexOf(item.id) !== -1) {
  73. gouwucheColor = '#E70013'
  74. }
  75. // 首页显示
  76. if (this.props.homeLIstBool) {
  77. return (
  78. <View style={[{ marginBottom: 10 }, this.props.viewWid]}>
  79. <TouchableOpacity
  80. style={{ backgroundColor: '#FFF' }}
  81. onPress={() => {
  82. this.props.imageTouch(item)
  83. }}
  84. >
  85. <Image
  86. resizeMode="contain"
  87. source={{ uri: ImageBaseUrl + item[this.props.pictureKey] }}
  88. style={this.props.imgWid}
  89. />
  90. </TouchableOpacity>
  91. <View
  92. style={{
  93. flex: 1,
  94. paddingHorizontal: 10,
  95. backgroundColor: '#FFF',
  96. justifyContent: 'space-between',
  97. }}
  98. >
  99. <TouchableOpacity
  100. onPress={() => {
  101. this.props.imageTouch(item)
  102. }}
  103. >
  104. <Text
  105. numberOfLines={1}
  106. style={{ flex: 1, color: '#333', fontSize: 14, lineHeight: 20 }}
  107. >
  108. {
  109. item.goodsAttrVals&&item.goodsAttrVals.length?(<Text style={{backgroundColor: '#f85300',color:'white'}}>调</Text>):""
  110. }
  111. {item[this.props.nameKey]}
  112. </Text>
  113. <View
  114. style = {{
  115. flexDirection: 'row',
  116. justifyContent: 'space-between',
  117. }}>
  118. <Text style={{fontSize:12}}>
  119. {'型号:'+(item.model?(item.model.length>10?item.model.substr(0,10):item.model):"")}
  120. </Text>
  121. <Text style={{fontSize:12}}>
  122. {'规格:'+(item.specification?(item.specification.split("/")[0]+"/"+item.basicUnitName):"")}
  123. </Text>
  124. </View>
  125. </TouchableOpacity>
  126. <View
  127. style={{
  128. flexDirection: 'row',
  129. justifyContent: 'space-between',
  130. }}
  131. >
  132. <Text style={{ color: '#E70013', fontSize: 18, lineHeight: 25 }}>
  133. {CURRENCY.currencySign}
  134. {ScaleUtil(item[this.props.priceKey] || item.salePrice)}
  135. </Text>
  136. <TouchableOpacity
  137. onPress={() => {
  138. this.props.shoppingCart(item)
  139. }}
  140. >
  141. <Icon
  142. name="icon-icon-gouwuche"
  143. size={22}
  144. color={gouwucheColor}
  145. />
  146. </TouchableOpacity>
  147. </View>
  148. </View>
  149. </View>
  150. )
  151. } else {
  152. // 非首页 --- 宫格
  153. return (
  154. <View style={this.props.viewWid}>
  155. <TouchableOpacity
  156. style={{ backgroundColor: '#FFF' }}
  157. onPress={() => {
  158. this.props.imageTouch(item)
  159. }}
  160. >
  161. <Image
  162. resizeMode="contain"
  163. source={{ uri: ImageBaseUrl + item[this.props.pictureKey] }}
  164. style={this.props.imgWid}
  165. />
  166. </TouchableOpacity>
  167. <View
  168. style={{
  169. flex: 1,
  170. paddingHorizontal: 10,
  171. paddingTop: 15,
  172. backgroundColor: '#FFF',
  173. borderTopColor: '#F5F5F5',
  174. borderTopWidth: 1 / PixelRatio.get(),
  175. }}
  176. >
  177. <Text style={{ color: '#E14C46', fontSize: 12 }}>
  178. {CURRENCY.currencySign}
  179. <Text style={{ fontSize: 18 }}>
  180. {ScaleUtil(item[this.props.priceKey] || item.salePrice)}
  181. </Text>
  182. </Text>
  183. <Text
  184. numberOfLines={1}
  185. style={{
  186. flex: 1,
  187. color: '#333',
  188. fontSize: 13,
  189. lineHeight: 18,
  190. marginTop: 5,
  191. }}
  192. >
  193. {item[this.props.nameKey]}
  194. </Text>
  195. <Text style={{ color: '#666', fontSize: 12 }}>
  196. 编码:{item[this.props.codeKey]}
  197. </Text>
  198. <View
  199. style={{
  200. flexDirection: 'row',
  201. justifyContent: 'space-between',
  202. alignItems: 'flex-end',
  203. paddingBottom: 5,
  204. }}
  205. >
  206. {/* <Text style={{ color: '#666', fontSize: 12, marginBottom: 5 }}>
  207. 库存:{ScaleUtil(item[this.props.invenKey])}(台)
  208. </Text> */}
  209. <TouchableOpacity
  210. onPress={() => {
  211. this.props.shoppingCart(item)
  212. }}
  213. >
  214. <Icon
  215. name="icon-icon-gouwuche"
  216. size={22}
  217. color={gouwucheColor}
  218. />
  219. </TouchableOpacity>
  220. </View>
  221. </View>
  222. </View>
  223. )
  224. }
  225. }
  226. _renderItemRow(item) {
  227. let gouwucheColor = '#666'
  228. if (this.props.shopListIds.indexOf(item.id) !== -1) {
  229. gouwucheColor = '#E70013'
  230. }
  231. // 列表形式
  232. return (
  233. <View style={[this.props.viewWid, { flexDirection: 'row' }]}>
  234. <TouchableOpacity
  235. style={{ backgroundColor: '#FFF' }}
  236. onPress={() => {
  237. this.props.imageTouch(item)
  238. }}
  239. >
  240. <Image
  241. resizeMode="contain"
  242. source={{ uri: ImageBaseUrl + item[this.props.pictureKey] }}
  243. style={this.props.imgWid}
  244. />
  245. </TouchableOpacity>
  246. <View
  247. style={{
  248. flex: 1,
  249. flexDirection: 'column',
  250. justifyContent: 'center',
  251. backgroundColor: '#FFF',
  252. paddingVertical: 20,
  253. paddingLeft: 20,
  254. paddingRight: 10,
  255. }}
  256. >
  257. <Text
  258. numberOfLines={1}
  259. style={{ flex: 1, fontSize: 14, color: '#333', lineHeight: 20 }}
  260. >
  261. {
  262. item.goodsAttrVals&&item.goodsAttrVals.length?(<Text style={{backgroundColor: '#f85300',color:'white'}}>调</Text>):""
  263. }
  264. {item[this.props.nameKey]}
  265. </Text>
  266. <Text
  267. style={{
  268. fontSize: 13,
  269. color: '#999',
  270. lineHeight: 18,
  271. marginTop: 2,
  272. }}
  273. >
  274. 编码:{item[this.props.codeKey]}
  275. </Text>
  276. {/* <Text
  277. style={{
  278. fontSize: 13,
  279. color: '#999',
  280. lineHeight: 18,
  281. marginTop: 3,
  282. }}
  283. >
  284. 库存:{ScaleUtil(item[this.props.invenKey])}(件)
  285. </Text> */}
  286. {/* <View
  287. style = {{
  288. flexDirection: 'row',
  289. justifyContent: 'space-between',
  290. }}> */}
  291. <Text style={{
  292. fontSize: 13,
  293. color: '#999',
  294. lineHeight: 18,
  295. marginTop: 2,
  296. }}>
  297. {'型号:'+(item.model?(item.model.length>10?item.model.substr(0,10):item.model):"")}
  298. </Text>
  299. {/* </View> */}
  300. {/* <View
  301. style = {{
  302. flexDirection: 'row',
  303. justifyContent: 'space-between',
  304. }}> */}
  305. <Text style={{
  306. fontSize: 13,
  307. color: '#999',
  308. lineHeight: 18,
  309. marginTop: 2,
  310. }}>
  311. {'规格:'+(item.specification?(item.specification.split("/")[0]+"/"+item.basicUnitName):"")}
  312. </Text>
  313. {/* </View> */}
  314. <View
  315. style={{
  316. flexDirection: 'row',
  317. justifyContent: 'space-between',
  318. marginTop: 5,
  319. paddingRight: 7,
  320. }}
  321. >
  322. <Text style={{ fontSize: 18, lineHeight: 25, color: '#E70013' }}>
  323. {CURRENCY.currencySign}
  324. {ScaleUtil(item[this.props.priceKey] || item.salePrice)}
  325. </Text>
  326. <TouchableOpacity
  327. onPress={() => {
  328. this.props.shoppingCart(item)
  329. }}
  330. >
  331. <Icon name="icon-icon-gouwuche" color={gouwucheColor} size={22} />
  332. </TouchableOpacity>
  333. </View>
  334. </View>
  335. </View>
  336. )
  337. }
  338. // 底部
  339. footerCom() {
  340. if (this.props.Parentprops.showFoot == 0) {
  341. return <View />
  342. } else if (this.props.Parentprops.showFoot == 1) {
  343. return (
  344. <View style={styles.footer}>
  345. <Text
  346. style={{
  347. fontSize: 14,
  348. lineHeight: 20,
  349. letterSpacing: 0.17,
  350. color: '#999',
  351. }}
  352. >
  353. 没有更多数据
  354. </Text>
  355. </View>
  356. )
  357. } else if (this.props.Parentprops.showFoot == 2) {
  358. return (
  359. <View style={styles.footer}>
  360. <ActivityIndicator />
  361. <Text>正在加载更多数据...</Text>
  362. </View>
  363. )
  364. }
  365. }
  366. render() {
  367. return (
  368. <View style={{ flex: 1, backgroundColor: 'transparent' }}>
  369. {this.props.titleBool ? (
  370. <View>
  371. <TouchableOpacity
  372. // style={{ backgroundColor: '#FFF' }}
  373. onPress={() => {
  374. this.props.moreTouch()
  375. }}
  376. style={{
  377. backgroundColor: '#FFF',
  378. flexDirection: 'row',
  379. justifyContent: 'space-between',
  380. padding: 10,
  381. paddingLeft: 20,
  382. }}
  383. >
  384. <View style={{ flexDirection: 'row' }}>
  385. {this.props.imageSource ? (
  386. <Image
  387. source={this.props.imageSource}
  388. style={{
  389. width: 16,
  390. height: 18,
  391. marginRight: 8,
  392. alignSelf: 'center',
  393. }}
  394. />
  395. ) : null}
  396. <Text
  397. style={{
  398. color: this.props.titleColor,
  399. fontSize: 16,
  400. lineHeight: 22,
  401. letterSpacing: 0.43,
  402. fontWeight: '600',
  403. alignSelf: 'center',
  404. }}
  405. >
  406. {this.props.titleName}
  407. </Text>
  408. </View>
  409. {this.props.titleIcon ? (
  410. <Icon
  411. name={this.props.titleIcon}
  412. size={26}
  413. style={{ alignSelf: 'center' }}
  414. color={'#CCC'}
  415. />
  416. ) : null}
  417. </TouchableOpacity>
  418. </View>
  419. ) : null}
  420. {this.props.listRowBool ? (
  421. // 列表形式
  422. <FlatList
  423. // keyExtractor={(item, index) => item[this.props.keyext]}
  424. keyExtractor={(item, index) =>
  425. this.props.keyext ? item[this.props.keyext] + index : index
  426. }
  427. extraData={this.props}
  428. data={this.props.data}
  429. ListFooterComponent={this.footerCom.bind(this)}
  430. columnWrapperStyle={{ flexWrap: 'wrap' }}
  431. numColumns={2}
  432. renderItem={({ item }) => this._renderItemRow(item)}
  433. onEndReachedThreshold={0.1}
  434. onEndReached={e => this.props.onEndReached()}
  435. refreshControl={
  436. <RefreshControl
  437. refreshing={this.props.Parentprops.isRefreshing}
  438. onRefresh={this.props.onRefresh.bind(this)} //(()=>this.onRefresh)或者通过bind来绑定this引用来调用方法
  439. tintColor="red"
  440. title={
  441. this.props.Parentprops.isRefreshing
  442. ? '刷新中....'
  443. : '下拉刷新'
  444. }
  445. />
  446. }
  447. />
  448. ) : this.props.homeLIstBool ? (
  449. <FlatList
  450. // keyExtractor={(item, index) => item[this.props.keyext]}
  451. keyExtractor={(item, index) =>
  452. this.props.keyext ? item[this.props.keyext] + index : index
  453. }
  454. extraData={this.props.extraData}
  455. columnWrapperStyle={{
  456. flexWrap: 'wrap',
  457. justifyContent: 'space-between',
  458. }}
  459. ListFooterComponent={
  460. this.props.homeLIstBool ? null : this.footerCom.bind(this)
  461. }
  462. numColumns={2}
  463. data={this.props.data}
  464. renderItem={({ item }) => this._renderItemCol(item)}
  465. />
  466. ) : (
  467. <FlatList
  468. // keyExtractor={(item, index) => item[this.props.keyext]}
  469. keyExtractor={(item, index) =>
  470. this.props.keyext ? item[this.props.keyext] + index : index
  471. }
  472. extraData={this.props.extraData}
  473. columnWrapperStyle={{
  474. flexWrap: 'wrap',
  475. justifyContent: 'space-between',
  476. }}
  477. ListFooterComponent={
  478. this.props.homeLIstBool ? null : this.footerCom.bind(this)
  479. }
  480. numColumns={2}
  481. data={this.props.data}
  482. renderItem={({ item }) => this._renderItemCol(item)}
  483. onEndReachedThreshold={0.1}
  484. onEndReached={e => this.props.onEndReached()}
  485. refreshControl={
  486. <RefreshControl
  487. refreshing={this.props.Parentprops.isRefreshing}
  488. onRefresh={this.props.onRefresh.bind(this)} //(()=>this.onRefresh)或者通过bind来绑定this引用来调用方法
  489. tintColor="red"
  490. title={
  491. this.props.Parentprops.isRefreshing
  492. ? '刷新中....'
  493. : '下拉刷新'
  494. }
  495. />
  496. }
  497. />
  498. )}
  499. </View>
  500. )
  501. }
  502. }
  503. const styles = StyleSheet.create({
  504. footer: {
  505. flexDirection: 'row',
  506. height: 24,
  507. justifyContent: 'center',
  508. alignItems: 'center',
  509. marginBottom: 10,
  510. marginTop: 10,
  511. },
  512. })