Grid.js 15 KB

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