Grid.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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={{fontSize:12}}>
  292. {'型号:'+(item.model?(item.model.length>10?item.model.substr(0,10):item.model):"")}
  293. </Text>
  294. </View>
  295. <View
  296. style = {{
  297. flexDirection: 'row',
  298. justifyContent: 'space-between',
  299. }}>
  300. <Text style={{fontSize:12}}>
  301. {'规格:'+(item.specification?(item.specification.split("/")[0]+"/"+item.basicUnitName):"")}
  302. </Text>
  303. </View>
  304. <View
  305. style={{
  306. flexDirection: 'row',
  307. justifyContent: 'space-between',
  308. marginTop: 5,
  309. paddingRight: 7,
  310. }}
  311. >
  312. <Text style={{ fontSize: 18, lineHeight: 25, color: '#E70013' }}>
  313. {CURRENCY.currencySign}
  314. {ScaleUtil(item[this.props.priceKey] || item.salePrice)}
  315. </Text>
  316. <TouchableOpacity
  317. onPress={() => {
  318. this.props.shoppingCart(item)
  319. }}
  320. >
  321. <Icon name="icon-icon-gouwuche" color={gouwucheColor} size={22} />
  322. </TouchableOpacity>
  323. </View>
  324. </View>
  325. </View>
  326. )
  327. }
  328. // 底部
  329. footerCom() {
  330. if (this.props.Parentprops.showFoot == 0) {
  331. return <View />
  332. } else if (this.props.Parentprops.showFoot == 1) {
  333. return (
  334. <View style={styles.footer}>
  335. <Text
  336. style={{
  337. fontSize: 14,
  338. lineHeight: 20,
  339. letterSpacing: 0.17,
  340. color: '#999',
  341. }}
  342. >
  343. 没有更多数据
  344. </Text>
  345. </View>
  346. )
  347. } else if (this.props.Parentprops.showFoot == 2) {
  348. return (
  349. <View style={styles.footer}>
  350. <ActivityIndicator />
  351. <Text>正在加载更多数据...</Text>
  352. </View>
  353. )
  354. }
  355. }
  356. render() {
  357. return (
  358. <View style={{ flex: 1, backgroundColor: 'transparent' }}>
  359. {this.props.titleBool ? (
  360. <View>
  361. <TouchableOpacity
  362. // style={{ backgroundColor: '#FFF' }}
  363. onPress={() => {
  364. this.props.moreTouch()
  365. }}
  366. style={{
  367. backgroundColor: '#FFF',
  368. flexDirection: 'row',
  369. justifyContent: 'space-between',
  370. padding: 10,
  371. paddingLeft: 20,
  372. }}
  373. >
  374. <View style={{ flexDirection: 'row' }}>
  375. {this.props.imageSource ? (
  376. <Image
  377. source={this.props.imageSource}
  378. style={{
  379. width: 16,
  380. height: 18,
  381. marginRight: 8,
  382. alignSelf: 'center',
  383. }}
  384. />
  385. ) : null}
  386. <Text
  387. style={{
  388. color: this.props.titleColor,
  389. fontSize: 16,
  390. lineHeight: 22,
  391. letterSpacing: 0.43,
  392. fontWeight: '600',
  393. alignSelf: 'center',
  394. }}
  395. >
  396. {this.props.titleName}
  397. </Text>
  398. </View>
  399. {this.props.titleIcon ? (
  400. <Icon
  401. name={this.props.titleIcon}
  402. size={26}
  403. style={{ alignSelf: 'center' }}
  404. color={'#CCC'}
  405. />
  406. ) : null}
  407. </TouchableOpacity>
  408. </View>
  409. ) : null}
  410. {this.props.listRowBool ? (
  411. // 列表形式
  412. <FlatList
  413. // keyExtractor={(item, index) => item[this.props.keyext]}
  414. keyExtractor={(item, index) =>
  415. this.props.keyext ? item[this.props.keyext] + index : index
  416. }
  417. extraData={this.props}
  418. data={this.props.data}
  419. ListFooterComponent={this.footerCom.bind(this)}
  420. columnWrapperStyle={{ flexWrap: 'wrap' }}
  421. numColumns={2}
  422. renderItem={({ item }) => this._renderItemRow(item)}
  423. onEndReachedThreshold={0.1}
  424. onEndReached={e => this.props.onEndReached()}
  425. refreshControl={
  426. <RefreshControl
  427. refreshing={this.props.Parentprops.isRefreshing}
  428. onRefresh={this.props.onRefresh.bind(this)} //(()=>this.onRefresh)或者通过bind来绑定this引用来调用方法
  429. tintColor="red"
  430. title={
  431. this.props.Parentprops.isRefreshing
  432. ? '刷新中....'
  433. : '下拉刷新'
  434. }
  435. />
  436. }
  437. />
  438. ) : this.props.homeLIstBool ? (
  439. <FlatList
  440. // keyExtractor={(item, index) => item[this.props.keyext]}
  441. keyExtractor={(item, index) =>
  442. this.props.keyext ? item[this.props.keyext] + index : index
  443. }
  444. extraData={this.props.extraData}
  445. columnWrapperStyle={{
  446. flexWrap: 'wrap',
  447. justifyContent: 'space-between',
  448. }}
  449. ListFooterComponent={
  450. this.props.homeLIstBool ? null : this.footerCom.bind(this)
  451. }
  452. numColumns={2}
  453. data={this.props.data}
  454. renderItem={({ item }) => this._renderItemCol(item)}
  455. />
  456. ) : (
  457. <FlatList
  458. // keyExtractor={(item, index) => item[this.props.keyext]}
  459. keyExtractor={(item, index) =>
  460. this.props.keyext ? item[this.props.keyext] + index : index
  461. }
  462. extraData={this.props.extraData}
  463. columnWrapperStyle={{
  464. flexWrap: 'wrap',
  465. justifyContent: 'space-between',
  466. }}
  467. ListFooterComponent={
  468. this.props.homeLIstBool ? null : this.footerCom.bind(this)
  469. }
  470. numColumns={2}
  471. data={this.props.data}
  472. renderItem={({ item }) => this._renderItemCol(item)}
  473. onEndReachedThreshold={0.1}
  474. onEndReached={e => this.props.onEndReached()}
  475. refreshControl={
  476. <RefreshControl
  477. refreshing={this.props.Parentprops.isRefreshing}
  478. onRefresh={this.props.onRefresh.bind(this)} //(()=>this.onRefresh)或者通过bind来绑定this引用来调用方法
  479. tintColor="red"
  480. title={
  481. this.props.Parentprops.isRefreshing
  482. ? '刷新中....'
  483. : '下拉刷新'
  484. }
  485. />
  486. }
  487. />
  488. )}
  489. </View>
  490. )
  491. }
  492. }
  493. const styles = StyleSheet.create({
  494. footer: {
  495. flexDirection: 'row',
  496. height: 24,
  497. justifyContent: 'center',
  498. alignItems: 'center',
  499. marginBottom: 10,
  500. marginTop: 10,
  501. },
  502. })