Grid.js 15 KB

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